123
-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|690|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
Socoder -> On Topic -> rotating tile map?

Wed, 18 Sep 2013, 11:47
spinal
can someone please help me get my head around the maths involved with rotating a tile map around a point?
say i have a small (or large, no real size) map, and i want to rotate it around a random point. the rotation of the indevidual tiles is taken care of by the programming language (monkey), but translating the coordinates is breaking my noodle. can someone tell me how to do it?

-=-=-
Check out my excellent homepage!
Wed, 18 Sep 2013, 12:19
Jayenkai
1. Find distance and angle of tile from point..
DistanceX= hereX-thereX
DistanceY= hereY-thereY
Distance=((distanceX*distanceX)+(distanceY*distanceY))
Angle=atan2(distanceX,-distanceY). ' or something, I can never remember which way around those are! One of them is negative for some reason)

2. Add rotation.
Angle=Angle+rotation

3. Get new positions
NewX=hereX+sin(Angle)*distance
NewY=hereY-cos(Angle)*distance


You might need to trial and error that a bit, but shouldn't be too hard.

-=-=-
''Load, Next List!''
Wed, 18 Sep 2013, 13:00
spinal
you know i remember doing this at school, i remember being good at it also. but it seems more than half my life later i cant remember a damn thing!

-=-=-
Check out my excellent homepage!
Wed, 18 Sep 2013, 13:20
Jayenkai
Yeah, I used to be better at maths, too. I'm even getting shit at the Countdown Numbers game, lately!!
We're all getting old

-=-=-
''Load, Next List!''
Wed, 18 Sep 2013, 14:08
shroom_monk
Gonna be a pernickety Maths student here and say that you're suppose to take 0 degrees as being due east, with increasing the angle taking you anticlockwise, so the equations should really be:
NewX = HereX + cos(Angle) * distance
NewY = HereY + sin(Angle) * distance
Angle = atan2(distanceY, distanceX)

|edit| Also distance is the square root of the sum of the squares, not just the sum of the squares! |edit|

That's why you have those weird negatives in your version. But either works perfectly, you just have to know where your 0 degrees (or radians!) is at.

I can never remember which way atan2 is, but I think it's usually Y then X, because it's a modified form of the equation atan(distanceY / distanceX), which doesn't give the full picture because of reasons.

-=-=-
A mushroom a day keeps the doctor away...

Keep It Simple, Shroom!
Wed, 18 Sep 2013, 14:17
shroom_monk
Also, here's a handy trick for rotating coordinates about the origin that lets you do all the maths in two lines (one for X, one for Y)!

The Maths Behind It
Matrices are a mathematical construct that let you perform various transformations. For instance, since PMC lets you apply matrix transformations straight to the canvas, I used a shear matrix to do the slanting effect on my text scroller demo a few weeks back.

There is also a matrix for rotation. The general form is:


If you matrix-multiply this with any vector, you can rotate that vector about the origin! Which leads us to...

Gimme da mathz!
Say I have a vector (x,y) in relation to the origin, and I want to rotate it by (a) degrees (or radians, or whatever units your trig functions are set to) about the origin.


-=-=-
A mushroom a day keeps the doctor away...

Keep It Simple, Shroom!
Wed, 18 Sep 2013, 14:28
Jayenkai
Aaah, that explains that, then! I've always done 0=up since drawing my first circles. Never occurred to me that it could be wrong!

Live and learn...

... I won't learn..!

-=-=-
''Load, Next List!''
Wed, 18 Sep 2013, 14:40
spinal
Oh how I would have loved for one of those answers to have worked!

Jays answer has the tiles all springing in and out of the center, Shrooms has them in completely the wrong shape all together

-=-=-
Check out my excellent homepage!
Wed, 18 Sep 2013, 14:42
shroom_monk
If people are interested, I could write up a couple of entries for the Articles section going through useful maths stuff for coding, more as a reference document than something to commit to memory. Some of it would probably be more useful to new coders than to those here, but it could be handy for future members getting started, perhaps?

-=-=-
A mushroom a day keeps the doctor away...

Keep It Simple, Shroom!
Wed, 18 Sep 2013, 14:53
Erebel55
Sure, I would be interested in giving that a look over shroom

-=-=-
Lava Monkey
https://play.google.com/store/apps/details?id=com.lvm.lavamonkey
Wed, 18 Sep 2013, 15:57
Jayenkai
Hurray for articles!!!!
Thu, 19 Sep 2013, 02:50
spinal
Ah, fixed it. it seems when I switched to shrooms code, I left a line of Jays in there which destroyed everything

Next... z rotating and scaling!!!! Woohoo!!

-=-=-
Check out my excellent homepage!
Thu, 19 Sep 2013, 05:10
Jayenkai
JayCode : Destroying Everything!!!
Sat, 21 Sep 2013, 11:28
spinal
ok, all hell has broken loose when i tried to add my player to the rotating map....




no doubt im doing a whole bunch of stuff wrong.... any clues?

-=-=-
Check out my excellent homepage!
Sat, 21 Sep 2013, 11:36
shroom_monk
It looks like your dx and dy calculations are back to front. Also, is it intentional that you're calculating Angle using dx and dy before you've actually assigned dx and dy?

-=-=-
A mushroom a day keeps the doctor away...

Keep It Simple, Shroom!
Sat, 21 Sep 2013, 12:15
shroom_monk
I should also add that, in the process of writing my most recent article, I realised I was being a bit misleading when I said we measure angles anti-clockwise. When plotting graphs in maths we do, but those graphs have the y-axis going upwards, rather than downwards as it does on a screen. So really, when programming, we take the angles to be clockwise, because our screen coordinates are upside-down! Sorry for the confusion.

-=-=-
A mushroom a day keeps the doctor away...

Keep It Simple, Shroom!