Rotate a 2D sprite?

Previous topic - Next topic

ampos

I want to rotate a 2D sprite, but I dont get it...

Code (glbasic) Select
ROTOSPRITE id,x1,y1,angle

But I want to do it with sprites...

Code (glbasic) Select
X2=X1+W
Y2=Y1+H

STARTPOLY id
   POLYVECTOR x1,y1,0,0
   POLYVECTOR x1,y2,0,h
   POLYVECTOR x2,y2,w,h
   POLYVECTOR x2,y1,w,0
ENDPOLY


In fact, I want to create my custom ROTOZOOMANIM but with polyvector

I have resolved the ZOOM & ANIM parts, but ROTO part is...  :rant:
check my web and/or my blog :D
http://diniplay.blogspot.com (devblog)
http://www.ampostata.org
http://ampostata.blogspot.com
I own PC-Win, MacBook 13", iPhone 3G/3GS/4G and iPAC-WinCE

Qube

You could use my code fromHERE which does rotation. It will probably be best to split things up a little to make them more generic, like using sprite sheets with the routines.



mentalthink

Whit cos and sinus not ?¿,
only you have take a littke care whit the argument in both parts,  (360/90), I don´t rebember well but I do it this same a lot of time ago, the trick it´s think in circles in any vertex of polysprite... it´s a bit of maths, but I don´t have any idea, and finish succesfully, I remember this, the 360 degrees divide en 4 parts, this foru parts are the 4 vertex of polysprite, sorry, but I don´t remenber the code... :giveup:

Wampus

I have code that mimics all the GLBasic sprite commands for POLYVECTORS instead. If you want it I'll post it.

bigtunacan

@Wampus,

I certainly wouldn't mind seeing that.  I've implemented my own version of zooming, mirroring, and animating with polyvectors, but haven't gotten around to implementing rotation yet either.  My current game doesn't need rotation; and if you already have the code to save us the trouble :)

chve

#5
I use this so for my purpose in a program which I am writing actually:


Code (glbasic) Select



LOADSPRITE "mage.bmp",1

// Rotacion de la carta
GLOBAL giro,angle


// Arrastar la carta
GLOBAL drag
GLOBAL wide
GLOBAL xcard,ycard,widecard,heigthcard
xcard=230  ;ycard=100
GETSPRITESIZE 1, widecard, heigthcard


zoneclick=0



//--------------------------- girar una carta o sprite
IF KEY(34)=1 THEN giro=TRUE
IF KEY(35)=1 THEN giro=FALSE
IF giro = FALSE THEN DRAWSPRITE 1,xcard,ycard

// GRABSPRITE 2,xcard,ycard,widecard,heigthcard
IF giro=TRUE
  IF b_1=1 THEN DEC angle,1
  IF b_2=1 THEN INC angle,1
  SMOOTHSHADING FALSE 
  ROTOSPRITE 1, xcard, ycard, angle
  // GRABSPRITE 2,xcard,ycard,widecard,heigthcard
ENDIF

SHOWSCREEN
WEND
SHOWSCREEN
GOTO main



ampos

Quote from: Qube on 2011-Dec-08
You could use my code fromHERE which does rotation. It will probably be best to split things up a little to make them more generic, like using sprite sheets with the routines.

Thank you, it helped me!

Anyway, your routine does a scale up 2x on sprites and it is not centered, so I have to add

h=h/2
w=w/2
INC x,w
INC y,h

check my web and/or my blog :D
http://diniplay.blogspot.com (devblog)
http://www.ampostata.org
http://ampostata.blogspot.com
I own PC-Win, MacBook 13", iPhone 3G/3GS/4G and iPAC-WinCE

Wampus

Quote from: bigtunacan on 2011-Dec-08
@Wampus,

I certainly wouldn't mind seeing that.  I've implemented my own version of zooming, mirroring, and animating with polyvectors, but haven't gotten around to implementing rotation yet either.  My current game doesn't need rotation; and if you already have the code to save us the trouble :)

Sure thing. The code below will do the equivalent of ROTOZOOMANIM with a polyvector set to triangles:-

Code (glbasic) Select
FUNCTION PolyRotoZoomAnim: sprite, frame, xpos, ypos, angle, scale


angle = 359 - angle


patx = sprites[sprite][frame][0]
paty = sprites[sprite][frame][1]
patxx = sprites[sprite][frame][0]+sprites[sprite][frame][2]
patyy = sprites[sprite][frame][1]+sprites[sprite][frame][3]


porwidth = sprites[sprite][frame][2]
porheight = sprites[sprite][frame][3]
pwidth = sprites[sprite][frame][2] * scale
pheight = sprites[sprite][frame][3] * scale


porcentx = porwidth / 2
porcenty = porheight / 2
pcentx = pwidth / 2
pcenty = pheight / 2


pminx = -porcentx
pminy = -porcenty
pmaxx = porcentx
pmaxy = porcenty


pltx = ((COS[angle]*pminx) - (SIN[angle]*pminy)) * scale
plty = ((SIN[angle]*pminx) + (COS[angle]*pminy)) * scale

plbx = ((COS[angle]*pminx) - (SIN[angle]*pmaxy)) * scale
plby = ((SIN[angle]*pminx) + (COS[angle]*pmaxy)) * scale

prtx = ((COS[angle]*pmaxx) - (SIN[angle]*pminy)) * scale
prty = ((SIN[angle]*pmaxx) + (COS[angle]*pminy)) * scale

prbx = ((COS[angle]*pmaxx) - (SIN[angle]*pmaxy)) * scale
prby = ((SIN[angle]*pmaxx) + (COS[angle]*pmaxy)) * scale


xpos = xpos + porcentx
ypos = ypos + porcenty


pltx = xpos + pltx
plty = ypos + plty
plbx = xpos + plbx
plby = ypos + plby
prtx = xpos + prtx
prty = ypos + prty
prbx = xpos + prbx
prby = ypos + prby


pcolour = RGB(255, 255, 255)

POLYVECTOR pltx, plty, patx, paty, pcolour
POLYVECTOR plbx, plby, patx, patyy, pcolour
POLYVECTOR prbx, prby, patxx, patyy, pcolour

POLYVECTOR prbx, prby, patxx, patyy, pcolour
POLYVECTOR prtx, prty, patxx, paty, pcolour
POLYVECTOR pltx, plty, patx, paty, pcolour

ENDFUNCTION


variables used:-

sprite = the sprite number (that you define)
frame = the animation frame number (always 0 for static images)
xpos = x position of where to draw the sprite on screen
ypos = y position of where to draw the sprite on screen
angle = angle!
scale = scale of sprite!

the sprite array in this example is structured like this:-

sprites[number of sprite][animation frame][0] = top left x position on sprite sheet
sprites[number of sprite][animation frame][1] = top left y position on sprite sheet
sprites[number of sprite][animation frame][2] = x width of sprite
sprites[number of sprite][animation frame][3] = y height of sprite




ampos

This is my final routine, implemented in my Z PROJECT code snipet:

Code (glbasic) Select
FUNCTION z_rotozoomanim: num%,frame%,x%,y%,w%,h%,ang,size,color=0xFFFFFF
LOCAL sx,sy,x1,y1,x2,y2,ancho,alto,xa,ya,x3,x4,y3,y4,xcos,ysin,xs,ys,sw,sh

GETSPRITESIZE num,sx,sy
sw=w;sh=h

x=x_offset+(x*x_zoom)-(((w*size)-w)/2)
w=w*x_zoom*size
y=y_offset+(y*y_zoom)-(((h*size)-h)/2)
h=h*y_zoom*size
INC x2,x1;INC y2,y1

ancho=sx/sw;alto=sy/sh
ya=INTEGER(frame/ancho)*sh
xa=MOD(frame,ancho)*sw

h=h/2
w=w/2
INC x,w
INC y,h

x1 = x
y1 = y
x2 = x
y2 = (y + h)
x3 = (x + w)
y3 = y
x4 = (x + w)
y4 = (y + h)


xcos = tCOS(ang / 4)
ysin = tSIN(ang / 4)

x1 = x - (xcos * w) - (ysin * h)
y1 = y - (xcos * h) + (ysin * w)

x2 = x - (xcos * w) + (ysin * h)
y2 = y + (xcos * h) + (ysin * w)

x3 = x + (xcos * w) - (ysin * h)
y3 = y - (xcos * h) - (ysin * w)

x4 = x + (xcos * w) + (ysin * h)
y4 = y + (xcos * h) - (ysin * w)

SELECT single_sheet
CASE 0
STARTPOLY num
POLYVECTOR x1, y1, xa, ya,color //TL
POLYVECTOR x2, y2, xa, ya + sh,color //BL
POLYVECTOR x4, y4, xa + sw, ya + sh,color //BR
POLYVECTOR x3, y3, xa + sw, ya,color //TR
ENDPOLY
CASE 1
POLYVECTOR x2, y2, xa, ya + sh,color //BL
POLYVECTOR x1, y1, xa, ya,color //TL
POLYVECTOR x4, y4, xa + sw, ya + sh,color //BR
POLYVECTOR x3, y3, xa + sw, ya,color //TR
POLYNEWSTRIP
ENDSELECT

ENDFUNCTION
check my web and/or my blog :D
http://diniplay.blogspot.com (devblog)
http://www.ampostata.org
http://ampostata.blogspot.com
I own PC-Win, MacBook 13", iPhone 3G/3GS/4G and iPAC-WinCE

Nathan

Great stuff, but is there an advantage to using this over the standard GLB commands?

Moru

It's most likely needed on iPhones because they are so crap at sprites :-)

Crivens

I could swear I read on this forum that iOS ran Polyvector slightly slower than the built in sprite commands. Might be a good idea to speed test it first.

Cheers
Current fave quote: Cause you like musicians and I like people with boobs.

spacefractal

iPad do not like colored polyvector (unless if its have been fixed in glbasic), but not used for iPad for my game (only used for font, which is not that much here). But generally Polyvector is faster when doing that with tiles, but its of course depend content of the graphics.
Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation - The beagle Jam - Cave Heroes 2023 - https://spacefractal.itch.io/

ampos

Polyvector is real fast if you are using a single sprite sheet.
check my web and/or my blog :D
http://diniplay.blogspot.com (devblog)
http://www.ampostata.org
http://ampostata.blogspot.com
I own PC-Win, MacBook 13", iPhone 3G/3GS/4G and iPAC-WinCE