Path Based Movement Question
Posted: Sun Sep 06, 2015 9:45 pm
Hey, first I'd like to say I appreciate the great physics library you have created here. Second, I'm having a bit of an issue with my click-to-move system. Essentially I want it to behave like a Diablo style system but I've run into a bit of an issue. It seems like whenever I click to move the EntityMover hangs for a fraction of a second. This becomes a huge issue when holding down the mouse button to rotate and what not as well (it moves really really slow since it is recalculating and hanging a bunch). I'm new to the library so I'm not 100% sure exactly how to fix this. Here are the relevant code bits:
Code: Select all
private static RPath _pathFinder;
private EntityMover _mover;
private Path<BEPUutilities.Vector3> _positionPath;
private double _pathTime;
private Space _spaceInstance;
private void Update()
{
_spaceInstance.Update();
if (Input.GetMouseButton(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
MoveToPositon(transform.position, hit.point);
}
}
if (_positionPath != null)
{
_pathTime += _spaceInstance.TimeStepSettings.TimeStepDuration;
_mover.TargetPosition = _positionPath.Evaluate(_pathTime);
var newPos = new Vector3(_mover.Entity.Position.X, _mover.Entity.Position.Y, _mover.Entity.Position.Z);
GroundActor(newPos);
}
}
private void MoveToPositon(Vector3 start, Vector3 end)
{
var startPos = Position.ParseVector(start);
var endPos = Position.ParseVector(end);
Position[] path;
if (_pathFinder.TryGetStraightPath(startPos, endPos, out path))
{
ResetPath();
ConstructPositionPath(path);
}
}
private void ResetPath()
{
_pathTime = 0;
_positionPath = null;
}
private void ConstructPositionPath(Position[] posPath)
{
var wrappedPositionCurve = new LinearInterpolationCurve3D
{
PreLoop = CurveEndpointBehavior.Clamp,
PostLoop = CurveEndpointBehavior.Clamp
};
for (int i = 0; i < posPath.Length; i++)
{
wrappedPositionCurve.ControlPoints.Add(i, new BEPUutilities.Vector3(posPath[i].X, posPath[i].Y, posPath[i].Z));
}
_positionPath = new ConstantLinearSpeedCurve(5, wrappedPositionCurve);
}