October 28, 2020, 05:55:55 AM

Author Topic: [bb] Rigid Body Simulation by Sweenie [ 1+ years ago ]  (Read 1153 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Rigid Body Simulation by Sweenie [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : Rigid Body Simulation
Author : Sweenie
Posted : 1+ years ago

Description : This is the beginning of a simple Rigid Body simulation in Blitz.
No collisions so far.(So the difficult part is yet to be done)


Code :
Code: BlitzBasic
  1. Graphics3D 800,600,0,2
  2. SetBuffer BackBuffer()
  3.  
  4. camera = CreateCamera()
  5. PositionEntity camera,0,0,-10
  6.  
  7. WireFrame True
  8.  
  9. Global Cprod_x#
  10. Global Cprod_y#
  11. Global Cprod_z#
  12. Const NEARZERO# = 0.0001
  13.  
  14. ;RigidBody Type
  15.  
  16. Type RigidBody
  17.  
  18.  Field Mass#
  19.  
  20.  Field Com_x# ; Center Of Mass
  21.  Field Com_y#
  22.  Field Com_z#
  23.  Field Linearvel_x# ; Linear Velocity
  24.  Field Linearvel_y#
  25.  Field Linearvel_z#
  26.  Field Linearacc_x# ; Linear Acceleration
  27.  Field Linearacc_y#
  28.  Field Linearacc_z#
  29.  Field Forcevec_x# ; Force Vector
  30.  Field Forcevec_y#
  31.  Field Forcevec_z#
  32.  Field Forceloc_x# ; Force Location
  33.  Field Forceloc_y#
  34.  Field Forceloc_z#
  35.  
  36.  Field Rotangle_x# ; Rotation Angle (Orientation)
  37.  Field Rotangle_y#
  38.  Field Rotangle_z#
  39.  Field Angularvel_x# ; Angular Velocity
  40.  Field Angularvel_y#
  41.  Field Angularvel_z#
  42.  Field Angularacc_x# ; Angular Acceleration
  43.  Field Angularacc_y#
  44.  Field Angularacc_z#
  45.  Field Inertia_x# ; Rotational Inertia
  46.  Field Inertia_y#
  47.  Field Inertia_z#
  48.  Field Torque_x# ; eh... Just Torque ;)
  49.  Field Torque_y#
  50.  Field Torque_z#
  51.  
  52.  Field Coeff# ; Coefficient of restitution / "Bounce Factor" 0.0 = Not elastic, 1.0 = VERY elastic, 1.1 = Too Elastic ;)
  53.  Field Friction# ; * Not implemented yet *
  54.  Field LinearDamping#
  55.  Field AngularDamping#
  56.  
  57. End Type
  58.  
  59.  
  60. RB.RigidBody = New RigidBody
  61. RBMass#=1.0
  62. RBInertia_x#=0.4
  63. RBInertia_y#=0.4
  64. RBInertia_z#=0.4
  65. RBLinearDamping# = 0.001
  66. RBAngularDamping# = 0.01
  67.  
  68. RB_Mesh = CreateSphere()
  69.  
  70. While Not KeyHit(1)
  71.  
  72. UpdateBody RB,0.02
  73.  
  74. PositionEntity RB_Mesh,RBCom_x#,RBCom_y#,RBCom_z#
  75. RotateEntity RB_Mesh,RBRotangle_x#,-RBRotangle_y#,RBRotangle_z#
  76.  
  77. If KeyDown(205) Then ; Right
  78.  RBForcevec_x# = 1.0
  79.  RBForcevec_y# = 0.0
  80.  RBForcevec_z# = 0.0
  81.  RBForceloc_x# = 0.0
  82.  RBForceloc_y# = 25.0
  83.  RBForceloc_z# = -25.0
  84. ElseIf KeyDown(203) Then ; Left
  85.  RBForcevec_x# = -2.0
  86.  RBForcevec_y# = 0.0
  87.  RBForcevec_z# = 0.0
  88.  RBForceloc_x# = 0.0
  89.  RBForceloc_y# = 15.0
  90.  RBForceloc_z# = -5.0
  91. Else
  92.  RBForcevec_x# = 0.0
  93.  RBForcevec_y# = 0.0
  94.  RBForcevec_z# = 0.0
  95. EndIf
  96.  
  97.  
  98. RenderWorld
  99.  
  100. Flip
  101.  
  102. Wend
  103. End
  104.  
  105.  
  106. Function UpdateBody(Body.RigidBody,dt#)
  107.  
  108.  ; *** Linear Dynamics ***
  109.  
  110.  ;Linear Acceleration
  111.  If BodyMass#>0 Then
  112.   BodyLinearacc_x# = BodyForcevec_x# / BodyMass#
  113.   BodyLinearacc_y# = BodyForcevec_y# / BodyMass#
  114.   BodyLinearacc_z# = BodyForcevec_z# / BodyMass#
  115.  EndIf
  116.  
  117.  ;Linear Velocity
  118.  BodyLinearvel_x# = BodyLinearvel_x# + BodyLinearacc_x# * dt#
  119.  BodyLinearvel_y# = BodyLinearvel_y# + BodyLinearacc_y# * dt#
  120.  BodyLinearvel_z# = BodyLinearvel_z# + BodyLinearacc_z# * dt#
  121.  
  122.  ;Center Of Mass
  123.  BodyCom_x# = BodyCom_x# + BodyLinearvel_x# * dt#
  124.  BodyCom_y# = BodyCom_y# + BodyLinearvel_y# * dt#
  125.  BodyCom_z# = BodyCom_z# + BodyLinearvel_z# * dt#
  126.  
  127.  
  128.  ; *** Rotational Dynamics ***
  129.  
  130.  ;Torque
  131.  CrossProduct BodyForceloc_x#,BodyForceloc_y#,BodyForceloc_z#,BodyForcevec_x#,BodyForcevec_y#,BodyForcevec_z#
  132.  BodyTorque_x#=Cprod_x#
  133.  BodyTorque_y#=Cprod_y#
  134.  BodyTorque_z#=Cprod_z#
  135.  
  136.  ;Angular Acceleration
  137.  BodyAngularacc_x# = BodyTorque_x# / BodyInertia_x#
  138.  BodyAngularacc_y# = BodyTorque_y# / BodyInertia_y#
  139.  BodyAngularacc_z# = BodyTorque_z# / BodyInertia_z#
  140.  
  141.  ;Angular Velocity
  142.  BodyAngularvel_x# = BodyAngularvel_x# + BodyAngularacc_x# * dt#
  143.  BodyAngularvel_y# = BodyAngularvel_y# + BodyAngularacc_y# * dt#
  144.  BodyAngularvel_z# = BodyAngularvel_z# + BodyAngularacc_z# * dt#
  145.  
  146.  ;Angles of rotation / Orientation
  147.  BodyRotangle_x# = BodyRotangle_x# + BodyAngularvel_x# * dt#
  148.  BodyRotangle_y# = BodyRotangle_y# + BodyAngularvel_y# * dt#
  149.  BodyRotangle_z# = BodyRotangle_z# + BodyAngularvel_z# * dt#
  150.  
  151.  ;Damp Linear & Angular Velocity
  152.  BodyLinearvel_x#=BodyLinearvel_x#*(1.0-BodyLinearDamping#)
  153.  BodyLinearvel_y#=BodyLinearvel_y#*(1.0-BodyLinearDamping#)
  154.  BodyLinearvel_z#=BodyLinearvel_z#*(1.0-BodyLinearDamping#)
  155.  BodyAngularvel_x#=BodyAngularvel_x#*(1.0-BodyAngularDamping#)
  156.  BodyAngularvel_y#=BodyAngularvel_y#*(1.0-BodyAngularDamping#)
  157.  BodyAngularvel_z#=BodyAngularvel_z#*(1.0-BodyAngularDamping#)
  158.  
  159.  ;Reset Velocities If Near Zero Value / *Hack* ;) Not necessary but prevents jittering when Body is considered not moving.
  160.  If Abs(BodyLinearvel_x#)<NEARZERO# Then BodyLinearvel_x#=0.0
  161.  If Abs(BodyLinearvel_y#)<NEARZERO# Then BodyLinearvel_y#=0.0
  162.  If Abs(BodyLinearvel_z#)<NEARZERO# Then BodyLinearvel_z#=0.0
  163.  If Abs(BodyAngularvel_x#)<NEARZERO# Then BodyAngularvel_x#=0.0
  164.  If Abs(BodyAngularvel_y#)<NEARZERO# Then BodyAngularvel_y#=0.0
  165.  If Abs(BodyAngularvel_z#)<NEARZERO# Then BodyAngularvel_z#=0.0
  166.  
  167.  
  168. End Function
  169.  
  170. Function DotProduct#(x1#,y1#,z1#,x2#,y2#,z2#)
  171.         Return (x1#*x2#)+(y1#*y2#)+(z1#*z2#)
  172. End Function
  173.  
  174. Function CrossProduct(x1#,y1#,z1#,x2#,y2#,z2#)
  175.         Cprod_x#=(y1#*z2#)-(z1#*y2#)
  176.         Cprod_y#=(z1#*x2#)-(x1#*z2#)
  177.         Cprod_z#=(x1#*y2#)-(y1#*x2#)
  178. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal