Object Management?

Started by
26 comments, last by AgentPaper 12 years, 11 months ago

In 2D you have a single angle, a fixed rotation axis (so to say; in fact it is a fixed plane of rotation where the said axis is orthogonal to), and a free choosable origin (where the axis passes through, of course). Using 0 (i.e. the point where all co-ordinates are 0) as origin, the formulas for rotation look like
x' := x cos( a ) + y sin( a )
y' := -x sin( a ) + y cos( a )
If you write exactly this as matrix expression (using column vectors), you'll have
p' := R( a ) * p
so there is really nothing advanced at this level. However, as soon as you start to concatenate rotations, or (as in your case) want to compute rotational differences, matrix expression are much more handy than the scalar expressions using trigonometry terms. This gets more and more true when you consider other transformations, too, and when you work in spaces with more dimensions. I suggest you to look at matrix math (not in the broad sense but trimmed for the needs of CG) because after mastered the 3 or 4 hurdles geometric things gets much easier to deal with. Not to mention that there are several packages out there that already implement matrices and vectors for your convenience; you just have to learn how to use the stuff.

If you need help, please tell us how the both original rotations are defined, and how the difference shall look like to be used further.


See, most of that is still going way over my head. I've only touched on matrices very very briefly in school, and that was a long time ago anyways.

The way the rotations are defined is, they start at 0 pointing straight down, and then move counter-clockwise up to positive pi facing straight up. Then it changes to negative PI and goes back down to 0 at the bottom again. As far as I can tell, this is the standard way that rotation works in XNA. The co-ordinate system has the origin at the top left side of the screen, with positive X going to the right, and positive Y running straight down.

I'm never going to need to use this specific code for 3d, since this game is wholly 2d, so I don't think I need anything too fancy. Just a simple algorithm to get an angle from 2 points is what I need.
Advertisement

...
The way the rotations are defined is, they start at 0 pointing straight down, and then move counter-clockwise up to positive pi facing straight up. Then it changes to negative PI and goes back down to 0 at the bottom again. As far as I can tell, this is the standard way that rotation works in XNA. The co-ordinate system has the origin at the top left side of the screen, with positive X going to the right, and positive Y running straight down.

I'm never going to need to use this specific code for 3d, since this game is wholly 2d, so I don't think I need anything too fancy. Just a simple algorithm to get an angle from 2 points is what I need.

So the orientations are given as an origin and a distant point on the vector. Then you can use the atan2 function (see e.g. the illustration here on wikipedia and developer documentation of your favorite IDE). It is suitable to compute the angle of the vector with a horizontal reference line by relating the horizontal part of the vector with its vertical part. Doing so for both the original and the target point gives you 2 angles. Then compute the difference of the 2 angles and you have a measure for the rotation.

So the orientations are given as an origin and a distant point on the vector. Then you can use the atan2 function (see e.g. the illustration here on wikipedia and developer documentation of your favorite IDE). It is suitable to compute the angle of the vector with a horizontal reference line by relating the horizontal part of the vector with its vertical part. Doing so for both the original and the target point gives you 2 angles. Then compute the difference of the 2 angles and you have a measure for the rotation.


That...is exactly what I've been trying to do, and what I have in the example code I posted. The problem is, I can't get it to spit out the right number.

That...is exactly what I've been trying to do, and what I have in the example code I posted. The problem is, I can't get it to spit out the right number.

Nope. I see a code snippet using atan, but I don't see usage of atan2 ! (Or else I've missed it?)

How could atan( -2 / 3 ) be different from atan( 2 / -3), although the 1st is in quadrant IV and the 2nd is in quadrant II? You have to consider the sign of y and x values separately when relating the parts. That is (besides avoiding a division by zero) what atan2 does in contrast to atan.
I find http://www.helixsoft.nl/articles/circle/sincos.htm to be nicely explaining what cos and sin can do for motion tracking.

Hopefully it's not a secret duplicate of any of the answers. :P

[quote name='AgentPaper' timestamp='1306365375' post='4815828']
That...is exactly what I've been trying to do, and what I have in the example code I posted. The problem is, I can't get it to spit out the right number.

Nope. I see a code snippet using atan, but I don't see usage of atan2 ! (Or else I've missed it?)

How could atan( -2 / 3 ) be different from atan( 2 / -3), although the 1st is in quadrant IV and the 2nd is in quadrant II? You have to consider the sign of y and x values separately when relating the parts. That is (besides avoiding a division by zero) what atan2 does in contrast to atan.
[/quote]

Well, as far as I can tell Atan and Atan2 are the same thing, only with Atan you need to divide the two values yourself, and in Atan2 you just input 2 values and it takes care of that. I guess I could try using Atan2, though I don't know how it would help.

...
Well, as far as I can tell Atan and Atan2 are the same thing, only with Atan you need to divide the two values yourself, and in Atan2 you just input 2 values and it takes care of that. I guess I could try using Atan2, though I don't know how it would help.

Please read carefully! There is a definitive difference in these functions:

atan gets a single value. It is hence not able to distinguish between the 2 cases (x>0, y>0) and (x<0, y<0), and it cannot distinguish between the 2 cases (x>0, y<0) and (x<0, y>0), just because its argument is ever y/x>0 in the first 2 cases and y/x<0 in the second 2 cases! But you need to distinguish all 4 cases to be able to compute an angle in the full circle (e.g. 360°), or else you are restricted to a half circle (i.e. 180°). To be precise, you also have to investigate the cases where y==0 or x==0.

atan2, on the other hand, gets both x and y as separate values, and hence it is able to distinguish all 4 cases. (It further takes care on x==0 to avoid a division by zero.)

See this definition of the atan2 function on wikipedia.org.
Ah, very interesting, I'll have to try that out!

This topic is closed to new replies.

Advertisement