July 27, 2021, 23:04:39

Author Topic: [bb] Verlet water :-) by Nate the Great [ 1+ years ago ]  (Read 780 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Verlet water :-)
Author : Nate the Great
Posted : 1+ years ago

Description : This is a modified verlet system for simulating a water level.  The water level variable sets the waterlevel on the y axis and all verlets are affected by the water level.. If an object is too heavy it will sink.  Use arrow keys to move.

Code :
Code: BlitzBasic
  1. Graphics3D 640,480,0,2
  2. SeedRnd(MilliSecs())
  3.  
  4. Global WATERLEVEL = 0
  5. waterpln = CreatePlane()
  6. EntityColor waterpln,0,0,255
  7. EntityAlpha waterpln,.5
  8. MoveEntity waterpln,0,waterlevel,0
  9.  
  10. cam = CreateCamera()
  11. MoveEntity cam,0,2,-30
  12. CameraZoom cam,2
  13. CameraFogMode cam,1
  14. CameraFogRange cam,20,100
  15. CameraRange cam,.001,100
  16. ;CameraClsColor cam,100,100,100
  17. ;CameraFogColor cam,100,100,100
  18.  
  19. piv = CreatePivot()
  20. EntityParent cam,piv
  21. ;TurnEntity piv,0,180,0
  22. plane = CreatePlane()
  23. tex = CreateTexture(256,256)
  24. SetBuffer TextureBuffer(tex)
  25. Color 255,255,255
  26. Rect 0,0,128,128,1
  27. Rect 128,128,128,128
  28. Color 0,225,0
  29. Rect 0,128,128,128
  30. Rect 128,0,128,128
  31. Color 255,255,255
  32.  
  33. mir = CreateMirror()
  34. MoveEntity mir,0,-4,0
  35.  
  36. SetBuffer BackBuffer()
  37. EntityTexture plane,tex
  38. MoveEntity plane,0,-4,0
  39. ScaleTexture tex,10,10
  40. EntityAlpha plane,.5
  41.  
  42. lit = CreateLight()
  43. MoveEntity lit,0,5,0
  44. TurnEntity lit,90,0,0
  45.  
  46. Type verlet
  47.         Field x#,y#,z#
  48.         Field vx#,vy#,vz#
  49.         Field ox#,oy#,oz#
  50.         Field ent,piv
  51.         Field collided,ID,mass#
  52. End Type
  53.  
  54.  
  55. Type rigidbody
  56.         Field x#,y#,z#,ent
  57.         Field yaw#,pitch#,roll#
  58.         Field v1.verlet,v2.verlet,v3.verlet,v4.verlet,v5.verlet,v6.verlet,v7.verlet,v8.verlet,cpiv
  59.         Field ID
  60. End Type
  61.  
  62.  
  63. Type constraint
  64.         Field p1.verlet
  65.         Field p2.verlet
  66.         Field ent
  67.         Field length#
  68. End Type
  69.  
  70. Global rigidbodynum = 0
  71.  
  72. Global cube = CreateCube()
  73. EntityAlpha cube,1
  74. Applyphysicscube(cube,10)
  75.  
  76. SetBuffer BackBuffer()
  77.  
  78. tim = MilliSecs()
  79.  
  80.  
  81. While Not KeyDown(1)
  82. Cls
  83.  
  84. If KeyDown(200) Then MoveEntity cam,0,0,.1
  85. If KeyDown(208) Then MoveEntity cam,0,0,-.1
  86. If KeyDown(203) Then MoveEntity cam,-.1,0,0
  87. If KeyDown(205) Then MoveEntity cam,.1,0,0
  88.  
  89. ;TurnEntity piv,0,1,0
  90.  
  91. updateverlets()
  92.  
  93. updateconstraints()
  94.  
  95. equalizeverlets()
  96.  
  97. drawstuff()
  98.  
  99. UpdateWorld()
  100. RenderWorld()
  101.  
  102. cnt = 0
  103. For v.verlet = Each verlet
  104.         cnt = cnt + 1
  105. Next
  106. Text 1,20,"Verticies: "+cnt
  107. Text 1,1,"FPS: "+1000/(MilliSecs()-tim)
  108. tim = MilliSecs()
  109.  
  110. Flip
  111.  
  112. Wend
  113.  
  114. WaitKey
  115.  
  116. Function Applyphysicscube(ent,mass#,idle = 0,siz# = 5)
  117.  
  118. rigidbodynum = rigidbodynum + 1
  119.  
  120. alph = 0
  121.  
  122. r.rigidbody = New rigidbody
  123. rid = rigidbodynum
  124. rent = ent
  125. rx# = EntityX(rent)
  126. ry# = EntityY(rent)
  127. rz# = EntityZ(rent)
  128. ryaw# = EntityYaw(rent)
  129. rpitch# = EntityPitch(rent)
  130. r
  131. oll# = EntityRoll(rent)
  132.  
  133.  
  134.  
  135. rv1.verlet = New verlet
  136. rv1id = rigidbodynum
  137. rv1x# = rx#-MeshWidth(rent)/2
  138. rv1y# = ry#-MeshHeight(rent)/2
  139. rv1z# = rz#-MeshDepth(rent)/2
  140. rv1ox# = rx#-MeshWidth(rent)/2-.9
  141. rv1oy# = ry#-MeshHeight(rent)/2-.2
  142. rv1oz# = rz#-MeshDepth(rent)/2-.1
  143. rv1ent = rent
  144. rv1mass# = mass#/8
  145. rv1piv = CreateSphere()
  146. EntityAlpha rv1piv,alph
  147. PositionEntity rv1piv,rv1x#,rv1y#,rv1z#
  148.  
  149. rv2.verlet = New verlet
  150. rv2id = rigidbodynum
  151. rv2x# = rx#-MeshWidth(rent)/2
  152. rv2y# = ry#+MeshHeight(rent)/2
  153. rv2z# = rz#+MeshDepth(rent)/2
  154. rv2ox# = rx#-MeshWidth(rent)/2-.3
  155. rv2oy# = ry#+MeshHeight(rent)/2-.3
  156. rv2oz# = rz#+MeshDepth(rent)/2-.1
  157. rv2ent = rent
  158. rv2mass# = mass#/8
  159. rv2piv = CreateSphere()
  160. EntityAlpha rv2piv,alph
  161. PositionEntity rv2piv,rv2x#,rv2y#,rv2z#
  162.  
  163. rv3.verlet = New verlet
  164. rv3id = rigidbodynum
  165. rv3x# = rx#-MeshWidth(rent)/2
  166. rv3y# = ry#-MeshHeight(rent)/2
  167. rv3z# = rz#+MeshDepth(rent)/2
  168. rv3ox# = rx#-MeshWidth(rent)/2-.5
  169. rv3oy# = ry#-MeshHeight(rent)/2-.1
  170. rv3oz# = rz#+MeshDepth(rent)/2-.5
  171. rv3ent = rent
  172. rv3mass# = mass#/8
  173. rv3piv = CreateSphere()
  174. EntityAlpha rv3piv,alph
  175. PositionEntity rv3piv,rv3x#,rv3y#,rv3z#
  176.  
  177. rv4.verlet = New verlet
  178. rv4id = rigidbodynum
  179. rv4x# = rx#-MeshWidth(rent)/2
  180. rv4y# = ry#+MeshHeight(rent)/2
  181. rv4z# = rz#-MeshDepth(rent)/2
  182. rv4ox# = rx#-MeshWidth(rent)/2
  183. rv4oy# = ry#+MeshHeight(rent)/2-.1
  184. rv4oz# = rz#-MeshDepth(rent)/2
  185. rv4ent = rent
  186. rv4mass# = mass#/8
  187. rv4piv = CreateSphere()
  188. EntityAlpha rv4piv,alph
  189. PositionEntity rv4piv,rv4x#,rv4y#,rv4z#
  190.  
  191. rv5.verlet = New verlet
  192. rv5id = rigidbodynum
  193. rv5x# = rx#+MeshWidth(rent)/2
  194. rv5y# = ry#+MeshHeight(rent)/2
  195. rv5z# = rz#+MeshDepth(rent)/2
  196. rv5ox# = rx#+MeshWidth(rent)/2
  197. rv5oy# = ry#+MeshHeight(rent)/2-.2
  198. rv5oz# = rz#+MeshDepth(rent)/2
  199. rv5ent = rent
  200. rv5mass# = mass#/8
  201. rv5piv = CreateSphere()
  202. EntityAlpha rv5piv,alph
  203. PositionEntity rv5piv,rv5x#,rv5y#,rv5z#
  204.  
  205. rv6.verlet = New verlet
  206. rv6id = rigidbodynum
  207. rv6x# = rx#+MeshWidth(rent)/2
  208. rv6y# = ry#+MeshHeight(rent)/2
  209. rv6z# = rz#-MeshDepth(rent)/2
  210. rv6ox# = rx#+MeshWidth(rent)/2
  211. rv6oy# = ry#+MeshHeight(rent)/2-.15
  212. rv6oz# = rz#-MeshDepth(rent)/2
  213. rv6ent = rent
  214. rv6mass# = mass#/8
  215. rv6piv = CreateSphere()
  216. EntityAlpha rv6piv,alph
  217. PositionEntity rv6piv,rv6x#,rv6y#,rv6z#
  218.  
  219. rv7.verlet = New verlet
  220. rv7id = rigidbodynum
  221. rv7x# = rx#+MeshWidth(rent)/2
  222. rv7y# = ry#-MeshHeight(rent)/2
  223. rv7z# = rz#-MeshDepth(rent)/2
  224. rv7ox# = rx#+MeshWidth(rent)/2
  225. rv7oy# = ry#-MeshHeight(rent)/2-.15
  226. rv7oz# = rz#-MeshDepth(rent)/2
  227. rv7ent = rent
  228. rv7mass# = mass#/8
  229. rv7piv = CreateSphere()
  230. EntityAlpha rv7piv,alph
  231. PositionEntity rv7piv,rv7x#,rv7y#,rv7z#
  232.  
  233. rv8.verlet = New verlet
  234. rv8id = rigidbodynum
  235. rv8x# = rx#+MeshWidth(rent)/2
  236. rv8y# = ry#-MeshHeight(rent)/2
  237. rv8z# = rz#+MeshDepth(rent)/2
  238. rv8ox# = rx#+MeshWidth(rent)/2
  239. rv8oy# = ry#-MeshHeight(rent)/2-.15
  240. rv8oz# = rz#+MeshDepth(rent)/2
  241. rv8ent = rent
  242. rv8mass# = mass#/8
  243. rv8piv = CreateSphere()
  244. EntityAlpha rv8piv,alph
  245. PositionEntity rv8piv,rv8x#,rv8y#,rv8z#
  246.  
  247. rcpiv = CreatePivot()
  248. PositionEntity rcpiv,rx#,ry#,rz#
  249.  
  250.  
  251. For v.verlet = Each verlet
  252.         If vID = rigidbodynum Then
  253.                 If idle = 0 Then
  254.                         For vv.verlet = Each verlet
  255.                                 If vvid = rigidbodynum Then
  256.                                         If vvpiv <> vpiv
  257.                                                 c.constraint = New constraint
  258.                                                 cp1.verlet = v.verlet
  259.                                                 cp2.verlet = vv.verlet
  260.                                                 dx# = cp1x# - cp2x#
  261.                                                 dy# = cp1y# - cp2y#
  262.                                                 dz# = cp1z# - cp2z#
  263.                                                 clength# = Sqr(dx#*dx# + dy#*dy# + dz#*dz#)
  264.                                                 cent = cp1ent
  265.                                         EndIf
  266.                                 EndIf
  267.                         Next
  268.                 EndIf
  269.         EndIf
  270. Next
  271.  
  272.  
  273. For c.constraint = Each constraint
  274.         For cc.constraint = Each constraint
  275.                 If cp1piv = ccp1piv And cp2piv = cp1piv Then
  276.                         Delete cc.constraint
  277.                 EndIf
  278.         Next
  279. Next
  280.  
  281. End Function
  282.  
  283.  
  284.  
  285. Function updateverlets()
  286.  
  287.  
  288. For v.verlet = Each verlet
  289.                 vcollided = False
  290.                 vvx# = (vx# - vox#)*.985 ; Get the velocities of the verlet,...add a bit of decay to simulate friction
  291.                 vvy# = (vy# - voy#)*.985
  292.                 vvz# = (vz# - voz#)*.985
  293.  
  294.                 vox# = vx# ; store position in "old"
  295.                 voy# = vy#
  296.                 voz# = vz#
  297.                
  298.                 vx# = vx# + vvx# ;store new postion based on velocity
  299.                
  300.                 vy# = vy# + vvy# - .007
  301.                
  302.                 vz# = vz# + vvz#
  303.                
  304.                 ;check screen bounds
  305.                 If vy#  < -4 ;ground collision
  306.                         vy# = -4
  307.                         vcollided = True
  308.                         vvy# = -vvy#
  309.                 EndIf
  310.                
  311.                 If vy# < waterlevel Then
  312.                         dtmp# = waterlevel-vy#
  313.                         If dtmp# > 1 Then
  314.                                 voy# = voy# - .02
  315.                         Else
  316.                                 voy# = voy# - .02*dtmp#
  317.                         EndIf
  318.                 EndIf
  319. Next
  320.  
  321.  
  322. End Function
  323.  
  324.  
  325.  
  326. Function drawstuff()
  327.         ;For v.verlet = Each verlet
  328.                 ;VertexCoords vsurf,vindex,vx#,vy#,vz#
  329.         ;Next
  330.        
  331.         For r.rigidbody = Each rigidbody
  332.                 cnt = 0
  333.                 avgx# = 0
  334.                 avgy# = 0
  335.                 avgz# = 0
  336.                 For v.verlet = Each verlet
  337.                         If vent = rent
  338.                                 cnt = cnt + 1
  339.                                 avgx# = avgx# + vx#
  340.                                 avgy# = avgy# + vy#
  341.                                 avgz# = avgz# + vz#
  342.                                 PositionEntity vpiv,vx#,vy#,vz#
  343.                         EndIf
  344.                 Next
  345.                 avgx# = avgx#/cnt
  346.                 avgy# = avgy#/cnt
  347.                 avgz# = avgz#/cnt
  348.                
  349.                 rx# = avgx#
  350.                 ry# = avgy#
  351.                 rz# = avgz#
  352.                
  353.                 RotateEntity rent,0,0,0
  354.                 PositionEntity rent,avgx#,avgy#,avgz#
  355.                
  356.                 cnt = 0
  357.                 avgyaw =0
  358.                 avgpitch = 0
  359.                 avgroll = 0
  360.                
  361.                
  362.                 ;this computes the orientation of the verticies using stevie g's code  Thnx Stevie G!!!
  363.                        
  364.                         ;align mesh to verlet cage
  365.                         x# = EntityX( rv5piv ) - EntityX( rv2piv ) + EntityX( rv6piv ) - EntityX( rv4piv )
  366.                         y# = EntityY( rv5piv ) - EntityY( rv2piv ) + EntityY( rv6piv ) - EntityY( rv4piv )
  367.                         z# = EntityZ( rv5piv ) - EntityZ( rv2piv ) + EntityZ( rv6piv ) - EntityZ( rv4piv )
  368.                         AlignToVector rent, x#,y#,z#,1  
  369.                         x# = EntityX( rv5piv ) - EntityX( rv6piv ) + EntityX( rv2piv ) - EntityX( rv4piv )
  370.                         y# = EntityY( rv5piv ) - EntityY( rv6piv ) + EntityY( rv2piv ) - EntityY( rv4piv )
  371.                         z# = EntityZ( rv5piv ) - EntityZ( rv6piv ) + EntityZ( rv2piv ) - EntityZ( rv4piv )
  372.                         AlignToVector rent, x#,y#,z#, 3
  373.                        
  374.  
  375.                        
  376.                
  377.         Next
  378. End Function
  379.  
  380.  
  381. Function updateconstraints()
  382.  
  383. For a = 1 To 10
  384.         For c.constraint = Each constraint
  385.                 dx#=cp2x#       -       cp1x#
  386.                 dy#=cp2y#       -       cp1y#
  387.                 dz#=cp2z#       -       cp1z#
  388.                                
  389.                 length#=Sqr(dx*dx + dy*dy + dz*dz) ; distance between p1 and p2
  390.  
  391.                 If length#<>0 ;avoid divide by 0, then normalize the vector
  392.                         diff# = (length# - clength#) / length#  ; vector length minus constraint length
  393.                 Else
  394.                         diff# = 0
  395.                 EndIf
  396.  
  397.                 dx# = dx# * .5 ;find the midpoint
  398.                 dy# = dy# * .5
  399.                 dz# = dz# * .5
  400.                
  401.                 If cp1collided = 0 Or a > 8 Then
  402.                         cp1x# = cp1x# + diff# * dx#
  403.                         cp1y# = cp1y# + diff# * dy#
  404.                         cp1z# = cp1z# + diff# * dz#
  405.                 EndIf
  406.                 If cp2collided = 0 Or a > 8 Then
  407.                         cp2x# = cp2x# - diff# * dx#
  408.                         cp2y# = cp2y# - diff# * dy#
  409.                         cp2z# = cp2z# - diff# * dz#
  410.                 EndIf
  411.         Next  ;constraints
  412. Next
  413.  
  414.  
  415. End Function
  416.  
  417.  
  418.  
  419. Function equalizeverlets()
  420.  
  421. ;For c.constraint = Each constraint
  422. ;       If clength# = 0 Then
  423. ;               dx#=cp2x#       +       cp1x#
  424. ;               dy#=cp2y#       +       cp1y#
  425. ;               dz#=cp2z#       +       cp1z#
  426. ;              
  427. ;               dx# = dx# * .5 ;find the midpoint
  428. ;               dy# = dy# * .5
  429. ;               dz# = dz# * .5
  430. ;              
  431. ;               cp2x# = dx#
  432. ;               cp2y# = dy#
  433. ;               cp2z# = dz#
  434. ;              
  435. ;               cp1x# = dx#
  436. ;               cp1y# = dy#
  437. ;               cp1z# = dz#
  438. ;              
  439. ;       EndIf
  440. ;Next
  441.  
  442. End Function
  443.  
  444.  
  445.  
  446. Function rotatephysicsentity(ent)
  447.  
  448. For r.rigidbody = Each rigidbody
  449.         If rent = ent Then
  450.                 tmppiv = CreatePivot()
  451.                 PositionEntity tmppiv,rx#,ry#,rz#
  452.         EndIf
  453. Next
  454.  
  455. End Function


Comments :


GW(Posted 1+ years ago)

 Pretty Neat!if you turn off vsync, the program wont run. [div by 0]


Nate the Great(Posted 1+ years ago)

 oh oops im not sure why that happens?


Difference(Posted 1+ years ago)

 My guess is framecounter delta = 0


Nate the Great(Posted 1+ years ago)

 oh this doesnt use delta timing sorry :/it just goes as fast as it can


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal