Problem animating multiple entities

dwardu

29-03-2011 17:58:49

I completed intermediate tutorial 1, and converted it into a class as i am going to use it for pathfinding. but when i populated the walk list, my agents will all move in the same direction, and not their designated direction according to their walk lists. Could someone have a look at what i am doing wrong please? the only shared values that i have between the agents are the root, scenemanager and the camera manager which is my camera class.


to summarize what i am doing when i initialize the object:
I pass the starting position of the agent, give it the camera manager class, the Root object that i initialized in my main application, then i pass it the camera manager, which will be used later on. since i am always creating the same entity with a RobotNode as the scene name,, i made sure it gets a unique name by assigning the scene node the entity is stored in as a guid, this is just a simple replacement of a string in the main example.
When i call the Move() method, the agent will move towards its destination.

I found out that the problem that i have is when i have multiple agents in the scene, the agents start to
move in a wierd way.
PTriangle is my own data structure.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mogre;
using MogreApplication.DataStructures;
using MogreApplication.PathFinding;

namespace MogreApplication
{
public class Agent
{
#region Properties
private Guid _ID;
private SceneManager _SceneManager;
private CameraManager _CameraManager;
private Root _Root;
private Vector3 _Position;

private PTriangle _StartTriangle;

public Vector3 Position
{
get { return _Position; }
set { _Position = value; }
}
public PTriangle StartTriangle
{
get { return _StartTriangle; }
set { _StartTriangle = value; }
}
private PTriangle _GoalTriangle;

public PTriangle GoalTriangle
{
get { return _GoalTriangle; }
set { _GoalTriangle = value; }
}
private Vector3 _Goal;

public Vector3 Goal
{
get { return _Goal; }
set { _Goal = value; }
}
private LinkedList<Vector3> _WalkList = null;

public LinkedList<Vector3> WalkList
{
get { return _WalkList; }
set { _WalkList = value; }
}
private Entity _Entity;
private SceneNode _Node;
private static float _WalkSpeed = 20.0f;
private static Vector3 _Direction = Vector3.ZERO;
private static Vector3 _Destination = Vector3.ZERO;
private static float _Distance = 0.0f;
private static bool _Walking = false;
private AnimationState _AnimState = null;
#endregion

#region Pathfinding Properties
private AStar _AStarPath;
public Guid ID
{
get { return _ID; }
set { _ID = value; }
}
#endregion

#region Constructors
public Agent(Root RootManager, SceneManager SceneManager, CameraManager CameraManager, Vector3 Position, PTriangle StartTriangle)
{
_SceneManager = SceneManager;
_CameraManager = CameraManager;
_ID = Guid.NewGuid();
_Root = RootManager;
_WalkList = new LinkedList<Vector3>();
_Position = Position;
// _WalkList.AddFirst(Position);
_AStarPath = new AStar(new EuclideanDistanceHeuristic());
_AStarPath.StartTriangle = StartTriangle;
_StartTriangle = StartTriangle;

//Create Entity
_Entity = _SceneManager.CreateEntity(_ID.ToString(), "robot.mesh");
_Node = _SceneManager.RootSceneNode.CreateChildSceneNode(_ID.ToString(), Position);
//_Node.Position = Position;

_Node.AttachObject(_Entity);
_Node.Scale(new Vector3(0.1f, 0.1f, 0.1f));
Sphere sph = _Entity.GetWorldBoundingSphere();
_Entity.CastShadows = true;

_AnimState = _Entity.GetAnimationState("Idle");
_AnimState.Loop = true;
_AnimState.Enabled = true;
}
#endregion

public void FindPath(Vector3 GoalPosition, PTriangle Goal)
{
_GoalTriangle = Goal;
_AStarPath.Goal = GoalPosition;
_AStarPath.findPath(_AStarPath.StartTriangle, Goal);

if (true)
{
_AStarPath.drawPath();
foreach (var item in _AStarPath.path)
{

_WalkList.AddLast(item);
}
}
}

public void Move(FrameEvent evt)
{
float mDeltaTime = evt.timeSinceLastFrame;

if (!_Walking)
{
if (nextLocation())
{
//Set the animation state of the object to walking
_AnimState = _Entity.GetAnimationState("Walk");
_AnimState.Loop = true;
_AnimState.Enabled = true;

//update the destination using the walklist
LinkedListNode<Vector3> tmp;
_Destination = _WalkList.First.Value;
tmp = _WalkList.First;
_WalkList.RemoveFirst();

//_WalkList.AddLast(tmp);
// will this make the bot loop in walking

_Direction = _Destination - _Node.Position;
_Distance = _Direction.Normalise();
_Walking = true;

}
else
{ // nowhere else to go
_AnimState = _Entity.GetAnimationState("Idle");
//set animation state to idle and set the loop to false
}
}
else
{
float move = _WalkSpeed * mDeltaTime;
_Distance -= move;
if (_Distance <= 0.0f)
{
//set the node destination to reached, and reset the direction to 0
_Node.Position = _Destination;
_Direction = Vector3.ZERO;
_Walking = false;
}else
{
//movement
_Node.Translate(_Direction * move);

// rotation
Vector3 src = _Node.Orientation * Vector3.UNIT_X;
//this is used to make sure that when we are doing a 180 degree turn
//there wont be any exception thrown
if ((1.0f + src.DotProduct(_Direction)) < 0.0001f)
{
_Node.Yaw(180.0f);
}
else
{
Quaternion quat = src.GetRotationTo(_Direction);
_Node.Rotate(quat);
}
// _AnimState.AddTime(mDeltaTime);
}
}
}

public bool nextLocation()
{
if (_WalkList == null ||_WalkList.Count == 0) return false;
return true;
}
}
}

smiley80

29-03-2011 19:31:53

the only shared values that i have between the agents are the root, scenemanager and the camera manager which is my camera class.

No, they also share these fields:
private static float _WalkSpeed = 20.0f;
private static Vector3 _Direction = Vector3.ZERO;
private static Vector3 _Destination = Vector3.ZERO;
private static float _Distance = 0.0f;
private static bool _Walking = false;

dwardu

29-03-2011 22:51:07

wow i feel like a douche -_- this must have happened when i was working with static code all the time to test stuff out. Thanks :)