GLBasic forum

Codesnippets => 2D-snippets => Topic started by: bigsofty on 2015-Nov-16

Title: Rotate and translate (and scale) any group of GLBasic 2D commands
Post by: bigsofty on 2015-Nov-16
This tiny routine sets a new origin, rotates and scales any thing between the two new commands...

Code: (glbasic) [Select]
Transform2DON( rotateAngle#, translateX#, translateY#, scaleX#, scaleY# ) // Angle, new origin X and new origin Y coords

... Normal 2D code goes here

Transform2DOFF()


file 1, quick demo...

Code: (glbasic) [Select]
// --------------------------------- //
// Project: Transform2D
// Start: Monday, November 16, 2015
// IDE Version: 12.312


// SETCURRENTDIR("Media") // go to media files

GLOBAL angle#
WHILE 1
X_MAKE2D
angle# = FMOD( angle# + 0.5, 360 )

Transform2DON( MOD( -angle#, 360 ) , 320, 240 )
FOR x = 0 TO 9
FOR y = 0 TO 9
DRAWRECT -400 + x*80, -400 + y*80, 80, 80, RGB(y*25.6, x*25.6, SIN(angle/2)*255 )
NEXT
NEXT
Transform2DOFF()

Transform2DON( angle# * 2 , 320 + COS(angle*2)*200, 240 + SIN(angle)*200, SIN(angle)*4, SIN(angle)*4 )
DRAWRECT -120, -50, 240, 100, RGB(SIN(angle/2)*255, 55, 0)
PRINT "ROTATE + TRANSLATE + SCALE", -100 ,0
Transform2DOFF()

SHOWSCREEN
WEND

file 2, the actual lib to rotate, translate and scale the 2D stuff...

Code: (glbasic) [Select]
// --------------------------------- //
// Project: Transform2D
// Start: Monday, November 16, 2015
// IDE Version: 12.312

INLINE

    };
    extern "C" void __stdcall glPushMatrix();
    extern "C" void __stdcall glPopMatrix();
    extern "C" void __stdcall glTranslatef( float, float, float );
    extern "C" void __stdcall glScalef( float, float, float );
    extern "C" void __stdcall glRotatef( float, float, float, float );
    namespace __GLBASIC__ {

ENDINLINE

FUNCTION Transform2DON: rotateAngle#, translateX#, translateY#, scaleX# = 1.0, scaleY# = 1.0
INLINE
   glPushMatrix();
   glTranslatef(translateX, translateY, 0.0);
   glScalef(scaleX, scaleY, 1);
   glRotatef( rotateAngle, 0, 0, 1.0);
ENDINLINE
ENDFUNCTION

FUNCTION Transform2DOFF:
INLINE
   glPopMatrix();
ENDINLINE
ENDFUNCTION

// Same stuff, just a little more flexible

FUNCTION Push2DMatrix: // Transformation On (Preserve current transformation matrix)
INLINE
   glPushMatrix();
ENDINLINE
ENDFUNCTION

FUNCTION Pop2DMatrix: // Transformation Off (restore the previous transformation matrix)
INLINE
   glPopMatrix();
ENDINLINE
ENDFUNCTION

FUNCTION Translate2D: translateX#, translateY# // Translate 2D
INLINE
   glTranslatef(translateX, translateY, 0.0);
ENDINLINE
ENDFUNCTION

FUNCTION Rotate2D: rotateAngle# // Rotate 2D
INLINE
   glRotatef( rotateAngle, 0, 0, 1.0);
ENDINLINE
ENDFUNCTION

FUNCTION Scale2D: scaleX#, scaleY# // Scale 2D
INLINE
   glScalef(scaleX, scaleY, 1);
ENDINLINE
ENDFUNCTION


It should be very fast are all the work is being done on the GPU.

Edit: Small bug in translate fixed, using these 2D function as a matrix stack should work now.
Title: Re: Rotate and translate any GLBasic 2D command
Post by: erico on 2015-Nov-16
Oh my! This is interesting, I have always feared rotations, things like sin and cos, and thus, the games I makes have to conform without it.
Thanks a lot Big, I´m sure going to examine it carefully. :good:
Title: Re: Rotate and translate any GLBasic 2D command
Post by: bigsofty on 2015-Nov-16
No need for SIN or COS(even though there are a few in the demo bit) Erico ;)

In theory you could rotate the whole screen with this but I'm not sure there is a need for that but it would be handy if you wanted to rotate a tilemap for example. Not heavily tested though as I just banged it out tonight with the small demo.
Title: Re: Rotate and translate any GLBasic 2D command
Post by: Ian Price on 2015-Nov-16
Sounds cool. Nice one :)
Title: Re: Rotate and translate any GLBasic 2D command
Post by: erico on 2015-Nov-17
But if one wants to make a shooter where the ship stays centered and stuff rotates, this could work couldn´t it?
I wonder about the collisions, thanks again!
Title: Re: Rotate and translate any GLBasic 2D command
Post by: bigsofty on 2015-Nov-17
Yes that should be fine. Collisions should still work without. Just visualise that the ship is rotating and the rest of the level is not. So no need to rotate X & Y. I'd personally go with some sort of circle to circle collision, for the player to baddies but AABB should still work too but dont expect pixel accuracy(AABB around a rotated sprite will no be great).


Title: Re: Rotate and translate any GLBasic 2D command
Post by: erico on 2015-Nov-17
Nice, it is just that I get a general idea about it, will play around with the code soon. :good:
Title: Re: Rotate and translate (and Scale) any GLBasic 2D command
Post by: bigsofty on 2015-Nov-19
Updated, added scaling too.

See first post.

P.S. I think to have full flexibility, the best thing to do would be to make the Scaling, Translation and Rotation different commands, this would allow them to be called in a different order, making things more flexible. So I added these extra commands too, drop me a line if you are having trouble with them.