RE: Rotating square

BlitzMax Forums/BlitzMax Beginners Area/RE: Rotating square

Joe90bigrat90(Posted 2011) [#1]
ok i have a square grid as a game board.
it has 25 squares on the tile.
so this one square is 5x5 squares wide.
Each of the 25 squares has a different picture on it.
Now ive created 4 images of the square. North, south east and west.
How would i go about doing the following.

I want to create a random number from 1-4 so it randomises teh big square in 1 of 4 directions (north south east and west).
As this big square has 25 little squares on it obviously as the big square rotates so do the 25 little squares.

I guess i need an array to store these but im not sure how to do it.

So i create a random number which puts the big square in a random direction from 1-4. Whatever direction the big square is facing the little 25 squares on the big square must also rotate with the big square if that makes sense.

Could someone show me what i need to do or give me an example of code would be great.
Many thanks
Kind Regards
Joe


Jesse(Posted 2011) [#2]
That is going to be quite a bit of math involved in doing that and if you don't have too much knowledge or experience in using trig functions you are going to have a hard time trying to figure it out. this next formula is going to be the foundation for all of your objects rotation.
this function rotates a point(x,y) around origin 0,0. the number of degrees is determined by stp
Function rotatePoint(x:float Var, y:float Var,stp:float)
	Local tx:Float = x*Cos(stp) - y*Sin(stp) 
	Local ty:Float = y*Cos(stp) + x*Sin(stp) 
	x = tx
	y = ty  
End Function

it doesn't rotate relative to 0 degrees. It rotates relative to it's current angle so keep that in mind.
you need to be creative when using it. So it can be used to rotate a square and it's children(the smaller squares), you need to do it in three steps:

1)you need to rotate the big square(the parent) stp amount. [parentAnge = parentAngle+stp]

2) each of the positions of each child square position need to be rotated a stp amount(by using the child's square handle relative to the handle of the parent square and using the function above) **

3)finally rotate each child stp amount. [childAngle = childAngle + stp]

based on that function, It doesn't matter what angle the object is in its current position, if you rotate a point 3 degrees, the point will be rotated 3 degrees from its current position relative to origin(0,0).

** if the child is to be rotated around it's parent position then the parent position has to be subtracted from the child then the child position is to be rotated and finally the parent position added again to the child position.

I don't know how easy it is for you to understand the above but good luck with it. :)

Last edited 2011


Joe90bigrat90(Posted 2011) [#3]
cool thankyou very much Jesse.
I will have a play around and see what happens.

Yeah its pretty complex isnt it. Hope i get the jist of it.

Cheers

Kind Regards

Joe

PS ill let you know if i have any problems!!