Path Based Movement Question

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
Robert125
Posts: 3
Joined: Sun Sep 06, 2015 9:36 pm

Path Based Movement Question

Post by Robert125 »

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);
    }
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Path Based Movement Question

Post by Norbo »

By hang, do you mean it takes a long time to compute? I don't see anything in there that would explain excess computation time. I would recommend checking a profiler to get a rough idea of where the time is being spent. If the time is spent actually in this code, check for weird conditions like NaNs or billions of unnecessary invocations.

Also, note that the entity mover and paths stuff isn't fundamental to the engine, and it's often simpler to not use it (unless you really want a cardinal spline or something like that).
Robert125
Posts: 3
Joined: Sun Sep 06, 2015 9:36 pm

Re: Path Based Movement Question

Post by Robert125 »

Hey Norbo, thanks for the response. What I mean by hanging is it pauses for a fraction of a second (main thread doesn't freeze) when it switches to a new path. The entity just sorta chills there and doesn't move at all. The actual calculation time doesn't seem to be effecting it significantly, which is strange. What I tried doing to verify this is creating the path on another thread and blocking new threads from being created while this computation was happening. During the next main thread update it would pick up the newly generated path and begin traversing it. Unfortunately I ran into the same problem once again, the entity pauses briefly before starting the new path.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Path Based Movement Question

Post by Norbo »

At a glance, I don't notice anything in the posted code which would explain that. The EntityMover is very simple- for dynamic entities it wraps a motor, and for kinematic entities it just sets velocities directly. As long as the EntityMover goal is being set and everything is in the space that should be, there's no physics-side reason for a pause.
Robert125
Posts: 3
Joined: Sun Sep 06, 2015 9:36 pm

Re: Path Based Movement Question

Post by Robert125 »

All right, thanks for the info.
Post Reply