GLBasic forum

Codesnippets => Code Snippets => Topic started by: Kitty Hello on 2008-Jan-22

Title: RotoSprite um einen Pin-Punkt auf dem Sprite
Post by: Kitty Hello on 2008-Jan-22
Code (glbasic) Select
DRAWRECT 0,0,64,32,RGB(255,255,0)
DRAWRECT 7,23,4,4,RGB(255,0,0)

GRABSPRITE 0, 0,0,64,32



WHILE TRUE
MOUSESTATE mx,my,b1,b2
INC phi, 1
RotoPivot(0, mx, my, 9,25, phi)
DRAWRECT mx-1,my-1, 2,2, RGB(0,255,0)
SHOWSCREEN
WEND


//! RotoPivot
// \param id - Sprite ID
// \param x,y  - x,y position of pivor point to draw on screen
// \param px,py- x,y position of pivot point on sprite (0,0=topleft corner)
// \param phi  - rotation angle as in ROTOSPRITE
FUNCTION RotoPivot: id, x,y, px, py, phi
LOCAL sx, sy, rx,ry
LOCAL cosp, sinp
cosp=COS(phi)
sinp=SIN(phi)


GETSPRITESIZE id, sx, sy

sx=sx/2
sy=sy/2

// reverse rotate pivot point arount center
// of sprite
DEC px, sx
DEC py, sy
rx = px*cosp + py*sinp
ry = py*cosp - px*sinp

// adjust center
INC rx,sx
INC ry,sy
// perform drawing
ROTOSPRITE id, x-rx, y-ry, phi
ENDFUNCTION
Title: RotoSprite um einen Pin-Punkt auf dem Sprite
Post by: Schranz0r on 2008-Jan-22
coole sache das!

Hast du noch zufällig das "Rotation Polyvector"-Teil da, das du mir mal geschickt hast ?(Die Funktion)
Title: RotoSprite um einen Pin-Punkt auf dem Sprite
Post by: Kitty Hello on 2008-Jan-22
Auszug aus einer Funktion in meinem "KidsFlash" Editor...
Code (glbasic) Select
GETSPRITESIZE sprid, spx, spy
scale = rpix / (SQR(spx*spx+spy*spy)/2) * .5
dx=spx*scale
dy=spy*scale

IF obj.hide
sprid = 1102 // ghost
GETSPRITESIZE sprid, spx, spy
ENDIF

IF obj.flip=0
spx0 = spx
spx=0
ELSE
spx0=0
ENDIF

STARTPOLY sprid
POLYVECTOR x+ cphi*dx-sphi*dy, y+cphi*dy+sphi*dx, spx,0,c
POLYVECTOR x+ cphi*dx+sphi*dy, y-cphi*dy+sphi*dx, spx,spy,c
POLYVECTOR x- cphi*dx+sphi*dy, y-cphi*dy-sphi*dx, spx0,spy,c
POLYVECTOR x- cphi*dx-sphi*dy, y+cphi*dy-sphi*dx, spx0,0,c
ENDPOLY
Title: RotoSprite um einen Pin-Punkt auf dem Sprite
Post by: Schranz0r on 2008-Jan-22
thx
Title: RotoSprite um einen Pin-Punkt auf dem Sprite
Post by: Schranz0r on 2008-Jan-23
hmm bekomms nicht hin :/
Title: RotoSprite um einen Pin-Punkt auf dem Sprite
Post by: Schranz0r on 2008-Feb-03
help ;)
Title: RotoSprite um einen Pin-Punkt auf dem Sprite
Post by: Kitty Hello on 2008-Feb-04
Code (glbasic) Select
DRAWRECT 0,0,64,32,RGB(255,255,0)
DRAWRECT 7,23,4,4,RGB(255,0,0)

GRABSPRITE 0, 0,0,64,32



WHILE TRUE
    MOUSESTATE mx,my,b1,b2
    INC phi, 1
    RotoZoomPivot(0, mx, 200, 9,25, phi, my/100)
    DRAWRECT mx-1,200-1, 2,2, RGB(0,255,0)
    SHOWSCREEN
WEND


//! RotoZoomPivot
// \param id - Sprite ID
// \param x,y  - x,y position of pivor point to draw on screen
// \param px,py- x,y position of pivot point on sprite (0,0=topleft corner)
// \param phi  - rotation angle as in ROTOSPRITE
// \param scale- scale factor for rotated sprite
FUNCTION RotoZoomPivot: id, x,y, px, py, phi, scale
LOCAL sx, sy, rx,ry
LOCAL cosp, sinp
    cosp=COS(phi)*scale
    sinp=SIN(phi)*scale
   
   
    GETSPRITESIZE id, sx, sy
   
    sx=sx/2
    sy=sy/2

    // reverse rotate pivot point arount center
    // of sprite
    DEC px, sx
    DEC py, sy
    rx = px*cosp + py*sinp
    ry = py*cosp - px*sinp
   
    // adjust center
    INC rx,sx
    INC ry,sy
    // perform drawing
    ROTOZOOMSPRITE id, x-rx, y-ry, phi, scale
ENDFUNCTION
Title: RotoSprite um einen Pin-Punkt auf dem Sprite
Post by: Kitty Hello on 2008-Feb-04
...was wolltest Du?
Title: RotoSprite um einen Pin-Punkt auf dem Sprite
Post by: Schranz0r on 2008-Feb-04
Einen RotoZoom auf den Mittelpunkt bezogen...

Müsste aber doch jetzt mit deiner Funktion auch gehn ^^

Muss ja nurnoch GETSPRITESIZE und damit dann die px und py füttern


EDIT: Ne ist ja kein Polyvector dabei -.-

Wollte es ja per Poly haben ...

Wollte nochmal ne bessere ParticleEngine machen in nächster Zeit
Title: RotoSprite um einen Pin-Punkt auf dem Sprite
Post by: Kitty Hello on 2008-Feb-04
aso. Rotozoom ist aber schneller als polyvector.
auf die Mitte:

getspritesize id, sx, sy
rotozoomsprite id, x-sx/2, y-sy/2, phi, scale
Title: RotoSprite um einen Pin-Punkt auf dem Sprite
Post by: Schranz0r on 2008-Feb-04
Na brauch das doch für Particle um sie Farbig zu machen :/
Title: RotoSprite um einen Pin-Punkt auf dem Sprite
Post by: Kitty Hello on 2008-Feb-05
Code (glbasic) Select
FUNCTION RotoZoom: sprid, x,y, phi, scale
LOCAL sphi, cphi, sp, spy, dx, dy
LOCAL c=RGB(255,255,255)
sphi=SIN(phi)
cphi=COS(phi)
GETSPRITESIZE sprid, spx, spy
dx=spx*scale
dy=spy*scale


// make it act like ROTOZOOMSPRITE
// if you remove this line, x+y are
// the center of the rotation
INC x, spx/2; INC y, spy/2

STARTPOLY sprid
POLYVECTOR x- cphi*dx-sphi*dy, y-cphi*dy+sphi*dx, 0,0,c
POLYVECTOR x- cphi*dx+sphi*dy, y+cphi*dy+sphi*dx, 0,spy,c
POLYVECTOR x+ cphi*dx+sphi*dy, y+cphi*dy-sphi*dx, spx,spy,c
POLYVECTOR x+ cphi*dx-sphi*dy, y-cphi*dy-sphi*dx, spx,0,c
ENDPOLY
ENDFUNCTION
Biddaschööön :D
Title: RotoSprite um einen Pin-Punkt auf dem Sprite
Post by: Schranz0r on 2008-Feb-05
:nw:
Title: RotoSprite um einen Pin-Punkt auf dem Sprite
Post by: AndyH on 2008-Feb-06
Oooo....thanks Gernot.

That was the last function I had to write for my BMax wrapper (ie: the colour part for the POLYVECTOR) so you've saved me a headache - and it works a treat. :)

I now can do:


spr3 = oLoadAnim("ovine.png", 32,32, 10, FALSE)
oSetHandle (spr3, 16,28 )
oSetColor ( RGB(255,0,255) )
oSetScale (1.5)
oSetRotation (45)
oDrawSprite ( spr3, 17, 120, 0 )
oSetRotation (90)
oDrawSprite ( spr3, 40, 120, 0 )
oSetColor ( RGB(255,255,255))

My draw sprite routine will pick the quickest method for drawing the sprite based upon the current scale, rotation and color values.  More important for the PPC and GP2X.
Title: RotoSprite um einen Pin-Punkt auf dem Sprite
Post by: Schranz0r on 2008-Feb-06
A BMax Wrapper?
Some examples ?
Title: RotoSprite um einen Pin-Punkt auf dem Sprite
Post by: AndyH on 2008-Feb-06
It's basically a sprite manager, with BMax-like commands to Set the pivot handle, rotation, scale and (now) colour for drawing sprites.  It lets you load in sprites or anim strips, although anim strips are GRABSPRITE'd into single sprites so I can use the ROTOZOOM and POLYVECTOR commands - but that part is hidden from you and you can still refer to the object Id's returned with a frame number.

I need Gernot to look at the GRABSPRITE issue though as sprites grabbed do not behave the same when scaled or rotated as sprites loaded with LOADSPRITE directly, even though the pixels are identical in both (with no alpha channel data in both).

I need to expand it to also handle text, and I'm keeping support in for ANIM's mostly for use in tilemaps for level drawing (these do not generally need the SETCOLOR, SETROTATE, SETZOOM support).  I may be able to expand it further to support the new ROTOZOOM command for ANIM's once that's ready, but I'm still going to need to do the GRABSPRITE method if I want to continue to support the SETCOLOR via Polyvector.  I wonder if Gernot can add a parameter for colour to the ANIM version of ROTOZOOM as he said that he'd be using POLYVECTOR's to make it work?

The DrawSprite function then decides which method to use to draw the sprite frame.

I can post code if interested, but it's not finished yet (the above to do and it's not optimised yet).
Title: RotoSprite um einen Pin-Punkt auf dem Sprite
Post by: Kitty Hello on 2008-Feb-06
rotozoomanim is in now, K?
Title: RotoSprite um einen Pin-Punkt auf dem Sprite
Post by: AndyH on 2008-Feb-06
Cool - thx!
Title: RotoSprite um einen Pin-Punkt auf dem Sprite
Post by: AndyH on 2008-Feb-12
Here's a version of the functions I was talking about (http://www.glbasic.com/forum/viewtopic.php?pid=10481#p10481).