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.
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.
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)
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)
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.