News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

Z rotate / Zoom Example

Started by kevin, April 26, 2005, 09:26:53 AM

Previous topic - Next topic

kevin

This examples produces the classic Rot-Zoom effect seen in many games/demos over the years.  


 Updated Code (Dec 2023) for suitable for the PlayBASIC learning edition and retail editions before PlayBASIC V1.64

PlayBASIC Code: [Select]
   //  Locate some media sorted within the PLayBASIC help files
Path$=programdir$()+"Help\Commands\Media\"

// Load it
loadfximage Path$+"bg22.jpg",1



; get images size, which are used when drawing the textured quad
iw=getimagewidth(1)-1
ih=getimageHeight(1)-1

; Create Vertex Type
Type tVert
X#
Y#
RotatedX#
RotatedY#
Endtype

; GridSize is the NUmber vertex in vertex plane
GridSize=13
Dim Verts(GridSize,GridSize) as tVert

; FaceSe is the Size of the Faces (in pixels)
FaceSize=128

; ====================================
; Init Vertex (cnenter around a mid point)
; ====================================
y=0
x=0
For ylp=0 to GridSize
y2=y+FaceSize
x=0
For Xlp=0 to gridsize
x2=x+FaceSize
Verts(xlp,ylp).x#= x-((GridSize*FaceSize)/2)
Verts(xlp,ylp).y#= y-((GridSize*FaceSize)/2)
x=x2
next
y=y2
next

BaseX=getScreenwidth()/2
BaseY=getscreenheight()/2


; start of DO/loop
timestart=timer()

do


TimePast#=(timer()-timeStart)



; bump rotation angle on the time past
; and not the frame rate
Angle# = (TimePast# / 20)
angle#=wrapangle(angle#,360)

; bump scaler zoom
scale#=500+CosRadius(Zoomangle#,400)

ZoomAngle# = (TimePast# / 20) *2
Zoomangle#=mod(ZoomAngle#,360)


; Get the screen Center point

; Pre calc the Cos & Sin
ca#=cos(angle#)
sa#=sin(angle#)

; rotate the points
For ylp=0 to GridSize
For Xlp=0 to gridsize
X#=Verts(xlp,ylp).x
Y#=Verts(xlp,ylp).y
newx#=(((ca#*x#)-(sa#*y#))*500)/Scale#
newy#=(((ca#*y#)+(sa#*x#))*500)/Scale#
Verts(xlp,ylp).rotatedx#=basex+newx#
Verts(xlp,ylp).rotatedy#=basey+newy#
next
next

; Lock Screen Buffer
lockbuffer
; DRaw textured polygons
For ylp=0 to GridSize-1
For Xlp=0 to gridsize-1
; Get Verts of Quad
x1=Verts(xlp,ylp).rotatedx
Y1=Verts(xlp,ylp).rotatedy
X2=Verts(xlp+1,ylp).rotatedx
Y2=Verts(xlp+1,ylp).rotatedy
X3=Verts(xlp+1,ylp+1).rotatedx
Y3=Verts(xlp+1,ylp+1).rotatedy
X4=Verts(xlp,ylp+1).rotatedx
Y4=Verts(xlp,ylp+1).rotatedy
; draw textrued quad
TextureQuad 1,x1,y1,0,0,x2,y2,iw,0,x3,y3,iw,ih,x4,y4,0,ih,0
next
next
unlockbuffer

; Show the screen and loop back to the do
sync
loop













Note: This code required PlayBASIC V1.069 or above to work correctly

PlayBASIC Code: [Select]
;   loadimage "..\..\gfx\bg22.jpg",1
; loadimage "..\..\gfx\back20.jpg",1
; Load and image and prepare it as an FX image
loadimage "..\..\gfx\pblogo1.jpg",1
preparefximage 1

; get images size, which are used when drawing the textured quad
iw=getimagewidth(1)-1
ih=getimageHeight(1)-1

; Create Vertex Type
Type tVert
X#
Y#
RotatedX#
RotatedY#
Endtype

; GridSize is the NUmber vertex in vertex plane
GridSize=13
Dim Verts(GridSize,GridSize) as tVert

; FaceSe is the Size of the Faces (in pixels)
FaceSize=120

; ====================================
; Init Vertex (cnenter around a mid point)
; ====================================
y=0
x=0
For ylp=0 to GridSize
y2=y+FaceSize
x=0
For Xlp=0 to gridsize
x2=x+FaceSize
Verts(xlp,ylp).x#= x-((GridSize*FaceSize)/2)
Verts(xlp,ylp).y#= y-((GridSize*FaceSize)/2)
x=x2
next
y=y2
next

; start of DO/loop
do

; bump rotation angle
angle#=mod(angle#+1,360)

; bump scaler zoom
scale#=500+CosRadius(Zoomangle#,400)
Zoomangle#=mod(ZoomAngle#+2,360)


; Get the screen Center point
BaseX=getScreenwidth()/2
BaseY=getscreenheight()/2

; Pre calc the Cos & Sin
ca#=cos(angle#)
sa#=sin(angle#)

; rotate the points
For ylp=0 to GridSize
For Xlp=0 to gridsize
X#=Verts(xlp,ylp).x
Y#=Verts(xlp,ylp).y
newx#=(((ca#*x#)-(sa#*y#))*500)/Scale#
newy#=(((ca#*y#)+(sa#*x#))*500)/Scale#
Verts(xlp,ylp).rotatedx#=basex+newx#
Verts(xlp,ylp).rotatedy#=basey+newy#
next
next

; Lock Screen Buffer
lockbuffer
; DRaw textured polygons
For ylp=0 to GridSize-1
For Xlp=0 to gridsize-1
; Get Verts of Quad
x1=Verts(xlp,ylp).rotatedx
Y1=Verts(xlp,ylp).rotatedy
X2=Verts(xlp+1,ylp).rotatedx
Y2=Verts(xlp+1,ylp).rotatedy
X3=Verts(xlp+1,ylp+1).rotatedx
Y3=Verts(xlp+1,ylp+1).rotatedy
X4=Verts(xlp,ylp+1).rotatedx
Y4=Verts(xlp,ylp+1).rotatedy
; draw textrued quad
TextureQuad 1,x1,y1,0,0,x2,y2,iw,0,x3,y3,iw,ih,x4,y4,0,ih
next
next
unlockbuffer

; Show the screen and loop back to the do
sync
loop


kevin


Draco9898

Hmmm well no wonder it doesn't work when I tried it, (1.068 here)...
All I get is strange plain strips of the image...
DualCore Intel Core 2 processor @ 2.3 ghz, Geforce 8600 GT (latest forceware drivers), 2 gigs of ram, WIN XP home edition sp2, FireFox 2.

"You'll no doubt be horrified to discover that PlayBasic is a Programming Language." -Kevin