Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SOLVED] atan2 ineffective when Rotating x to face y?  (Read 8559 times)

0 Members and 1 Guest are viewing this topic.

lazerblade

  • Newbie
  • *
  • Posts: 14
    • View Profile
[SOLVED] atan2 ineffective when Rotating x to face y?
« Reply #15 on: October 23, 2011, 09:48:33 pm »
Quote from: "sbroadfoot90"
Reading through all your posts, you kinda never really explain what sort of behaviour you want. Everything you say is really confusing. Perhaps you should just start over, draw a simple diagram and let us know what you want to do.

This is not a programming problem, it's a maths problem.


Well, I believe I already stated quite clearly that I have drawn this out many times over. If you need a diagram, I can provide one. I took the time to draw this up in GIMP:



Given an object centered at point A, and given a point B, determine Theta, or what rotation an object centered at A would need in order to face B.

My solution was the arctangent of D over C, plus compensation for quadrant and special cases. However, I learned about atan2() which does quadrants, division, and special cases for you, which is why I'm using it. I then multiply my solution by 180 over PI in order to convert to degrees.

I expect to get back theta in degrees. I then expect this rotation be set for an object centered at A, causing it to face B.

If the following function should be doing this, that means the bug is elsewhere in my code. An explanation of why I'm doing it wrong, or a clarification that it should be working are both useful to me.


Code: [Select]
float angleBetweenVectors(sf::Vector2f a, sf::Vector2f b)
{
   return 57.2957795f * atan2(b.y - a.y, b.x - a.x);
}



If you need me to clarify anything, or if you have any additional questions, just say so.

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
[SOLVED] atan2 ineffective when Rotating x to face y?
« Reply #16 on: October 24, 2011, 12:19:13 am »
That should be exactly correct... if you expect the rotations to go counterclockwise. However, I think SFML2 was recently changed to make angles go clockwise, so you'll have to compensate with theta_final = 360-theta
I use the latest build of SFML2

lazerblade

  • Newbie
  • *
  • Posts: 14
    • View Profile
[SOLVED] atan2 ineffective when Rotating x to face y?
« Reply #17 on: October 24, 2011, 12:25:41 am »
Quote from: "OniLink10"
That should be exactly correct... if you expect the rotations to go counterclockwise. However, I think SFML2 was recently changed to make angles go clockwise, so you'll have to compensate with theta_final = 360-theta


Thank you very much. I shall hunt for the bug elsewhere. You probably understand that knowing where a bug isn't, is almost as useful as knowing where a bug is. You have all been quite helpful, so I extend my gratitude. :D

lazerblade

  • Newbie
  • *
  • Posts: 14
    • View Profile
[SOLVED] atan2 ineffective when Rotating x to face y?
« Reply #18 on: October 26, 2011, 10:07:44 pm »
I finally managed to fix the bug. Because you confirmed that the my rotation function, I was able to look for it in the one other place I thought it might be, which was the local translation function.

It was really a relief to fix this bug after musing over it for about a month.