PathEngine home previous: Tutorial 2next: Line collision and collision info
Contents, Programmers Guide, Example Projects, Tutorials, Tutorial 2, Agent rotation

Agent rotation

The PathEngine collision model is based on shapes that translate with an agent's local origin.
(See The PathEngine Movement Model.)
So for characters that need to rotate it's a good idea to use a collision shape that approximates a circle centred on the agent origin.
So this means a regular n-sided polygon.
A shape like the octagon we used in 'tutorial1' is a good example.

In game we would see the character rotating but the PathEngine collision shape would remain unaffected.
In fact this is usually preferable to collision that takes character rotation into account because AI and user control is greatly complicated if character rotation can be blocked by collision.

For this tutorial we simply add a marker to the top of our agent to indicate the current heading for the agent.
When the user rotates the agent this marker rotates.
Moving the agent forward moves the agent in the direction of the marker.

Implementing rotation

A variable is added in main.cpp to keep track of the current heading:

// direction agent is currently facing
    float agent_heading = 0;

An arrow is drawn on the agent to indicate this heading:

testbed->setColour("orange");
testbed->drawAgentHeading(agent.get(), 30, 20, agent_heading);

The interesting stuff is all in the extra files 'moveagent.cpp' and 'sliding.h'.
We include 'moveagent.h' and add the following calls to the update loop:

// move the agent under the control of user input
    if(agent)
    {
	    TurnAgentUnderMouseControl(testbed, agent_heading);
	    MoveAgentUnderKeyControl(testbed, pathengine, agent, agent_heading, context, 6.0f);
    }

The following code in 'moveagent.cpp' takes care of rotating the agent under user control:

void TurnAgentUnderMouseControl(iTestBed* testbed, float& agent_heading)
{
	int32_t dx,dy;
	testbed->getMouseScreenDelta(dx,dy);
	agent_heading += dx * 0.016f;
	agent_heading = fmod(agent_heading, 6.2856f);
}

Documentation for PathEngine release 6.04 - Copyright © 2002-2024 PathEnginenext: Line collision and collision info