Make the object perpendicular to the terrain?

This is a noob question: how do I make a model perpendicular to the terrain? I am creating a world editor, where I can choose a model that follows my mouse. Now I tried to use lookAt() using the contactNormal() (from raycasting), mult quaternions with contact points, but with no results.


can you provide a code sample of the attempts you made?


  • I tried to use the contactNormal provided by my raycasting collision in the lookAt() method of my object

      Quaternion q = new Quaternion();
      q.lookAt(collisionResults.getClosestCollision().getContactNormal(), Vector3f.UNIT_Y);

But the tree stays paralell to the ground. (If the image is not loading, click here)

So, this is my problem. I hope you can help me :wink:

Ah. the problem that you have is that lookAt() is to make the “Z” axis of the tree look at the terrain.

You have two easy solutions:

  1. Rotate the tree model mesh so that the Z axis is parallel to the tree, not the Y axis (rotate 90 degrees on X axis).

  2. make the tree a child of an empty node that has the Z axis parallel to the tree (rotate the tree spatial locally 90 degrees on X) and use the look at on the empty node.

you also need the negative contact normal, or you can rotate -90 degrees instead.

Thanks! Rotating my model 90 degrees in the X axis solved my problem!

A Quaternion is a rotation… but so is a 3x3 matrix… and a 3x3 matrix is nicely the three orthogonal vectors representing the rotated coordinate axes x, y, and z. So if you can find those then you can make a rotation.

You already have up:
Vector3f up = contactNormal;

Now you just need the other two:
Vectof3f temp = tree.getLocalRotation().mult(Vector3f.Z); // forward rotation in its current view
Vector3f left = up.cross(temp); // should be left axis
Vector3f forward = left.cross(up);

Quaternion newRotation = new Quaternion().fromAxes(left, up, forward);

Edit: there is always the chance that I got the cross products backwards… if you see strange results then try printing the values.

Thanks, @pspeed! I don’t understand matrixes very well, but your solution was very helpful! Thanks!

Maybe this is useful: