Ground Hugging (Similar to Mario Kart, Super Mario Galaxy..)

Hey guys, so I’ve been trying to figure out how to implement ground hugging.

Which is basically having the player model’s down vector always facing the ground.

I’ve been trying to do it using the entity’s lookAt function gathering the ground normal etc but I haven’t been able to correctly do this.

In Unity, I’ve made it several times using Quaternion.LookRotation and making a Cross product but I don’t know what would be the equivalent in PlayCanvas.

Here’s a few very poor 2D illustrations of what I’m trying to do;

And here’s the Unity thread that helped me through the problem back in the day (however I’ve had no luck translating this to JS):

If you have the normal of the ground you want to hug, you can create the forward vector along the surface by cross producting the surface normal and the vehicle’s right vector. (You might have to do it the other way round as positive Z is ‘backwards’ in PlayCanvas)

Once you have the new forward vector, you can use the code from this thread to re-orientate the vehicle.

1 Like

Hey I think I got it working, thanks a lot for the helper function!

Is there any reads you recommend to understand more about Mat4? They’re like black magic for me right now, kind of like Quats are :stuck_out_tongue:

I don’t remember in which part matrix are, but it’s really useful to read the whole article.