I am trying to figure out the math to project a vector that represents my player movement direction from a top view ( Fig. 1 ) on to a plane which that is represented by a normal vector ( Fig. 2 ). The movement direction vector is calculated from the top view and doesnāt have any Z ( up/down ) component to it. I am using Bullet to get the normal of the surface that my player is walking on ( represented by the blue lines in Fig. 2 ) and I want to take my movement direction vector ( A ) and line it up with the surface like vector B in Fig. 3.

Fig. 1: Top View Movement Direction Vector

Fig. 2: Ground Normal Vector

Fig. 3: The Goal is Alignment of Movement Vector With Ground Surface

While both these answers would technically suffice, I presume you want a more general solution? Considering thereās several ways of doing it, do you want the answer with vectors or quaternions? As math notation, haxe or node examples (considering this is the Armory forum after all)?

@Naxela It would be great to get it in Haxe form, using the Armory Vector class, and the math notation could be useful too. Iām fine using Quats being that there is an applyQuat() function on the Vector class. That might be the best way to do it because I do want to keep the movement direction vectorās length the same, but I can always just normalize it and multiply it to set the length as long as I can get the rotation right.

Iām already getting the surface normal, the question was how to get the direction vector to line up with the surface when the only vector that I have to go off of is the surface normal.

Assume that n is the face normal and d the direction vector. p will be the resulting projection vector.

make sure that n has a length of 1.

calculate the dot product of d and n and store it in a variable. Iāll call it t for temp.

perform this calculation: p = d - ( n ā¢ t), where nā¢t means to multiply every value of n with t. This removes every āpartā of d that points into the direction of n. In your third figure nā¢t would be the vector pointing from the tip of B to the tip of A.

@zicklag I have a little more time to help you today. A first advice would be to take some time to familiarize yourself with the quaternions. This is essential to achieve a successful project (less CPU demanding) and not be confronted with rotation problems (cf. ācardanā problem with Euler).

Letās quickly summarize the need in 3 steps:
1/ obtain the quaternion qa which allows to pass from the vector A, normalized which represents the displacement of your object I suppose and noted Ć¢, to the vector N, perpendicular to the plane of projection which you have and noted ^n (read the cap on the n) (normalisation is essentiel before getting qa).
2/ applying this rotation qa to the vector A gives a vector Aā perpendicular to the inclined plane (thus // to N)
3/ do a tilt/attitude of -90Ā° to Aā and āitās doneā

Thus to do it:

you will find the āfromToā function in āArmory/iron math quatā to get the qa quaternion.

to set attitude -90Ā° to Aā, just apply a quaternion with x = 0, y = 0, z = -0.7071, w = 0.7071

@Didier It might just be my lack of properly understanding Quaternions, but I still think that the vector based approach is more appropriate in this case.
As far as I understand it, Quaternions are an elegant way of describing rotations in 3D space, because of the three complex numbers they use, while I assume he wants to project the vector onto the plane, instead of rotating it into the plane. Like a shadow (assuming that the incoming light is parallel to the surface normal).

As far as I understood the problem, this is what he wants:

where <Ā·, Ā·> resembles the dot product and the surface normal n has a length of 1. Also there is no rotation or any other (euler) angles involved. If one wants the resulting vector to be as long as the original one, one could apply some simple stretching to it.

@zaethan sure,it exists several ways as with vectorial or trigonometrics ways to resolve this ā¦ but quaternions are particularly well suited for game engine ā¦ and I find them more fun
Imagine them like if you where at the command of a plane and flying where you want.
If you look at Armory code, you will see that they are often used, giving a compact code, so I think too itās a good idea to start to use them if you want to take advantage of actual modern code.

Thank you all for your help! Iāll probably try out both techniques, and Iām glad to have two approaches to try and your explanations will help me understand Quaternion and Vector math a little bit more. This will help a lot. If I get it working, I will be using it in a first person controller that I will share with the community.

P.S. If anybody knows any websites dedicated to teaching math including Vector, Quaternion, or Matrix, that would be great.

I was able to get it working with the vector math strategy! I went with the vector based strategy because I understood it more than than the Quaternion based strategy, but as soon as I get a better understanding of Quats that will be a consideration.

Iāve still got to work out some physics issues, but this will help my character controller a lot. Thanks everybody!