Author Topic: Rotate and translate (and scale) any group of GLBasic 2D commands  (Read 1494 times)

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2453
    • View Profile
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.
« Last Edit: 2016-May-02 by bigsofty »
Cheers,

Ian.

“It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration.”
(E. W. Dijkstra)

Offline erico

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 4041
    • View Profile
    • Portfolio
Re: Rotate and translate any GLBasic 2D command
« Reply #1 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:

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2453
    • View Profile
Re: Rotate and translate any GLBasic 2D command
« Reply #2 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.
Cheers,

Ian.

“It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration.”
(E. W. Dijkstra)

Offline Ian Price

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 4090
  • On the shoulders of giants.
    • View Profile
    • My Apps
Re: Rotate and translate any GLBasic 2D command
« Reply #3 on: 2015-Nov-16 »
Sounds cool. Nice one :)
I came. I saw. I played.

Offline erico

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 4041
    • View Profile
    • Portfolio
Re: Rotate and translate any GLBasic 2D command
« Reply #4 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!

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2453
    • View Profile
Re: Rotate and translate any GLBasic 2D command
« Reply #5 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).


« Last Edit: 2015-Nov-17 by bigsofty »
Cheers,

Ian.

“It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration.”
(E. W. Dijkstra)

Offline erico

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 4041
    • View Profile
    • Portfolio
Re: Rotate and translate any GLBasic 2D command
« Reply #6 on: 2015-Nov-17 »
Nice, it is just that I get a general idea about it, will play around with the code soon. :good:

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2453
    • View Profile
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.
« Last Edit: 2015-Nov-19 by bigsofty »
Cheers,

Ian.

“It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration.”
(E. W. Dijkstra)