| | |
I need math formula for this
Please support our Game Development advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2007
Posts: 81
Reputation:
Solved Threads: 1
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.
•
•
Join Date: Jan 2009
Posts: 79
Reputation:
Solved Threads: 12
•
•
•
•
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.
theta = arctan(y/x)
theta is your degrees so, theta = arctan(10/10) = 45 degrees
•
•
Join Date: Aug 2008
Posts: 71
Reputation:
Solved Threads: 10
(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
)
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
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:
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.
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:
C Syntax (Toggle Plain Text)
double obj1x, obj1y, obj2x, obj2y; /* positions of objects 1 & 2 */ double obj1rot; /* rotation of object1*/ double relx = obj2x - obj1x; double rely = obj2y - obj1y; /* length of relative position (i.e. distance) */ double reldis = sqrt ( ( relx * relx ) + ( rely * rely ) ); /* avoid division by zero... */ if ( reldis > EPSILON ) { /* EPSILON should be a very small number */ /* normalized relative position */ double n_relx = relx / reldis; double n_rely = rely / reldis; double theta = atan2 ( n_rely, n_relx ); double delta = theta - obj1rot; obj1rot += delta; /* a bit contrived at the end, you can do what you want with delta here. */ } else { /* objects are in the same place, degenerate result. */ }
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..
•
•
Join Date: Aug 2008
Posts: 71
Reputation:
Solved Threads: 10
•
•
•
•
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.
i made a complication with two lines. XD
![]() |
Similar Threads
- Cone Formula (Java)
- Array and Switch issues (C++)
- not enough system resources to display completely (Windows Software)
- HELP! Illegal Character Constant in a Console Application (C++)
- making a matrix (C)
- Graphics In Pixel: Part II (C++)
- sum of series (C++)
Other Threads in the Game Development Forum
- Previous Thread: First openGL Triangle, help
- Next Thread: need help with lighting in openGL.
| Thread Tools | Search this Thread |
Tag cloud for Game Development
3d advertising ai algorithm ban c++ cambridge camera censorship china competition console development engine fov fpx game gamedevelopment gameprogramming gamer games gaming gauntanamo government graphics idaho in-gameadvertisement intel intellectualproperty l-systems laracroft larrabee lindenmayer live manhunt math mathematics matrix mercenaries microsoft mmorpg modded msn multicore naked news nintendo obama opengl palin physics pirate playstation politics processor projection ps3 rpg search selection software sony stephenhawking stocks studio technology terrorism tombraider uk videogame web wii world-of-warcraft xbox xbox-live xbox360






