Calculating direction between vectors

Hello guys,

I need a bit of help with vectors and direction. As I tried to create AI car which steers and move itself to the some point on the map, the car steers only to match the direction of the vector I told him to go. But it would be nice, if the car steers to the exact point. And yes, I’m using a part of code from MonkeyZone class AutonomousVehicleControl.

For example: the car starts at vector A (0, 0, 0), I tell it to go to vector B (0, 0, 10), the car will go straight forward because it match the rotation to vector B. BUT if I move the start point of the car to (5, 0, 0) but keep the rotation, car go again straight forward to the “vector B”. The problem in this case is that it misses the point and will go forward till the end of map.

I need to calculate new vector, what stores the direction between car and point and by which car can steer to the told point. Basic diagram below. The green point is location on the map, red one is vehicle, where arrow shows forward vector of the car.

I failed in solving this math problem :face_with_head_bandage:

Thank for any help (or docs, topics where it has been already solved)

Two things:

  1. You might want to look into Steering Behaviors ultimatively (e.g. see on github MeFisto94/jme3-artificial-intelligence), because they will smoothen movements, like not immediately make the car look into the right direction and if the target moves, don’t turn on the point. You could also do that on your own by interpolating between the current look direction and the target direction though.

  2. You want the “direction” vector: Direction from A to B = B - A. As simple as that. So it means in your case (-5, 0, 10).
    Two notes: A) Directional Vectors should always be normalized and as such should the result (so instead of -5, 0, 10, something like (-1/3, 0, 2/3) (or whatever .normalize() does), so they are always 1 long.
    B) Moving the Car would be current position + direction.mult(speed * tpf)

do you mean?

Vector3f directionVector = redCar.getWorldTranslation().substract(greenCar.getWorldTranslation()).normalize();

it will be just vector.

about rotation you can always just use redCar.lookAt() method, if you need “smooth” rotate you can always use interpolate/slerp methods

2 Likes

Thank you guys for every post. I’ll look at in next days :slight_smile:

Actually this works perfect.

 directionVector = vehicle.getPhysicsLocation().subtract(targetLocation); 
 directionVector.normalizeLocal();
 float angle = FastMath.PI - (vector3.angleBetween(directionVector));

where vector3 is

vehicle.getForwardVector(vector3).normalizeLocal();

Now just have to do the calculating for steering. Car steers only left. If the target is on right side, car does the 270 degree rotation (90 degree steers left, then steers right but go backward, and at 225 degree steers left again to match the new direction)

2 Likes

I went over how to do this without angles here:

Basic steering stuff including how fast to steer left or right. It won’t have a problem crossing 0 it won’t have a problem crossing 180… it won’t have a problem with any of the other half-dozen issues with using angles.

This doesn’t work for me. If the target is on X axle on the right side of the vehicle, in “left” it won’t have value like -1 or 1. When moving to the right straight to target It has value from -1 to -50. I need to calculate some float which will say if the target is on left or right in any direction of the car, not only the X or Z.

The float which will tell if the angle should multiply by -1 or not.

If you have a vector pointing left and a normalized vector of the “other object” relative to the ‘steering’ object then the dot product of those two vectors will be between -1 and 1.

Gonna try it. Thank. I’m going mad with these vectors :smiley: