Author Topic: RotoSprite 3D  (Read 2042 times)

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10760
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
RotoSprite 3D
« on: 2007-Apr-24 »
Hier mal eine Funktion, die ein Sprite um 3 Achsen dreht, wobei ich auf X/Z und Y/Z begrenzen w├╝rde:

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