user3319320
user3319320

Reputation: 259

3D rotation using direction vector

I have an object moving in 3D space (origin at the center). I am calculating the direction vector as it moves and using that to apply the rotation before any transformations to the object at the origin.

      direction vector (position, previousPos): positionX-previousPosX/mag etc

      transformation

      rX = acos(direction_vector_x); etc
      rY = acos(direction_vector_y);
      rZ = acos(direction_vector_z);

      rotate(rZ, rX,rY) // axis rotation 

      draw object

The object is in position at first but then randomly appears diagonally and the wrong way round. After that the rotation is smooth, but sometimes goes backwards or upside down? Could someone let me know how to calculate this? Thanks.

Upvotes: 1

Views: 2597

Answers (1)

Spektre
Spektre

Reputation: 51923

you are creating 3D coordinate system from single vector that is not enough ...

  • you need at least one other vector like Up or North vectpr that is not parallel with direction
  • then use cross product to generate 2 perpendicular vectors to eachother and direction
  • from that you have 3 axises, origin should be known also ...
  • so construct transform matrix and use that instead of Euler angles
  • see transform matrix anatomy

Upvotes: 1

Related Questions