November 28, 2020, 10:37:50 AM

Author Topic: [bmx] Shadows by SillyPutty [ 1+ years ago ]  (Read 377 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] Shadows by SillyPutty [ 1+ years ago ]
« on: June 29, 2017, 12:28:40 AM »
Title : Shadows
Author : SillyPutty
Posted : 1+ years ago

Description : How to draw 2D shadows

Code :
Code: BlitzMax
  1. SuperStrict
  2.  
  3. Graphics 640,480
  4.  
  5. Global xpos:Float
  6. Global ypos:Float
  7. Global angle:Float
  8. Global Falloff:Int = 1000
  9.  
  10. SetClsColor 125,125,125
  11.  
  12. Function DrawShadowCaster(x1:Float,y1:Float,x2:Float,y2:Float)
  13.        
  14.         Local shadowCaster:Float[8]
  15.         Local lightAngle:Float
  16.        
  17.         lightAngle = ATan2(xpos-x2,ypos-y2)
  18.  
  19.         SetColor 0,0,0
  20.        
  21.         shadowCaster[0] = x1
  22.         shadowCaster[1] = y1
  23.         shadowCaster[2] = x2
  24.         shadowCaster[3] = y2
  25.         shadowCaster[4] = x2-Sin(lightAngle)*Falloff
  26.         shadowCaster[5] = y2-Cos(lightAngle)*Falloff
  27.  
  28.         lightAngle = ATan2(xpos-x1,ypos-y1)
  29.  
  30.         shadowCaster[6] = x1-Sin(lightAngle)*Falloff
  31.         shadowCaster[7] = y1-Cos(lightAngle)*Falloff
  32.        
  33.         DrawPoly ShadowCaster
  34.         SetColor 255,255,255
  35.         DrawLine x1,y1,x2,y2
  36.        
  37. End Function
  38.  
  39.  
  40. While not KeyDown(KEY_ESCAPE)
  41.        
  42.        
  43.         xpos = MouseX()
  44.         ypos = MouseY()
  45.        
  46.         SetColor 255,255,0
  47.         DrawOval xpos-5,ypos-5,10,10
  48.        
  49.         ' box
  50.         DrawShadowCaster 200,200,400,200
  51.         DrawShadowCaster 200,200,200,100
  52.         DrawShadowCaster 200,100,400,100
  53.         DrawShadowCaster 400,100,400,200
  54.        
  55.         'wall
  56.         DrawShadowCaster 200,400,400,400
  57.  
  58.         DrawLine 200,200,400,200
  59.         DrawLine 200,200,200,100
  60.         DrawLine 200,100,400,100
  61.         DrawLine 400,100,400,200
  62.        
  63.         Flip
  64.         Cls
  65. Wend


Comments :


SillyPutty(Posted 1+ years ago)

 Thanks to Cyanide who gave me an example about a year ago on how to do this, since then I lost the code he gave me, but knocked this one up from what I learnt from his method. Adding it here so I dont lose the code again :)


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal