### Author Topic: RotoSprite 3D  (Read 5019 times)

#### Kitty Hello

• code monkey
• Prof. Inline
• Posts: 10814
• here on my island the sea says 'hello'
##### RotoSprite 3D
« on: 2007-Apr-24 »
Here's some code to rotate a sprite around 3 axes. I suggest rotating around X/Z and Y/Z only, though.

Code: (glbasic) [Select]
`FILLRECT 0,0,31,16,RGB(255,0,0)FILLRECT 8,0,24,31,RGB(255,128,0)GRABSPRITE 0,0,0,32,32FILLRECT 0,0,31,16,RGB(64,64,64)FILLRECT 8,0,24,31,RGB(64,64,64)FILLRECT 12,0,20,31,RGB(99,99,99)GRABSPRITE 1,0,0,32,32WHILE TRUE MOUSESTATE mx, my, b1,b2 phi = GETTIMERALL()/100 PukeSprite(0,1, my*2, mx*2, phi, 100, 100) PRINT mx + " - " + my + " - "+phi, 0,0 SHOWSCREENWEND// rotate a sprite in a terrible way// img1 - upper side// img2 - bottom side (same orientation)// rotX - rotation angle around x axis (right side)// rotY - rotation around up-down axis on screen// rotZ - rotation like ROTOSPRITEFUNCTION PukeSprite: img1, img2, rotX, rotY, rotZ, x,yLOCAL w, h, phi, swap, tx, tyLOCAL s, c, cl, px, py GETSPRITESIZE img1, w,h tx=w ty=h phi = MOD(ABS(rotY), 360) IF phi < 90 OR phi>270 swap = img1 img1=img2 img2=swap ENDIF w = w * COS(phi) phi = MOD(ABS(rotX), 360) IF phi < 90 OR phi>270 swap = img1 img1=img2 img2=swap ENDIF h = h * COS(phi) s=SIN(rotZ) c=COS(rotZ) w=w/2 h=h/2 cl=RGB(255,255,255) STARTPOLY img1 px=-w; py=-h; POLYVECTOR x+px*c+py*s, y-px*s+py*c,  0, 0,cl px=-w; py= h; POLYVECTOR x+px*c+py*s, y-px*s+py*c,  0,ty,cl px= w; py= h; POLYVECTOR x+px*c+py*s, y-px*s+py*c, tx,ty,cl px= w; py=-h; POLYVECTOR x+px*c+py*s, y-px*s+py*c, tx, 0,cl // and back for back face culling px= w; py= h; POLYVECTOR x+px*c+py*s, y-px*s+py*c, tx,ty,cl px=-w; py= h; POLYVECTOR x+px*c+py*s, y-px*s+py*c,  0,ty,cl px=-w; py=-h; POLYVECTOR x+px*c+py*s, y-px*s+py*c,  0, 0,cl ENDPOLYENDFUNCTION`

#### Hemlos

• To boldy go where no pixel has gone before!
• Global Moderator
• Prof. Inline
• Posts: 1621
• Particle Hawk
##### Re: RotoSprite 3D
« Reply #1 on: 2008-Aug-05 »
2d/3d drawsprite 3d rotation.
This is rewritten for the GLBASIC.EXE V.5+
-Hem

Code: (glbasic) [Select]
`// --------------------------------- //// Project: test// Start: Tuesday, August 05, 2008// IDE Version: 5.341GLOBAL mx,my,b1,b2DRAWRECT 0,0,31,16,RGB(255,0,0)DRAWRECT 8,0,24,31,RGB(255,128,0)GRABSPRITE 0,0,0,32,32DRAWRECT 0,0,31,16,RGB(64,64,64)DRAWRECT 8,0,24,31,RGB(64,64,64)DRAWRECT 12,0,20,31,RGB(99,99,99)GRABSPRITE 1,0,0,32,32WHILE TRUE MOUSESTATE mx, my, b1,b2 phi = GETTIMERALL()/100 PukeSprite(0,1, my*2, mx*2, phi, 100, 100) PRINT mx + " - " + my + " - "+phi, 0,0 SHOWSCREENWEND// rotate a sprite in a terrible way// img1 - upper side// img2 - bottom side (same orientation)// rotX - rotation angle around x axis (right side)// rotY - rotation around up-down axis on screen// rotZ - rotation like ROTOSPRITEFUNCTION PukeSprite: img1, img2, rotX, rotY, rotZ, x,yLOCAL w, h, phi, swap, tx, tyLOCAL s, c, cl, px, py GETSPRITESIZE img1, w,h tx=w ty=h phi = MOD(ABS(rotY), 360) IF phi < 90 OR phi>270 swap = img1 img1=img2 img2=swap ENDIF w = w * COS(phi) phi = MOD(ABS(rotX), 360) IF phi < 90 OR phi>270 swap = img1 img1=img2 img2=swap ENDIF h = h * COS(phi) s=SIN(rotZ) c=COS(rotZ) w=w/2 h=h/2 cl=RGB(255,255,255) STARTPOLY img1 px=-w; py=-h; POLYVECTOR x+px*c+py*s, y-px*s+py*c,  0, 0,cl px=-w; py= h; POLYVECTOR x+px*c+py*s, y-px*s+py*c,  0,ty,cl px= w; py= h; POLYVECTOR x+px*c+py*s, y-px*s+py*c, tx,ty,cl px= w; py=-h; POLYVECTOR x+px*c+py*s, y-px*s+py*c, tx, 0,cl // and back for back face culling px= w; py= h; POLYVECTOR x+px*c+py*s, y-px*s+py*c, tx,ty,cl px=-w; py= h; POLYVECTOR x+px*c+py*s, y-px*s+py*c,  0,ty,cl px=-w; py=-h; POLYVECTOR x+px*c+py*s, y-px*s+py*c,  0, 0,cl ENDPOLYENDFUNCTION`
« Last Edit: 2008-Aug-05 by Hemlos »
Volume_of_Earth(km^3) = 4/3*3.14*POW(6371.392896,3)

#### Kitty Hello

• code monkey
• Prof. Inline
• Posts: 10814
• here on my island the sea says 'hello'
##### Re: RotoSprite 3D
« Reply #2 on: 2008-Aug-05 »
Ah. Thx Hem.