News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

Parallax Mountain Range In Triangles

Started by kevin, August 24, 2012, 02:49:24 AM

Previous topic - Next topic

kevin

 Parallax Mountain Range In Triangles

   This demo draws a Left/Right scrolling parallax scene, much like a Shadow Of The Beast I guess.  The obvious difference would be that scene is drawn using coloured triangles rather than bitmap images.   The effect create by projection the players current x coordinate and wrapping it to the parallax layers tile size.   The projection gives us the relative size, then all we need do is draw a row of the triangles across the screen.    


 Video
 



 Source Code

PlayBASIC Code: [Select]
   setfps 61.7

; projection used for 3d to 2d transformations
Projection#=500


; Init Players starting values
PlayerXpos# =0
PlayerSpeed# =20

; --------------------------------
; Main Loop
; --------------------------------
Do

; draw the backdrop
c0=$504010
c1=$002000
c2=$008000
shadebox 0,000,800,300,c0,c0,c1,c1
shadebox 0,300,800,600,c1,c1,c2,c2


; size of 'mountain tile' in 3d space
TileWidth#=400

;
LayerYpos=100
For Depth#=6050 to 50 step -500


// wrap z cord to a tile width *2
WrappedX#=wrapvalue(PlayerXpos#,0,TileWidth#*2)

; Project Tile Size
ProjectedTileSize#= (TileWidth#*Projection#)/Depth#

; Calc the x pos, project the wrapped X and use as offset on the left
; of the screen.
Xpos#= (TileWidth#*-4)+floor((WrappedX#*Projection#)/Depth#)

; Project X+ Ypos
Ypos#= (GetscreenHeight()/2)+int((LayerYpos*Projection#)/Depth#)

Scale# =1-( Depth#/7000)

Colour=RgbFade($30f020,Scale#*100)

; draw a row triangles to represernt a mountain range. The routine
; expects the x coordinate to be off the screen(to the left) and it'll
; draw a row of triangles from the left to right. Since they're all the
; same we get the illusion of infinite moving playfield.
Draw_Triangle_Mountian_Range(Xpos#,Ypos#,ProjectedTileSize#,ProjectedTileSize#,Colour)
next

// Check key for player movement
if LeftKey() then PlayerSpeed#=-10
if RightKey() then PlayerSpeed#=10

; Move the player
PlayerXpos#+=PlayerSpeed#

; scale the speed down towards zero
PlayerSpeed#=curvevalue(0,PlayerSpeed#,20)

; draw info
setcursor 0,0
print "Arrow Keys"
print "Player Xpos:"+str$(PlayerXpos#)

Sync

loop




Psub Draw_Triangle_Mountian_Range(Xpos#,Ypos#,TileWidth,TileHeight,Colour)

Width=GetSurfaceWidth()-xpos#

TilesAccross =(Width/TileWidth)+1

lockbuffer
For lp =0 to TilesAccross-1 step 2

x1=Xpos#+(lp*TileWidth)
x2=x1+TileWidth
x3=x2+TileWidth
y1=Ypos#
y2=Y1+TileHeight
tric x1,y2,x2,y1,x3,y2,colour

next
unlockbuffer

EndPsub





 Related Articles

   * Perspective Parallax (clever coders challenge #18)
   * Tree Parallax
   * 8 Way Layered Star Field / Asteroids Style
   * Shadow Of The Beast demo
   * Thesius XIII - Forest Blast Tech
   * Xenon PlayBASIC Tech Demo
   * 2D Platformer Revisited - Parallax Version

 

ATLUS