December 03, 2020, 08:31:34 PM

Author Topic: [bb] 2D Golf ball code by DarkNature [ 1+ years ago ]  (Read 486 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] 2D Golf ball code by DarkNature [ 1+ years ago ]
« on: June 29, 2017, 12:28:39 AM »
Title : 2D Golf ball code
Author : DarkNature
Posted : 1+ years ago

Description : Some test stuff I did for my current project. You specify the angle you want to hit the ball at with the A and Z keys and click the left mouse button to hit it. Power is relative to the distance between the ball and the mouse cursor.

Feel free to use this but, please, give me a mention if you do.

Have fun.


Code :
Code: BlitzBasic
  1. Graphics 800,600
  2. SeedRnd(MilliSecs())
  3.  
  4. Type ball
  5.  
  6.         Field x#,y#,h#
  7.         Field lastx,lasty
  8.         Field xv#,yv#,hv#
  9.         Field t
  10.         Field inFlight
  11.        
  12. End Type
  13.  
  14. Const grav#=.00058
  15. Const fric#=0.975
  16. Const bounce#=0.8
  17. Global vAng#=45
  18.  
  19. Global tim=CreateTimer(60)
  20. Global b.ball=New ball
  21. Global windPwr#
  22. Global windAng#
  23.  
  24. bx#=GraphicsWidth()/2
  25. by#=GraphicsHeight()-50
  26. bh#=0
  27. bxv#=0
  28. byv#=0
  29. bhv#=0
  30. binFlight=False
  31.  
  32. Function makeWind()
  33.         windPwr#=Rnd#(0.01,0.02)
  34.         windAng#=Rnd#(360)
  35. End Function
  36.  
  37. Function hitBall()
  38.  
  39.         hAng#=ATan2(MouseX()-bx#, MouseY()-by#)
  40.         pwr#=Float(Sqr((bx#-MouseX())^2+(by#-MouseY())^2))/100
  41.        
  42.         blastx=bx#
  43.         blasty=by#
  44.         bxv#=Sin(hAng#)*pwr#
  45.         byv#=Cos(hAng#)*pwr#
  46.         bhv#=Cos(vAng#)*pwr#
  47.         b       =0
  48.         bh#=0
  49.         binFlight=True
  50.  
  51. End Function
  52.  
  53. Function doBall()
  54.        
  55.         myFric#=fric#
  56.         myBounce#=bounce#
  57.         b       =b      +1
  58.         bx#=bx#+bxv#
  59.         by#=by#+byv#
  60.         bh#=bh#+bhv#
  61.                
  62.         If bh#>0
  63.                 bhv#=bhv#-(grav#*(b     *b      ))*.5
  64.         Else
  65.                 bh#=0
  66.                 bhv#=-bhv#-(grav#*(b    *b      ))*myBounce#
  67.                 bxv#=bxv#*myFric#
  68.                 byv#=byv#*myFric#
  69.         End If
  70.        
  71.         If Abs(bh#)>1
  72.                 bxv#=bxv#+Sin(windAng#)*windPwr#
  73.                 byv#=byv#+Cos(-windAng#)*windPwr#
  74.         End If
  75.        
  76.         If Abs(bxv#)<0.1 And Abs(byv#)<0.1
  77.                 binFlight=False
  78.                 bh#=0
  79.         End If
  80.        
  81.         Color 255,255,255
  82.         Oval bx#-((bh#/5)/2)-2,by#-((bh#/5)/2)-2,4+(bh#/5),4+(bh#/5)
  83.  
  84. End Function
  85.  
  86. SetBuffer BackBuffer()
  87. ClsColor 0,120,0
  88. MoveMouse(400,300)
  89. makeWind()
  90. windtime=MilliSecs()
  91. While Not KeyHit(1)
  92.        
  93.         WaitTimer(tim)
  94.         Cls
  95.                
  96.         Rect MouseX()-5,MouseY()-5,10,10,0
  97.        
  98.         If MouseHit(1) And binFlight=False hitBall()
  99.         If KeyDown(30) And vAng#>0 vAng#=vAng#-0.5
  100.         If KeyDown(44) And vAng#<90 vAng#=vAng#+0.5
  101.        
  102.         xoff=30
  103.         yoff=550
  104.         Line xoff,yoff,xoff-Sin(-vAng#)*50,yoff-Cos(vAng#)*50
  105.         Line xoff,yoff,xoff+50,yoff
  106.         Text xoff,yoff+20,90-vAng#
  107.        
  108.         xoff=GraphicsWidth()-60
  109.         yoff=550
  110.         Line xoff-30,yoff,xoff+30,yoff
  111.         Line xoff,yoff-30,xoff,yoff+30
  112.         Color 2555,255,0
  113.         Line xoff,yoff,xoff+Sin(windAng#)*30,yoff+Cos(windAng#)*30
  114.         Text xoff-25,yoff+35,Int(windPwr#*1000)+" KM/H"
  115.        
  116.         If MilliSecs()>windtime+5000+Rnd(2000) makeWind(): windtime=MilliSecs()
  117.        
  118.         doBall()
  119.        
  120.         Flip
  121.  
  122. Wend
  123. End


Comments :


Doggie(Posted 1+ years ago)

 Excellent. Wonder how I missed this before.


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal