GLBasic forum

Main forum => GLBasic - en => Topic started by: AndyH on 2008-Feb-07

Title: Pivot points
Post by: AndyH on 2008-Feb-07
This is the code Gernot made in another post to use polyvector to rotate and scale.

Code (glbasic) Select
FUNCTION RotoZoomPoly: _spr, _id, _x,_y, _angle, _scale, _col
LOCAL sphi, cphi, spx, spy, dx, dy

    sphi=SIN(_angle)
    cphi=COS(_angle)
    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 _id
    POLYVECTOR _x- cphi*dx-sphi*dy, _y-cphi*dy+sphi*dx, 0,0, _col
    POLYVECTOR _x- cphi*dx+sphi*dy, _y+cphi*dy+sphi*dx, 0,spy, _col
    POLYVECTOR _x+ cphi*dx+sphi*dy, _y+cphi*dy-sphi*dx, spx,spy, _col
    POLYVECTOR _x+ cphi*dx-sphi*dy, _y-cphi*dy-sphi*dx, spx,0, _col
    ENDPOLY
ENDFUNCTION
It calculates the position of the four corners of the bounding rectangle for the angle and scale with the pivot or handle in the centre of the rectangle.  I'm having problems getting the maths right to allow me to specify a different pivot point for the rectangle to rotate around.  eg: the pivot point might be the top left of the rectangle rather than centre.

Can anyone help solve this before my head explodes :P
Title: Pivot points
Post by: AndyH on 2008-Feb-07
Think I have it now :P

Code (glbasic) Select
FUNCTION _oRotoZoomPoly: _spr, _id, _x,_y, _angle, _scale, _col
LOCAL sphi, cphi, spx, spy, dx, dy

    sphi=SIN(_angle)
    cphi=COS(_angle)
    spx = oSprites[_spr].w
    spy = oSprites[_spr].h
    dx=spx/2*_scale
    dy=spy/2*_scale
     
    LOCAL hx, hy
   
    hx = (spx/2-oSprites[_spr].hx)*_scale
    hy= (spy/2-oSprites[_spr].hy)*_scale

    STARTPOLY _id
    POLYVECTOR _x - cphi* (dx-hx) - sphi* (dy-hy),  _y - cphi*(dy-hy) + sphi*(dx-hx),   0,   0,   _col
    POLYVECTOR _x - cphi* (dx-hx) + sphi* (dy+hy),  _y + cphi*(dy+hy) + sphi*(dx-hx),   0,   spy, _col
    POLYVECTOR _x + cphi* (dx+hx) + sphi*(dy+hy),   _y + cphi*(dy+hy) - sphi*(dx+hx),   spx, spy, _col
    POLYVECTOR _x + cphi* (dx+hx) - sphi*(dy-hy),   _y - cphi*(dy-hy) - sphi*(dx+hx),   spx, 0,   _col
    ENDPOLY
ENDFUNCTION
Brain ache!  I'll see about optimising it in the morning, but feel free to make any suggestions !
Title: Pivot points
Post by: bigsofty on 2008-Feb-07
Looks good to me Andy, obviously look up tables for COS and SIN would be good to non-co-pro platforms but that's outside the function.
Title: Pivot points
Post by: Kitty Hello on 2008-Feb-07
Looks ace. You might have dx_hx and dx_plus_hx variables and multiplay these, that way you remove 2 multiplications (ok, peanuts).
Also, in Samples/COMMON there's a file qmath.gbas, which offers QSIN/QCOS commands that are much faster than the real ones.
Title: Pivot points
Post by: AndyH on 2008-Feb-07
Thanks Gernot - will check those out.
Title: Pivot points
Post by: AndyH on 2008-Feb-07
Hi Gernot.  I don't seem to have qmath.gbas or any files that contain QSIN or QCOS.  Could you upload a copy please :)
Title: Pivot points
Post by: Kitty Hello on 2008-Feb-08
Oh dear. In the next update, OK?
Title: Pivot points
Post by: AndyH on 2008-Feb-08
Thx. :D
Title: Pivot points
Post by: Schranz0r on 2008-Feb-08
:D AndyH

Have you a demo from youre BMax wrapper?
Title: Pivot points
Post by: AndyH on 2008-Feb-08
Yes, I will post it.  It's nothing amazing - but it's making my life easier and adding a few things I miss.  I think I need to put the sprite and point collision wrapper in first to round it off(unfortunately only supports collisions between the original sprite images and not the scaled/rotated version for now as I don't need this functionality yet - and it will be slow to do on PPC and GP2X).

I have the ROTOZOOMANIM in now but only for the 'tileset' type of sprite my object supports...I'll still be polling Gernot to fix the GRABSPRITE command to make this wrapper complete.
Title: Pivot points
Post by: AndyH on 2008-Feb-12
It's here (http://www.glbasic.com/forum/viewtopic.php?pid=10481#p10481).  (except for the ROTOZOOMANIM which is out for the moment)