Pivot points

Previous topic - Next topic

AndyH

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

AndyH

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 !

bigsofty

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.
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)

Kitty Hello

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.

AndyH

Thanks Gernot - will check those out.

AndyH

Hi Gernot.  I don't seem to have qmath.gbas or any files that contain QSIN or QCOS.  Could you upload a copy please :)

Kitty Hello

Oh dear. In the next update, OK?

AndyH


Schranz0r

:D AndyH

Have you a demo from youre BMax wrapper?
I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

AndyH

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.

AndyH

It's here.  (except for the ROTOZOOMANIM which is out for the moment)