Codesnippets > 2D-snippets

Rotate and translate (and scale) any group of GLBasic 2D commands

(1/2) > >>

bigsofty:
This tiny routine sets a new origin, rotates and scales any thing between the two new commands...


--- Code: GLBasic ---        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 ---// --------------------------------- //
// 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 ---// --------------------------------- //
// 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.

erico:
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:

bigsofty:
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.

Ian Price:
Sounds cool. Nice one :)

erico:
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!

Navigation

[0] Message Index

[#] Next page

Go to full version