gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > Rotation and flipping

#26919 - babaloomer - Wed Sep 29, 2004 3:21 am

is there an easy way to do rotation/scaling AND flipping at the same time on one sprite?

#26920 - babaloomer - Wed Sep 29, 2004 3:47 am

yup there is... (it's a shame i didn't realized it earlier)

simply apply a negative scaling factor instead of playing with the horizontal and vertical flip bits...

there is an example of a rotating function here:
http://www.mdh165.pwp.blueyonder.co.uk/docstuts/Chapter7_SpriteFeaturesPt1.pdf

that was easy ;)

#26937 - pyros - Wed Sep 29, 2004 8:33 pm

Applying a negetive scaling factor will flip both horizontal AND vertical.

To flip horizontal or vertical, there are (at least) two ways of doing it:

The Easy Way:

Horizontal: negate pa, and pb (see below)
Vertical: negate pc and pd (also see below)
Both: negate pa,pb,pc and pd

The Maths Way:

pa,pb,pc,pd form a matrix (see the TONC pages)

To flip horizontal, vertical or both, simply multiply this matrix by the appropriate transformation matrix, and use the resulting values.
This (obviously) will produce the above result.

In a recent program I found it necessery to both flip and rotate sprites by multiples of 90degrees. Here's how to do each combination:


Code:

   a =  + scale * cos(angle);
   b =  + scale * sin(angle);
   c =  - scale * sin(angle);
   d =  + scale * cos(angle);


   pa = - a;   Flip Horizontal
   pb = - b;
   pc = + c;
   pd = + d;

   pa = + c;   Rotate Clockwise 90
   pb = + d;
   pc = - a;
   pd = - b;

   pa = - c;   Rotate Right 90
   pb = - d;   and Flip Vertical
   pc = - a;
   pd = - b;

   pa = - a;   Flip Horizontal
   pb = - b;   and Flip Vertical
   pc = - c;   (rotate 180)
   pd = - d;

   pa = + a;   Flip Vertical
   pb = + b;
   pc = - c;
   pd = - d;

   pa = - c;   Rotate Clockwise 270
   pb = - d;   (anti-clockwise 90)
   pc = + a;
   pd = + b;

   pa = + c;   Rotate Clockwise 270
   pb = + d;   and Flip Vertical
   pc = + a;
   pd = + b;

#30015 - dj-ceejay - Sat Nov 27, 2004 5:42 pm

The example on:

Quote:
there is an example of a rotating function here:
http://www.mdh165.pwp.blueyonder.co.uk/docstuts/Chapter7_SpriteFeaturesPt1.pdf


uses mode 1. I am using mode 4 for my game, does this matter?
Is it possible to scale and rotate in mode 4.

Thanks

Chris.
_________________
Fruit Machine Games:
http://www.fmsoftware.info/

#30017 - tepples - Sat Nov 27, 2004 5:51 pm

Rot/scale is the same in all modes that support it, except that modes 3, 4, and 5 do not support hardware wrapping of backgrounds.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.


Last edited by tepples on Sat Nov 27, 2004 10:58 pm; edited 1 time in total

#30020 - dj-ceejay - Sat Nov 27, 2004 6:34 pm

Thanks for your help.
I have fixed it now. I was using the sprite before calling RotateSprite() - The sprite is displayed, then it starts rotating. Therefore there was no data in attribute 2.

Thanks again.
_________________
Fruit Machine Games:
http://www.fmsoftware.info/