943,864 Members | Top Members by Rank

Ad:
Jan 5th, 2009
0

I need math formula for this

Expand Post »
I'm creating a game and I need to rotate this object. Ok i'll make my explanation simple, I have Object1 and Object2.. Let's say Object1 is at coordinates 0,0 (x,y) and Object 2 is at coordinates 10,10... Object 1 is currently facing south, Now I need object 1 to be facing Object 2 so I need the "degree" value from object 1 to object 2. Does anyone have a math formula for getting the degree? or an alternative solution can help.
Similar Threads
Reputation Points: 7
Solved Threads: 1
Junior Poster in Training
it2051229 is offline Offline
82 posts
since May 2007
Jan 5th, 2009
0

Re: I need math formula for this

Click to Expand / Collapse  Quote originally posted by it2051229 ...
I'm creating a game and I need to rotate this object. Ok i'll make my explanation simple, I have Object1 and Object2.. Let's say Object1 is at coordinates 0,0 (x,y) and Object 2 is at coordinates 10,10... Object 1 is currently facing south, Now I need object 1 to be facing Object 2 so I need the "degree" value from object 1 to object 2. Does anyone have a math formula for getting the degree? or an alternative solution can help.
To get the degrees with two points the formula is

theta = arctan(y/x)

theta is your degrees so, theta = arctan(10/10) = 45 degrees
Reputation Points: 21
Solved Threads: 12
Junior Poster in Training
MatEpp is offline Offline
79 posts
since Jan 2009
Jan 8th, 2009
0

Re: I need math formula for this

You should look at the unit circle.

So obj 1 has a velocity of 0 in direction 3pi/4(south) and you want it to face northeast towards the obj 2.

so you need to rotate obj one anti clockwise on the angle pi/4.

this is a hint try to figure it out from there, if you get stuck then reply back.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Jan 12th, 2009
0

Re: I need math formula for this

(english is my second language and i cant recall how do you call line with no ends, axis is something different i think, well in my language. can anyone tell its name so i can make an edit, thanks)

i have an idea, but im not sure if it is usable, maybe if you tell me in which form you save the direction object is facing.
anyway, here's the idea, maybe you can sort out the rest of things.
you have mathematical formula that gives you angle between axis a and axis b where a = k*x + n, and b = k1*x + n1 (this is general form)
in your case one axis crosses the coordinates (0,0) - object 1 and (10,10) - object 2, and the other one is crossing (0,0) - object 1 and makes an angle of 270 degrees with positive side of x axis.
using this you calculate each axis, and also their parameters k and k1. formula for angle between two axes is:
tg (beta) = (k - k1) / 1 + k*k1
(i have no time right now my friend but ill come back soon and make things more precise, hope this rings you some bells )
Last edited by Alibeg; Jan 12th, 2009 at 6:08 am. Reason: grammar
Reputation Points: 11
Solved Threads: 11
Junior Poster in Training
Alibeg is offline Offline
81 posts
since Aug 2008
Jan 12th, 2009
1

Re: I need math formula for this

Lines do not have ends: although a line is often uniquely defined by two endpoints coincident with the line; a 'line' is correctly considered to be infinite in both directions.

Line segments are lines with ends... and a 'one ended line' is usually termed either a ray, or a half-line ( depending on whether or not it's considered to be directed).

An axis is generally just a direction vector - i.e. an axis has no position, only a (usually normalized) direction.

MattEpp's suggestion is roughly correct : determine the angle that object2 is relative to the reference angle of object1 ( the reference angle is at 0 degrees ), and subtract the current angle of object1 to give the angle to rotate by (which we'll call 'delta').

However, some considerations - arctan ignores the quadrant (infact, the input to arctan makes finding the quadrant impossible): so you can't differentiate between the output of arctan for certain pairs of (non-congruent-modulo-360) angles. That's basically bad, so, use arctan2 (C function is atan2). Remember output is radians, NOT degrees.

You also need to normalize the input to atan/atan2, else the result is meaningless.

So, in C:
  1. double obj1x, obj1y, obj2x, obj2y; /* positions of objects 1 & 2 */
  2. double obj1rot; /* rotation of object1*/
  3.  
  4. double relx = obj2x - obj1x;
  5. double rely = obj2y - obj1y;
  6.  
  7. /* length of relative position (i.e. distance) */
  8. double reldis = sqrt ( ( relx * relx ) + ( rely * rely ) );
  9.  
  10. /* avoid division by zero... */
  11. if ( reldis > EPSILON ) { /* EPSILON should be a very small number */
  12. /* normalized relative position */
  13. double n_relx = relx / reldis;
  14. double n_rely = rely / reldis;
  15. double theta = atan2 ( n_rely, n_relx );
  16. double delta = theta - obj1rot;
  17. obj1rot += delta;
  18. /* a bit contrived at the end, you can do what you want with delta here. */
  19. } else {
  20. /* objects are in the same place, degenerate result. */
  21. }

If your doing incremental movement (i.e. rotating a small amount every frame), then you'll actually want the smallest delta between theta and obj1rot mod 360, which is pretty easy to find given theta and obj1rot.
Last edited by MattEvans; Jan 12th, 2009 at 8:15 am. Reason: small changes
Moderator
Featured Poster
Reputation Points: 522
Solved Threads: 64
Veteran Poster
MattEvans is offline Offline
1,091 posts
since Jul 2006
Jan 12th, 2009
0

Re: I need math formula for this

Click to Expand / Collapse  Quote originally posted by MattEvans ...
Lines do not have ends: although a line is often uniquely defined by two endpoints coincident with the line; a 'line' is correctly considered to be infinite in both directions.

Line segments are lines with ends... and a 'one ended line' is usually termed either a ray, or a half-line ( depending on whether or not it's considered to be directed).

An axis is generally just a direction vector - i.e. an axis has no position, only a (usually normalized) direction.
thanks, thats what i meant
i made a complication with two lines. XD
Reputation Points: 11
Solved Threads: 11
Junior Poster in Training
Alibeg is offline Offline
81 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Game Development Forum Timeline: First openGL Triangle, help
Next Thread in Game Development Forum Timeline: need help with lighting in openGL.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC