A.I & Path Following In Bepuphysics

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
knightbenax
Posts: 9
Joined: Fri Feb 08, 2013 8:27 am

A.I & Path Following In Bepuphysics

Post by knightbenax »

Hi, i have two questions.

First one, i am trying to get an a.i Vehicle entity yo drive around a track itself, i have my raycast all set up for navigating through the track and all. What i did was that once it detects through a raycast that its far away from the wall of the track it should rotate back to be in line with the wall. No matter the calculations i do i can't seem to get it to rotate right.

Here is my code snippet.

Code: Select all

this.Accelerate();
if (rdistance > 3) 
            {
                steered = true;
                Vector3 right = new Vector3(3, 0, 0);
                Vector3 difference = position - right;
                Vector3 newForwardUnit = Vector3.Normalize(Vehicle.Body.Position - right);
                Vector3 rotAxis = Vector3.Cross(Vehicle.Body.OrientationMatrix.Forward, newForwardUnit);

                float rotAngle = (float)Math.Acos(Vector3.Dot(Vehicle.Body.OrientationMatrix.Forward, newForwardUnit));
                angle = rotAngle;
                //angle = mRotation;
                Vehicle.Wheels[1].Shape.SteeringAngle = angle;
                Vehicle.Wheels[3].Shape.SteeringAngle = angle;
            }
The accelerate method there is simply this

Code: Select all

Vehicle.Wheels[1].DrivingMotor.TargetSpeed = ForwardSpeed;
            Vehicle.Wheels[3].DrivingMotor.TargetSpeed = ForwardSpeed;

Secondly, if i change method to using pathtfinding like the example I found in the demo, it moves, along the line perfectly, but it never rotates to face the forward direction of the curve, so its like the car moves on its side, then back to its front and so forth.

Where could i be wrong?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: A.I & Path Following In Bepuphysics

Post by Norbo »

Here is my code snippet.
Arbitrarily subtracting (3,0,0) from the position isn't really a meaningful operation in most contexts. Because of this, the current value of newForwardUnit is likely meaningless.

In addition, rotAngle is computed using a dot product alone. However, using the dot product in that way only tells you the angle between two vectors. So, pi/4 radians to the left will have the same value as pi/4 radians to the right (or pi/4 radians down or up...). That isn't very useful for steering; the car would always turn the same direction.
Secondly, if i change method to using pathtfinding like the example I found in the demo, it moves, along the line perfectly, but it never rotates to face the forward direction of the curve, so its like the car moves on its side, then back to its front and so forth.
The path following demo is designed just to make an entity follow a very specific path. You can specify the specific locations and rotations along a curve. While a proper usage would allow the car to move and rotate 'correctly,' it wouldn't behave like a true dynamic vehicle at all- it would be an animation as opposed to AI.
knightbenax
Posts: 9
Joined: Fri Feb 08, 2013 8:27 am

Re: A.I & Path Following In Bepuphysics

Post by knightbenax »

Hi, i have able to fix the issue. Now i want to use CardinalSplineCurve3D for variable speed and movement through a path but i have no idea how to set the speed of the entity for a particular point.

All i see in the constructors is a time variable and the Vector3 point.

How can i set this?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: A.I & Path Following In Bepuphysics

Post by Norbo »

The CardinalSplineCurve3D just specifies a series of vectors associated with time parameters. While you could manually place the points and times such that there is a rough speed maintained, this is quite tedious.

Instead, the curve should be resampled. One option would be to use the VariableLinearSpeedCurve. You can specify a 'velocity' curve which dictates how fast objects move around on the given 'position' curve. More samples increases quality at the cost of a little performance.
knightbenax
Posts: 9
Joined: Fri Feb 08, 2013 8:27 am

Re: A.I & Path Following In Bepuphysics

Post by knightbenax »

Thanks but could i get a sample code of how to use VariableLinearSpeedCurve, especially declaring it.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: A.I & Path Following In Bepuphysics

Post by Norbo »

There is an example of the ConstantLinearSpeedCurve in the PathFollowingDemo, commented out; it looks like this:

Code: Select all

            //There's also a constant speed and variable speed curve type that can be used.
            //Try the following instead to move the entity at a constant rate:
            positionPath = new ConstantLinearSpeedCurve(5, wrappedPositionCurve);
This can be modified into a VariableLinearSpeedCurve by creating another curve that specifies the speed at various times, something like this:

Code: Select all

            var speedCurve = new StepCurve1D();
            speedCurve.PreLoop = CurveEndpointBehavior.Wrap;
            speedCurve.PostLoop = CurveEndpointBehavior.Wrap;
            speedCurve.ControlPoints.Add(-1, 1);
            speedCurve.ControlPoints.Add(0, 2);
            speedCurve.ControlPoints.Add(1, 4);
            speedCurve.ControlPoints.Add(2, 8);
            speedCurve.ControlPoints.Add(3, 16);
            speedCurve.ControlPoints.Add(4, 8);
            speedCurve.ControlPoints.Add(5, 4);
            speedCurve.ControlPoints.Add(6, 2);
            speedCurve.ControlPoints.Add(7, 5);
            speedCurve.ControlPoints.Add(8, 10);
            speedCurve.ControlPoints.Add(9, 16);
            speedCurve.ControlPoints.Add(10, 1);

            positionPath = new VariableLinearSpeedCurve(speedCurve, wrappedPositionCurve, 1000);
The last parameter specifies how many samples to take. 1000 is a lot; that helps prevent weird behavior where aren't enough samples to smooth things out. You can probably get by with fewer, though.
Post Reply