UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: kevin on August 24, 2012, 02:49:24 AM

Title: Parallax Mountain Range In Triangles
Post by: kevin on August 24, 2012, 02:49:24 AM
 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

[pbcode]

   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

[/pbcode]


 Related Articles

   * Perspective Parallax (clever coders challenge #18)  (http://www.underwaredesign.com/forums/index.php?topic=3158.0)
   * Tree Parallax (http://www.underwaredesign.com/forums/index.php?topic=3695.0)
   * 8 Way Layered Star Field / Asteroids Style (http://www.underwaredesign.com/forums/index.php?topic=3837.0)
   * Shadow Of The Beast demo (http://www.underwaredesign.com/forums/index.php?topic=1438.0)
   * Thesius XIII - Forest Blast Tech (http://www.underwaredesign.com/forums/index.php?topic=1896.0)
   * Xenon PlayBASIC Tech Demo (http://www.underwaredesign.com/forums/index.php?topic=165.0)
   * 2D Platformer Revisited - Parallax Version (http://www.underwaredesign.com/forums/index.php?topic=2906.0)

 
Title: Re: Parallax Mountain Range In Triangles
Post by: ATLUS on August 24, 2012, 07:32:27 AM
very good example.