Will
Will

Reputation: 75615

drawing a model on a slope

My models are axis-aligned.

I know the normal of the surface I want to draw them on; how do I compute the rotations or rotation matrix that I should use to draw the model perpendicular to the surface?

Upvotes: 0

Views: 480

Answers (1)

Gareth Rees
Gareth Rees

Reputation: 65854

Well, if u is the unit up vector, and n is the unit surface normal, then you want a rotation that turns u into n. (This rotation isn't unique: you can compose any rotation about u before it, and any rotation about n after it.)

How to compute this rotation? Well, it depends on what framework or API you're working with, and how rotations are represented. A decent framework has an API call for this, for example in Unity 3D there's Quaternion.FromToRotation.

If you don't have an API call for that, then you can work it out for yourself. Any rotation can be represented in axis–angle form, say (a, θ). Here θ is the angle between the vectors u and n (which you can compute using the cosine rule: u · n = |u| |n| cos θ), and a is an axis perpendicular to both vectors: a = u × n. (There are two special cases: when u = n the null rotation will do, and if u = −n any half-rotation about any vector perpendicular to u will do.)

If you're using OpenGL you can now call glRotate.

And if you don't even have that ... well, you've no business asking this kind of basic question! But see Wikipedia for how to turn axis–angle representation into matrix representation.

Upvotes: 3

Related Questions