PathEngine home previous: Getting the position under the mousenext: Placing obstructions ‑ using collision contexts
Contents, Programmers Guide, Example Projects, Tutorials, Tutorial 1, Placing and moving agents

Placing and moving agents

Interface iAgent objects represent the placement of an agent or obstruction with a given shape on a given mesh.

The application starts out with no agent or obstacle placed, so we initialise our agent pointers to null:

iAgent *agent=0;
iAgent *obstruction_agent1=0;
iAgent *obstruction_agent2=0;

The following code is added to our our key message switch statment to place or move the user controlled agent when '4' is pressed:

cPosition p=testbed->positionAtMouse();
if(p.cell!=-1 && !mesh->testPointCollision(agent_shape,context,p))
{
	if(!agent)
		agent=mesh->placeAgent(agent_shape,p);
	else
		agent->moveTo(p);
}

If there is no position under the mouse then placement or movement will not be attempted.
Also we don't allow the agent to be placed or moved to a position which is obstructed.

The iMesh::testPointCollision() function checks collision for the agent's shape at the requested position.

The context parameter specifies a state of collision that should apply to the query.
When we add obstacles we will include them in this context.
The result is that it will be impossible to place an agent at a position overlapping an obstacle.
(More about this in the next section.)

Note that the iAgent is constructed when the agent is placed on the mesh.
An iAgent object represents the placement of some entity on a mesh, as opposed to representing the entity itself.
So to move a character in a game from one mesh to another requires the destruction of one iAgent object and the construction of another to correspond to placement on the new mesh.


Documentation for PathEngine release 6.00 - Copyright © 2002-2016 PathEnginenext: Placing obstructions ‑ using collision contexts