0

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.

5
Contributors
5
Replies
7
Views
8 Years
Discussion Span
Last Post by Alibeg
0

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
:icon_smile:

0

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.

0

(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 :D)

1

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:

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.

Votes + Comments
Always good info in your posts.
0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.