RotoSprite 3D

Previous topic - Next topic

Kitty Hello

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,32
FILLRECT 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,32


WHILE 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
SHOWSCREEN


WEND

// 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 ROTOSPRITE
FUNCTION PukeSprite: img1, img2, rotX, rotY, rotZ, x,y
LOCAL w, h, phi, swap, tx, ty
LOCAL 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
ENDPOLY
ENDFUNCTION

Hemlos

#1
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.341
GLOBAL mx,my,b1,b2

DRAWRECT 0,0,31,16,RGB(255,0,0)
DRAWRECT 8,0,24,31,RGB(255,128,0)
GRABSPRITE 0,0,0,32,32
DRAWRECT 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,32


WHILE 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
SHOWSCREEN


WEND

// 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 ROTOSPRITE
FUNCTION PukeSprite: img1, img2, rotX, rotY, rotZ, x,y
LOCAL w, h, phi, swap, tx, ty
LOCAL 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
ENDPOLY
ENDFUNCTION
Bing ChatGpt is pretty smart :O

Kitty Hello

Ah. Thx Hem.