1

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.

user3319320
  • 229
  • 1
  • 3
  • 11
  • The range of arccos is not [0,2pi)! You need to add a constant to get everything "in line", depending on whether x or y are greater than or less than zero. EDIT: http://www.wolframalpha.com/input/?i=graph+y+%3D+arccos%28x%29 – druckermanly Dec 08 '14 at 05:20
  • Whist I see what you are getting at, could you please explain that a little more? – user3319320 Dec 08 '14 at 05:50

1 Answers1

1

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
Community
  • 1
  • 1
Spektre
  • 49,595
  • 11
  • 110
  • 380
  • 1 vector is sufficient. – user1095108 Dec 08 '14 at 07:45
  • @user1095108 only for direction ... but that causes the 'random' rotations along side that are mentioned in OP so no that is not sufficient (unless your object is single line parallel to direction) – Spektre Dec 08 '14 at 07:47
  • The same thing could be contained in 1 quaternion too. – user1095108 Dec 08 '14 at 07:54
  • @user1095108 yes but quaternion is 4D you need one chunk of additional info ... that can not be extracted from single 3D vector. that is why one vector is added does not matter which but has to be the same all the time and not lie in the direction vector. Or am I missing something new in math? – Spektre Dec 08 '14 at 07:57
  • No you aren't missing anything, you could just have expanded your answer a bit. You could have written, 2 vectors or 1 quaternion. – user1095108 Dec 08 '14 at 08:16
  • @user1095108 well but the OP does not include any quaternions the input is movement direction vector (I assume from some sort of physic simulation). but I get the statement that is bothering you feel free to edit it... – Spektre Dec 08 '14 at 08:19
  • So i have a direction vector, up vector, cross product and origin (x,y,z,w)? What do I need to do using standard openGl commands? Do i need to calculate a rotation vector as i have not seen a way to apply a normal transformation matrix? I cannot use any extra libraries. Sorry for asking again! – user3319320 Dec 08 '14 at 15:13
  • @user3319320 create array `GLfloat m[16];` load the appropriate positions with the axis vectors and origin (like in the transform matrix anatomy link) and load/multiply it to the OpenGL matrix you want to rotate. that is all no rotation need it is already inside ... also look here if you are confused http://stackoverflow.com/a/21100338/2521214 – Spektre Dec 08 '14 at 21:12
  • Hey, thanks. I got that working, but I have a scaling issue where my 3D object is only drawn in 2D along an axis now? It seems to be facing the correct way though :P using glMultMatrixf(Matrix44f matrix); – user3319320 Dec 08 '14 at 21:15
  • @user3319320 did you use 3 perpendicular vectors to each other ? if you set any of them to zero then that axis will be flat ... the up vector is used to create first perpendicular vector and not used in matrix... you need use 2 times the cross product ... – Spektre Dec 08 '14 at 21:17
  • That would be why.. although now it is still flat not not going in the right direction. So I do the cross product of the up and the direction vector- which column does that go in? and then the cross product of the direction vector and the last cross product. rotation_matrix = Matrix44f({cross1, cross2, direction, origin }?. Thanks – user3319320 Dec 08 '14 at 21:30
  • @user3319320 the order of the axises depends on your mesh. for example mine meshes have forward direction mostly in Z axis. so the dir vector goes to Z axis (3th column) if you have wrong order then the mesh will be rotated/mirrored in 90/180 deg from wanted direction. also order of vectors inside crossproduct determine polarity of the resulting vector so you can play with that too... if you are lost then add images/small mesh example to question ... do not forget that your projection and modelview matrices are also a key in this – Spektre Dec 09 '14 at 07:36
  • @user3319320 take direction vector that is not rotating your mesh at all, then compute the other 2 vectors and the resulting matrix should be unit so look inside your vectors and the position will reveal it self ... also the polarity – Spektre Dec 09 '14 at 07:38