Calculating a rotation based on four 3d points in space.


I’m building a game similair to the f-zero x game. It is a futuristic hover car game that sticks/magnestised to a track that has loops and everything you can imagine. The hardest part is probably the physics for keeping the craft sticking to the track.

So there are titorials out there that use addforxe to create hovercars but I feel its too unreliable to keep the car attached to the track, thus I have turned off unreal engine gravity physics and perform my own.

currently to keep the car on the track, I do a line trace from the center of the craft and shoot down using an inverted up vector to see how far the track is. The crafts position is set and maintained by a specified distance on the vector shooting straight down to the track so the craft clings to the track. This works until you add slopes and tracks that go upside down.

to make the craft work on slope, the craft itself needs to rotate with the track slope and keep maintaining this. Ive had a few ideas of how this works by working out the forward vector by doing a cross product on the impact normal and current craft right vector but it still is messy.

Another way is to have a line trace at all four corners of the craft shoot downwards the negative up vector. then calculate a world position for each corner where the distance to hitting the track is the same distance as each other. Then using a **** load of sin and cos and vectors between the four points calculated work out a final rotation for the craft and set it to that. for example, if both line traces on the left have a higher z coordinate (up position), this would create a rotator which would tilt the craft to the right.
But working the exact blueprint code for this is complex. Has anyone done anything like this before?

Try ‘Make Rotator from ZX’, with the hit normal plugged into the Z, and the actor forward plugged into X. Plug rotator into a ‘SetWorldRotation’ node.