Getting the speed in the forward direction of a vehicle + pointing in direction of velocity


I am working at something, and for that, GetCurrentVehicleSpeedKmHour() just doesn’t do the trick. I need a method to get the speed of a vehicle in the direction it’s facing (relative to the rotation of the vehicle), so that moving along it’s other axes (like falling) doesn’t change anything abouth the speed. I also need a way to make it point in the direction it faces, but preferably with forces, witouth forcing rotation.

Many thanks in avance.

Use math.

What you want is to get the vector projection of your velocity onto your vehicle’s direction, once you do that, you have its velocity towards its direction.

1 Like


I tried this code:

[java]Quaternion direction = new Quaternion();

direction.lookAt(vehicle.getLinearVelocity(), new Vector3f(0,1,0));

float speed = vehicle.getLinearVelocity().dot(vehicle.getForwardVector(null));

info.setText("Speed of vehicle: " + speed);[/java]the top two lines are for calculating the direction it’s moving to, and the bottom lines are for the speed.

How can I apply a force to the vehicle so it will start pointing in that direction witout forcing it?

If you subtract the vehicle’s direction minus the normalized vehicle velocity, you will get the direction to which to apply the force to counter the velocity that goes away from the direction