I need math formula for this

Please support our Game Development advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2007
Posts: 81
Reputation: it2051229 is an unknown quantity at this point 
Solved Threads: 1
it2051229 it2051229 is offline Offline
Junior Poster in Training

I need math formula for this

 
0
  #1
Jan 5th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 79
Reputation: MatEpp is an unknown quantity at this point 
Solved Threads: 12
MatEpp MatEpp is offline Offline
Junior Poster in Training

Re: I need math formula for this

 
0
  #2
Jan 5th, 2009
Originally Posted by it2051229 View 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.
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,435
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 186
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: I need math formula for this

 
0
  #3
Jan 8th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 71
Reputation: Alibeg is an unknown quantity at this point 
Solved Threads: 10
Alibeg Alibeg is offline Offline
Junior Poster in Training

Re: I need math formula for this

 
0
  #4
Jan 12th, 2009
(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
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 1,091
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 63
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

Re: I need math formula for this

 
1
  #5
Jan 12th, 2009
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
Plato forgot the nullahedron..
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 71
Reputation: Alibeg is an unknown quantity at this point 
Solved Threads: 10
Alibeg Alibeg is offline Offline
Junior Poster in Training

Re: I need math formula for this

 
0
  #6
Jan 12th, 2009
Originally Posted by MattEvans View Post
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Game Development
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC