0

I try to change the orientation of my objects towards the center with atan2f but it's give me the wrong value. Am I wrong somewhere ?

glm::vec3 center = glm::vec3(0.4f, 0.4f, 0.0f);
for(int i = 0; i < num; i++){
  float rel = i / (float) num;
  float angle = rel * M_PI * 2;
  glm::vec3 position = glm::vec3(cos(angle), glm::sin(angle), position.z);

  position.x *= radius; position.y *= radius;
  position.x += center; position.y += center;
  glm::mat4 trans = glm::translate(glm::mat4(1.0f), position);

  float newAngle = atan2f(position.y - center.y, position.x - center.x);

  glm::mat4 rotation = glm::rotation(glm::mat4(1.0f), newAngle, glm::vec3(0, 0, 1));

  numModel.at(i) = trans * rotation;
}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Vitge
  • 49
  • 1
  • 5
  • Can you notice anything special about the wrong value? Maybe you need to invert the sign or add `pi/2` to it or something similar. – HolyBlackCat Jul 21 '17 at 11:02
  • @HolyBlackCat Thanks for your answer. I have 3 object, if i try to change their angle with atan2f, I have one it stay to 0 degrees , another approximately 120 degrees and another -120 degrees. The rights value must be 90 degres, 135 degrees approximately, and the last approximately -135 degrees too.. – Vitge Jul 21 '17 at 12:36

1 Answers1

0

If you want to rotate an object around the center of the world you have to set up a rotation matrix first:

float angle = ...;
glm::vec3 rotAxis( ... );
glm::mat4 rotMat = glm::rotation( glm::mat4(1.0f), angle, rotAxis );

Note the glm::rotation function calculates the sine and the cosine of the angle and sets the appropriate fields inside the matrix.

2nd you have to setup your objects position matrix, this is the matrix which describes the initial position of the object:

glm::vec3 position( radius, 0.0f, 0.0f );
glm::mat4 transMat = glm::translate( glm::mat4(1.0f), position );

To rotate the object around the center of the world you have to multiply the rotation matrix rotMat by the model matrix transMat, because the rotation matrix defines the reference system, for placing the object.

glm::mat4 modelMat = rotMat * transMat;

If you want to rotate an object around its own origin you have first to place your object in the world and then you have to rotate it:

glm::mat4 modelMat = transMat * rotMat;

See also the answer to the question OpenGL transforming objects with multiple rotations of Different axis

In your case, where you want that the object is directed to the center of the world, you first have to translate the object to its place. 2nd you have to calculate the angle between the X-axis of the world and the direction from the object to the center. Finally you have to turn the object in the opposite direction.

 glm::vec3 position( ... );
 glm::vec3 center( ... );
 glm::mat4 transMat = glm::translate( glm::mat4(1.0f), position );

 float angle = atan2f(center.y - position.y, center.x - position.x);

 glm::vec3 rotAxis( 0.0f, 0.0f, 1.0f );
 glm::mat4 rotMat = glm::rotation( glm::mat4(1.0f), -angle, rotAxis );

 glm::mat4 modelMat = transMat * rotMat;
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Hi, I already created the object around the center and did the translate. What I try to do, is not to rotate around a center but orienting the vertices of my objects to the center. – Vitge Jul 21 '17 at 13:56