Writing the behaviour module
First thing to do is create a class and hinerit from the base BehaviourModule class. With a constructor a desctructor and a static creator function used by atoms to create an isntance of this module.
コード ブロック | ||
---|---|---|
| ||
class FollowTargetModule: public Atoms::BehaviourModule { public: FollowTargetModule() {} virtual ~FollowTargetModule() {} static Atoms::BehaviourModule* creator(const std::string& parameter); }; Atoms::BehaviourModule* creator(const std::string& parameter) { return new FollowTargetModule(); } |
Lets add some attributes to our node. In this case we need only a target position. The attributes of the modules are stored inside a MapMetadata object, and you can add new attribute using the addAttribute function. So lets add this attribute inside the constructor.
コード ブロック | ||
---|---|---|
| ||
FollowTargetModule()
{
AtomsCore::Vector3Metadata targetPosition(AtomsCore::Vector3(0.0,0.0,0.0));
// The last argumet specify if this attribute can be override per agent
addAttribute("targetPosition", &targerPosition, true);
} |
Now we need to compute the new agent direction at each frame before the agent pose is computed, so we need to override the initFrame function.
You can override other function linke the endFrame or initSimulation or agentsCreated but in this case we need only the initFrame
コード ブロック | ||
---|---|---|
| ||
void initFrame(const std::vector<Atoms::Agent*>& agents, Atoms::AgentGroup* agentGroup = nullptr)
{
} |
In the init frame we need to get our targetPosition value and iterate over each agent to get the position and set the new direction
コード ブロック | ||
---|---|---|
| ||
void initFrame(const std::vector<Atoms::Agent*>& agents, Atoms::AgentGroup* agentGroup = nullptr)
{
// Get the target position from the moduel attribute
AtomsCore::MapMetadata& attributeMap = attributes();
AtomsCore::Vector3& targetPosition = attributeMap.getTypedEntry<AtomsCore::Vector3Metadata>("targetPosition")->get();
// iterate over each agent
for(Atoms::Agent* agent: agents)
{
if (!agent)
continue;
//get the agent position
AtomsPtr<AtomsCore::Vector3Metadata> positionPtr = agent->metadata().getTypedEntry<AtomsCore::Vector3Metadata>("position")->get();
AtomsPtr<AtomsCore::Vector3Metadata> directionPtr = agent->metadata().getTypedEntry<AtomsCore::Vector3Metadata>("direction")->get();
if(!positionPtr || !directionPtr )
continue;
// compute the new direction
directionPtr->set((targetPosition - positionPtr->get()).normalized());
}
} |
コード ブロック | ||
---|---|---|
| ||
void initFrame(const std::vector<Atoms::Agent*>& agents, Atoms::AgentGroup* agentGroup = nullptr)
{
// Get the target position from the moduel attribute
AtomsCore::MapMetadata& attributeMap = attributes();
AtomsCore::Vector3& targetPosition = attributeMap.getTypedEntry<AtomsCore::Vector3Metadata>("targetPosition")->get();
//get the override map metadata
AtomsPtr<AtomsCore::MapMetadata> targetPosition = attributeMap.getTypedEntry<AtomsCore::MapMetadata>("targetPosition_override");
// iterate over each agent
for(Atoms::Agent* agent: agents)
{
if (!agent)
continue;
//get the agent position
AtomsPtr<AtomsCore::Vector3Metadata> positionPtr = agent->metadata().getTypedEntry<AtomsCore::Vector3Metadata>("position")->get();
AtomsPtr<AtomsCore::Vector3Metadata> directionPtr = agent->metadata().getTypedEntry<AtomsCore::Vector3Metadata>("direction")->get();
if(!positionPtr || !directionPtr )
continue;
// compute the new direction
directionPtr->set((targetPosition - positionPtr->get()).normalized());
}
} |
Register the behaviour module
Compile the behaviour module
Use the behaviour module