-
need help with rotation x,y math
Hi smart math people,
I need to figure out a new x and y coordinate for an active, as though I rotated its position by X digrees. Any idea how that's done?
For instance:
active object is at x=14, y=26... I want to rotate its position by 34 digrees.
thanks
-
Re: need help with rotation x,y math
The X and Y coordinates have nothing to do with the rotation. You can effect rotation as an action however.
-
Re: need help with rotation x,y math
I guess I'm not explaining clearly. I dont mean rotate the image. I mean figure out its new x and y coordinate as though it's orbiting coordinate zero,zero.
So, if its currently at x=14, y=26 and it has moved in its orbit by 24 digrees, whats it new x,y?
That makes sense?
-
Re: need help with rotation x,y math
you can find its new location by converting to polar coordinates, but unless you use floats instead of integers it will be very inaccurate.
For example:
X1 = "X of Origin"
Y1 = "Y of Origin"
X2 = "X of Object"
X2 = "Y of Object"
R = "Distance from Origin to Object" = sqr((X1 - X2) pow 2 + (Y1 - Y2) pow 2)
T; = "Angle from Origin to Object" = atan2(Y1-Y2,X2-X1)
E; = "Difference in Angle"
X3 = "X after Rotation" = cos(T + E) * R
Y3 = "Y after Rotation" = -sin(T + E) * R
so for example:
X1 = 0
Y1 = 0
X2 = 14
Y2 = 26
R = sqr((0 - 14) pow 2 + (0 - 26) pow 2) = 29.53
T; = atan2(0-26,14-0) = -61.70°
E; = 24°
X3 = cos(-61.70 + 24) * 29.53 = 23.36
Y3 = -sin(-61.70 + 24) * 29.53 = 18.06
-
Re: need help with rotation x,y math
WOW! Thanks much Piselthief. I'll report back after I give this a try!
I bow to your math-y-ness!!
-
Re: need help with rotation x,y math
Or derived from matrix math:
Xo = "X of Origin"
Yo = "Y of Origin"
X = "X of Object"
Y = "Y of Object"
E = "Difference in Angle"
NewX = Cos(E) * (X-Xo) + Sin(E) * (Y-Yo) + Xo
NewX = Cos(E) * (Y-Yo) - Sin(E) * (X-Xo) + Yo
EDIT: Same example as above:
Xo = 0
Yo = 0
X = 14
Y = 26
E = 24°
NewX = Cos(24) * (14) + Sin(24) * (26) = 12.79 + 10.58 = 23.36
NewX = Cos(24) * (26) - Sin(24) * (14) = 23.75 - 5.69 = 18.06
EDIT2: The matrix version is often used in professional code because you only need the sin and cos of the angle and some multiplies and adds, instead of sin, cos, atan2, pow2 and sqrt, making it much much faster. You can also combine matrixes together to get one matrix that will apply translation, rotation, and scale in one go, which can then be applied to all the points of an object (or many objects orbiting the same point, etc).
-
Re: need help with rotation x,y math
I enjoy this stuff and know it will get used (sometime). Thanks.
For future use here is Dynasoft's code as a snippet.
__________ file: X on arc.ini
[Details]
Description=The resultant X coordinate of an object in flight. Starting at Xo/Yo (ObjectXo) and arcing through an angle (E-degrees) with a circle center X/Y (XCenter).
[Function]
String=Cos(>E-degrees<)*(>ObjectXo<->XCenter<)+Sin(>E-degrees<)*(>ObjectYo<->YCenter<)+>XCenter<
Variable1=>E-degrees<
Variable2=>ObjectXo<
Variable3=>ObjectYo<
Variable4=>XCenter<
Variable5=>YCenter<
# # # # # #
__________ file: Y on arc.ini
[Details]
Description=The resultant Y coordinate of an object in flight. Starting at Xo/Yo (ObjectYo) and arcing through an angle (E-degrees) with a circle center X/Y (YCenter).
[Function]
String=Cos(>E-degrees<)*(>ObjectYo<->YCenter<)-Sin(>E-degrees<)*(>ObjectXo<->XCenter<)+>YCenter<
Variable1=>E-degrees<
Variable2=>ObjectXo<
Variable3=>ObjectYo<
Variable4=>XCenter<
Variable5=>YCenter<
Have I got something wrong?
Here is a test file using the snippet.
http://www.buddhamind.info/mmf/AK-arc_test.mfa
I just presume something is wrong... somewhere.
I will find time later to look at PixelThief's code
-
Re: need help with rotation x,y math
Took me a moment...
You're using the *new* X coordinate in the line where you figure out the new Y, it should be the old X.
-
Re: need help with rotation x,y math
thinks im going to have to start learning some math for all this stuff I need to do lol...
Anyone got any sagestions where to start?
-
Re: need help with rotation x,y math
I learned matrix maths in a computer games programming university course. Though GCSE and A-Level maths helped too.
-
Re: need help with rotation x,y math
-- Dynasoft
Well spotted indeed. I thought after about doing an edit to say "Hey, did you realise that both lines are NewX = ..."
Having said that I will still need a translation of
Quote:
You're using the *new* X coordinate in the line where you figure out the new Y, it should be the old X.
your orig post....
NewX = Cos(E) * (X-Xo) + Sin(E) * (Y-Yo) + Xo
NewX = Cos(E) * (Y-Yo) - Sin(E) * (X-Xo) + Yo
The other thing I wondered about was bracketing.
Here is a sample of how I thought brackets should be added to be sure the calcs are done in order - I have added {}
NewX = {Cos(E) * (X-Xo)} + {Sin(E) * {(Y-Yo) + Xo}}
Light and movement are what, for me, makes a screen interesting. Poor play can never really be compensated for but giving time to visuals is worth the effort.
Thanks for following this.
-
Re: need help with rotation x,y math
Quote:
Originally Posted by arfa
Well spotted indeed. I thought after about doing an edit to say "Hey, did you realise that both lines are NewX = ..."
Quote:
Originally Posted by Dynasoft
NewX = Cos(E) * (X-Xo) + Sin(E) * (Y-Yo) + Xo
NewX = Cos(E) * (Y-Yo) - Sin(E) * (X-Xo) + Yo
Whoops, that should indeed be NewY for the 2nd one:
NewX = Cos(E) * (X-Xo) + Sin(E) * (Y-Yo) + Xo
NewY = Cos(E) * (Y-Yo) - Sin(E) * (X-Xo) + Yo
Quote:
Originally Posted by arfa
The other thing I wondered about was bracketing.
Here is a sample of how I thought brackets should be added to be sure the calcs are done in order - I have added {}
NewX = {Cos(E) * (X-Xo)} + {Sin(E) * {(Y-Yo) + Xo}}
The bracketing is:
NewX = { {Cos(E) * (X-Xo)} + {Sin(E) * (Y-Yo)} } + Xo
NewY = { {Cos(E) * (Y-Yo)} - {Sin(E) * (X-Xo)} } + Yo
(You shouldn't need the extra {} though, MMF will get the order right)
Quote:
Originally Posted by arfa
Having said that I will still need a translation of
Quote:
You're using the *new* X coordinate in the line where you figure out the new Y, it should be the old X.
You are setting the X position to the first calculation, then retrieving that new X position for the Y calculation, so you end up with this:
NewX = Cos(E) * (X-Xo) + Sin(E) * (Y-Yo) + Xo
NewY = Cos(E) * (Y-Yo) - Sin(E) * (NewX-Xo) + Yo
You need to either store the old X and Y and then do both calculations using the stored values, or do the calculations into stored values and then set position.
-
Re: need help with rotation x,y math
Ahhhh, the tangled tumour that is literature of more than one brain. If you follow my converse perversity juxtaposed against the corollary of my second cat's first litter.
> > s cattered.
Quote:
NewX = Cos(E) * (X-Xo) + Sin(E) * (Y-Yo) + Xo
NewY = Cos(E) * (Y-Yo) - Sin(E) * (NewX-Xo) + Yo
This makes it quote clare.
Thanks for the quick reply. I will see about putting this into a working example.
-
Re: need help with rotation x,y math
I wonder if you are still following this HolyMonkey?
I wonder if your original question was to actually position the object - as opposed to predicting it XY *before* rotation? I would be interested to know your game scenario if it is the predictive type.
Assuming it is not - and, for me I imaging most games as "move, jump, collide, etc and see what happens" - then here are a couple of examples using a simple circumference formula that I found while trying to get my head around the predictive math.
http://www.buddhamind.info/mmf/AK_arc_test-2.mfa
@Dynasoft - ouch.! I can't get this sucker to work for me
@Pixelthief - MMF doesn't allow -Sin
@both... I am still interested to follow this. I wonder if you had the time to put a working model together?
thanks - ak
-
Re: need help with rotation x,y math
Yes, I'm still following this, but its not for a game... its just to be able to create one "thing" from several images near each other, then cohesively rotate the entire thing without it getting "ripped apart" Dynasoft's first equations worked perfectly for me. I figured that the second one was meant to read "new Y". :)
-
Re: need help with rotation x,y math
When you want to type
-Sin(Alterable Value A("Object"))
Instead just type:
0-Sin(Alterable Value A("Object"))
:S
-
Re: need help with rotation x,y math
Yeah, MMF doesn't support unary minus, except for negative numbers.
-
Re: need help with rotation x,y math
yeah I keep forgetting to say something
Hey, clickteam. Add unary minus symbols to your parser! They're not *all* binary operators. Its almost 2011!
-
Re: need help with rotation x,y math
>> Dynasoft's first equations worked perfectly for me
Could you post a simple example of that?
-
Re: need help with rotation x,y math
I will as soon as I get the time to make one...which might not be until some time this weekend...
-
Re: need help with rotation x,y math
I won't have net access for a few days so no hurry.
thanks
-
Re: need help with rotation x,y math
Quote:
Originally Posted by HolyMonkey
I will as soon as I get the time to make one...which might not be until some time this weekend...
Please do. I won't be near MMF all weekend, so I can't help arfa...
-
Re: need help with rotation x,y math
Hi all.
Here's the best I can come up with with my limited free time:
Example MFA
I'm assuming due to a rounding thing, they slowly move toward the center active. This rounding issue doesnt bother me for my needs(not a game, and not for a continuous orbit, just for a one shot calculation)... If I needed to maintain this "orbit" in a game I would just kave stored the original x,y offests in each "petal" and reset the new x,y to those numbers aver avery complet revolution...as in, once each petal is back to its starting angle.
-
Re: need help with rotation x,y math
Actually, I now have a need to correct this rounding issue but can't use the method I was thinking of... Can one of you mathy guys help me fix the issue where they gravitate toward the center sprite?
thanks
-
Re: need help with rotation x,y math
If you store the object's initial coordinates and always rotate from those it will work.
Alternatively if you always work with alterable values instead of retrieving the position, you'll get more precision and it should stop the rounding errors being noticable.
-
Re: need help with rotation x,y math
thanks HM.
I just got back and will study the set up later.
While I was away I tweaked my earlier example to add objects in a circle. Maybe you need unique objects. I think this would still work for that. It would be easy enough to set my center with 'bouncing ball' or you could do a 'random direction math thing'.
http://www.buddhamind.info/mmf/AK_arc_test-3a.mfa
Have a play with the various player-input keys.
There are a few things still in the back of my mind to add to this but no time just now.
I too had problems with the rounding thing. [line 16 - set angle for MUM] It meant that with a lot of obj. - 25+ there was a gap in the ring. I will start a thread and see if there is a (better way than mine) for that.
-
Re: need help with rotation x,y math
In case you didn't spot this on rounding decimals...
http://www.clickteam.com/epicenter/ubbthreads.php?ubb=showflat&Number=214817&#Post214 817
-
Re: need help with rotation x,y math
-
Re: need help with rotation x,y math
HolyMonkey,
I still haven't made time to study your example. I have been working on the widget version of my approach.
Here is a version of your scenario with the math I have been using. This is a fixed value setup and you can compare it to my earlier example with full variables. I have to say that it looks *way* easier than your example and this inclines me to continue with this math approach.
I hope this helps.
http://www.buddhamind.info/mmf/AK_4obj-Rotate.mfa
-
Re: need help with rotation x,y math
arfa, I have been looking into this as well. Your example works well, but it looks a little less smooth. Is there a way to make it look a little less jerky?
-
Re: need help with rotation x,y math
Experiment with changing the angle increment [smaller is smoother] and the time factor [ditto] set to add it.
Keep in mind that x/y is being calculated on the fly and the more frequently this is done the more resources required. I couldn't say exactly how much but it can add up when set alongside the rest of your project.
-
Re: need help with rotation x,y math
Well, my use for it would be used for something similar to an interactive bar that can rotate objects for a certain angle.
For instance, I want all objects to rotate around the object to 30 degrees. After that, it would stop. I do hope, however, that this will not consume too many resources. A project I have in mind (haven't really started it yet) is going to play with a ton of things....I just hope I'm up for it. XD
-
Re: need help with rotation x,y math
Ausomeman, just change the timed angle-increment line to a user input.
Here is my final rendering. I have learnt a lot doing this and can implement some of it directly in a current work.
http://www.buddhamind.info/mmf/AK_arc_test-4.mfa