How to project a vector onto global axes?

I’m finally making some good progress on my raycasting suspension vehicle, having figured out the equations for drag and friction (well, I had the equations, I just wasn’t calculating a variable correctly).

Now, my next thing to account for is this. When accelerating, I use the car’s LookVector and scale it by a constant that represents the force of the engine. This is okay, but I need to know how to project it on only the global Z and X axis, so that if a car is going upwards or downwards, the force is still going in only the global Z/X plane and not up or down (Y), causing cars to either skyrocket or nosedive.

This picture explains it perfectly. The line in the car represents the car’s LookVector. What I need to convert it to is the line on the bottom, which represents a global plane.

I’m applying this via a VectorForce relative to Attachment0, meaning that forward means forward relative to the attachment, meaning it depends on where the car is looking. Should I change it relative to the world?

Any help is appreciated.

2 Likes

There may be several ways to accomplish this but here’s the one that was obvious to me.
What you could do is take the cross product of the car’s RightVector and a global UpVector (Not relative to the car, Vector3.new(0, 1, 0)).
This would result in a vector perpendicular to both the RightVector and the global UpVector, and for it to be perpendicular to Vector3.new(0, 1, 0) then it must be horizontal (as this UpVector is completely vertical). This should be the vector you are looking for.

I’m not sure how you would transform this into object space to be able to use it as a force relative to Attachment0, so unless you can figure that part out on your own I would recommend using world space.

3 Likes

Thanks for the information! I’ll try this and get back to you, seems promising. I never really thought about cross product, that’s really smart.

Works like a charm, thanks.

Code:

local engineForce = chassis.CFrame.RightVector:Cross(Vector3.new(0, 1, 0)).Unit * -tune.EngineForce
chassis.Throttle.Force = chassis.Position + engineForce

With the attachment relative to world.

3 Likes