*sigh* i am too dumb for calculating a simple rotation...

hello again,

i have two points, which are connected with a joint. to visualize the joint i place the graphic of a chain between the two objects. the calculation of the point between the two points is pretty easy and works, but i'm somehow to dumb to calculate the correct rotation...

any help would be awesome! 🙂

here's the code:

Code:
// getting the positions of the two bodies
      Vector2DF posBody1 = Vector2DF(m_joint->GetBody1()->GetCenterPosition().x, m_joint->GetBody1()->GetCenterPosition().y);
      Vector2DF posBody2 = Vector2DF(m_joint->GetBody2()->GetCenterPosition().x, m_joint->GetBody2()->GetCenterPosition().y);

      float posX = posBody1.x - posBody2.x;
      float posY = posBody1.y - posBody2.y;
      float absPos = sqrt(posX * posX + posY * posY);

      float angle = sin(posX / absPos);

//   position is the point between the two bodies
      Vector2DF position = Vector2DF(posBody1.x - posX / 2, posBody1.y - posY / 2);

      SetPosition(position);   // works!
      SetRotation(angle);      // does not work... :(
You're thinking of sine = opposite / hypotenuse, but for that you'd need to use the inverse sine function of that ratio. Unfortunately it doesn't work because sine is multiple valued on (-PI, PI].

The standard way to do this is to use atan2 rather than arcsin: http://en.wikipedia.org/wiki/Atan2
ewjordan wrote:
You're thinking of sine = opposite / hypotenuse, but for that you'd need to use the inverse sine function of that ratio. Unfortunately it doesn't work because sine is multiple valued on (-PI, PI].

The standard way to do this is to use atan2 rather than arcsin: http://en.wikipedia.org/wiki/Atan2

thanks a lot! works like a charm! 🙂
hey how did you fix this?

I can't seem to rotate the body at the right angle X_x'
here's my code

float fDx = rotatingBody->m_position.x - vPointer.x;
float fDy = rotatingBody->m_position.y - vPointer.y;

rotatingBody->m_rotation = atan2(fDy, fDx);

*Edited:
ohh got it the b2Body was sleeping that's why it isn't rotating X_x'
anyways I tweaked the code a bit into
rotatingBody->m_rotation = atan2(fDy, fDx)- 0.5f * b2_pi;

yeah it works like a charm ^_^ thanks

Original thread: https://box2d.org/forum/viewtopic.php?t=281