I am attempting to solve a problem involving Quaternions and 3D rotation.

I am using a games engine (Torque 3D) for a project that I am developing. I have modified the gravity system so that the player is pulled in three dimensions towards a central point (the center of a spherical world).

I would like the player to also turn so that their feet are facing this central point, regardless of their position or initial orientation. I want the player to be able to stand on the spherical world, and currently, even though they are being pulled to the center of the world correctly, the orientation doesn't change.

The orientation is held in a Quaternion called "mOrient".

I know that the solution will involve rotation matrices, and probably world/local space translations, but I am not sure how to put all this together.

I am currently attempting this, unsuccessfully: Assume (0,0,0) is the center point, getPosition() is the position of the player. mOrient is the players 3D rotation quaternion, this is what I need to change.

  Point3F gravityvec = Point3F(0, 0, 0) - getPosition();

  QuatF q = QuatF(gravityvec);

  mOrient = q;

Does anyone have a solution for this?

I'm programming in C++ inside a games engine, but Pseudocode would be fine, I just need some assistance with the mathematical side.


1 Answer 1


Build a LookAt rotation matrix and then you can converter it to a quaternion.

Most engines already have that function built-in but here's the pseudo-code:

mat3 LookAt(vec3 up, vec3 front)
  vec3 right; 
  mat3 m;

  right = CrossProduct(Normalize(up), Normalize(front)); // figure out the right vector

  front = CrossProduct(right, up); // make sure front is properly oriented

  m[0] = right;
  m[1] = up;
  m[2] = front;

  return m;

up is the vector from the center of the world to the player (the negative of your gravity vector)

front is where the player is facing (assuming your 3D model faces in the Z direction.)


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .