Programatically creating entities

Requests regarding how to set up experiments in ARGoS.
Elendurwen
Posts: 41
Joined: Sat Aug 24, 2013 5:08 pm
Contact:

Programatically creating entities

Postby Elendurwen » Mon Dec 02, 2013 6:09 pm

Hi,

I want to add entities to my arena at some point in the program. As far as I understand, you have to use CFactory::New to do this, instead of just creating the entities yourself. So what I am trying is:

Code: Select all

CEntity pelletEntity = *CFactory<CEntity>::New("box"); CBoxEntity& pellet = *any_cast<CBoxEntity*>(pelletEntity); pellet.SetSize(CVector3(1,1,1)); pellet.SetMass(1); AddEntity(pelletEntity);
However, the cast throws an error. I also tried just adding the pelletEntity itself, commenting out the 3 lines of code in the middle, but that thew segmentation error. Could somebody please explain how you can add boxes (and set their properties) properly?

pincy
Site Admin
Posts: 632
Joined: Thu Mar 08, 2012 8:04 pm
Location: Boston, MA
Contact:

Re: Programatically creating entities

Postby pincy » Thu Dec 05, 2013 7:29 pm

You can use the following code.

=== my_loop_functions.h ===

Code: Select all

#include <argos3/core/simulator/loop_functions.h> class MyLoopFunctions : public CLoopFunctions { public: virtual void Init(TConfigurationNode& t_tree); };
=== my_loop_functions.cpp ===

Code: Select all

#include "my_loop_functions.h" #include <argos3/plugins/simulator/entities/box_entity.h> void MyLoopFunctions::Init(TConfigurationNode& t_tree) { /* Create the box */ CBoxEntity b = new CBoxEntity("my_box", // the id CVector3(1,1,1), // the position of the base center in m CQuaternion(), // the orientation true, // this box is movable CVector3(0.1,0.1,0.1), // the size of the box in m 1); // the mass in kg /* Add the box to the space */ AddEntity(*b); } REGISTER_LOOP_FUNCTIONS(MyLoopFunctions, "my_loop_functions");
Cheers,
Carlo
I made ARGoS.

Elendurwen
Posts: 41
Joined: Sat Aug 24, 2013 5:08 pm
Contact:

Re: Programatically creating entities

Postby Elendurwen » Thu Dec 05, 2013 7:50 pm

Ah, thanks! The comment next to the AddEntity in loop_functions.cpp confused me :)

Code: Select all

"Important: the entity must be created with a <tt>new</tt> statement or a CFactory::New() statement."

pincy
Site Admin
Posts: 632
Joined: Thu Mar 08, 2012 8:04 pm
Location: Boston, MA
Contact:

Re: Programatically creating entities

Postby pincy » Thu Dec 05, 2013 7:55 pm

Good point, that's a really bad comment :-) I'll fix it!

Cheers,
Carlo
I made ARGoS.


Return to “How to... ?”