projectiles.monkey2 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. Namespace myapp
  2. #Import "<mojo3d-vr>"
  3. #Import "assets/scuffed-plastic-pbr/@/scuffed-plastic-pbr"
  4. #Import "assets/plastic-pattern-pbr/@/plastic-pattern-pbr"
  5. #Import "assets/vivecontroller.gltf"
  6. Using std..
  7. Using mojo..
  8. Using mojo3d..
  9. Class MyWindow Extends Window
  10. Field _renderer:VRRenderer
  11. Field _scene:Scene
  12. Field _camera:Camera
  13. Field _light:Light
  14. Field _ground:Model
  15. Field _ctrlModel:Model
  16. Field _bases:=New Model[2]
  17. Field _ctrls:=New Model[2]
  18. Field _bullets:=New Model[2]
  19. Method New( title:String="Simple mojo app",width:Int=640,height:Int=480,flags:WindowFlags=WindowFlags.Resizable )
  20. Super.New( title,width,height,flags )
  21. End
  22. Method OnCreateWindow() Override
  23. SwapInterval=0
  24. 'Enables VR
  25. '
  26. _renderer=New VRRenderer
  27. _renderer.TrackingSpace=VRTrackingSpace.Standing
  28. _scene=New Scene
  29. _scene.UpdateRate=90
  30. _scene.MaxSubSteps=2
  31. 'Use more detailed CSM shadow split distances to compensate for the much nearer near clip plane.
  32. '
  33. _scene.CSMSplits=New Float[]( 2,4,16,256 )
  34. '_scene.UpdateRate=90
  35. 'create camera
  36. '
  37. _camera=New Camera
  38. _camera.Near=.01 'Note: near clip plane 1 centimeter from camera so we can look at things nice and close up.
  39. _camera.Far=100 'camera far/near ratio is probably a bit much
  40. 'create light
  41. '
  42. _light=New Light
  43. _light.Rotate( 75,15,0 )
  44. _light.CastsShadow=True
  45. 'ground material
  46. '
  47. Local groundMaterial:=New PbrMaterial( Color.Green,0,1 )
  48. 'create ground
  49. '
  50. Local groundBox:=New Boxf( -10,-.1,-10,10,.1,10 )
  51. _ground=Model.CreateBox( groundBox,1,1,1,groundMaterial )
  52. _ground.CastsShadow=False
  53. ' _ground.Move( 0,-1,0 )
  54. ' Local groundCollider:=New BoxCollider( _ground )
  55. ' groundCollider.Box=groundBox
  56. Local groundCollider:=New ConvexHullCollider( _ground )
  57. groundCollider.Mesh=_ground.Mesh
  58. '***** bullet mesh collisions suck badly, big problem? *****
  59. ' Local groundCollider:=New MeshCollider( _ground )
  60. ' groundCollider.UseInternalEdgeInfo=True
  61. ' groundCollider.Mesh=_ground.Mesh
  62. Local groundBody:=New RigidBody( _ground )
  63. ' groundBody.Kinematic=True
  64. groundBody.Mass=0
  65. 'create basestations
  66. '
  67. Local baseBox:=New Boxf( -.042,.042 )
  68. Local baseMaterial:=New PbrMaterial( Color.Aluminum,1.0,1.0 )
  69. _bases[0]=Model.CreateBox( baseBox,1,1,1,baseMaterial )
  70. Local baseCollider:=New BoxCollider( _bases[0] )
  71. baseCollider.Box=baseBox
  72. Local baseBody:=New RigidBody( _bases[0] )
  73. baseBody.Mass=0
  74. _bases[1]=_bases[0].Copy()
  75. 'create controllers
  76. '
  77. Local ctrlModel:=Model.Load( "asset::vivecontroller.gltf" )
  78. ctrlModel.Mesh.TransformVertices( AffineMat4f.Rotation( -Pi/2,0,0 ) )
  79. _ctrls[0]=ctrlModel
  80. _ctrls[1]=ctrlModel.Copy()
  81. 'create bullets
  82. '
  83. For Local i:=0 Until 2
  84. Local mat:=New PbrMaterial( i ? Color.Red Else Color.Blue,0,.5 )
  85. _bullets[i]=Model.CreateSphere( .05,24,12,mat,_ctrls[i] )
  86. _bullets[i].LocalPosition=New Vec3f( 0,0,.06 )
  87. Local collider:=New SphereCollider( _bullets[i] )
  88. collider.Radius=.05
  89. Local body:=New RigidBody( _bullets[i] )
  90. body.Kinematic=True 'while under user control
  91. body.Restitution=.7 'bouncy
  92. body.Mass=.170 '170g, about a pool ball?
  93. 'Some fudge...launched dynamic bodies seem to be interfering with kinematic bodies,
  94. 'even when kinematic bodies are hidden? investigate...
  95. body.CollisionGroup=0
  96. body.CollisionMask=0
  97. Next
  98. #If __CONFIG__="debug"
  99. For Local an:=0 Until 360 Step 12
  100. #else
  101. For Local an:=0 Until 360 Step 6
  102. #endif
  103. Local sz:=.2,sz2:=1.50,sz3:=.1
  104. Local box:=New Boxf( -sz,-sz2,-sz3,+sz,+sz2,+sz3 )
  105. Local mat:=New PbrMaterial( New Color( Rnd(),Rnd(),Rnd() ),1,0 )
  106. Local model:=Model.CreateBox( box,1,1,1,mat )
  107. model.Rotate( 0,an,0 )
  108. model.Move( 0,sz2,Rnd( 2,8 ) )
  109. Local collider:=New BoxCollider( model )
  110. collider.Box=box
  111. Local body:=New RigidBody( model )
  112. body.Mass=1 'Default mass, 1kg
  113. Next
  114. End
  115. Method OnRender( canvas:Canvas ) Override
  116. RequestRender()
  117. 'need to call this for VRRenderer before renderer or you'll get an error.
  118. '
  119. _renderer.Update()
  120. '***** shoot *****
  121. Local i:=0
  122. Local cstate:=_renderer.GetControllerState( i )
  123. If cstate.GetButtonPressed( VRButton.Trigger )
  124. If _bullets[i].Visible
  125. Local bullet:=_bullets[i].Copy( Null )
  126. bullet.RigidBody.Kinematic=False
  127. bullet.RigidBody.CollisionGroup=1
  128. bullet.RigidBody.CollisionMask=1
  129. ' bullet.Position=cstate.Matrix.t
  130. ' bullet.RigidBody.LinearVelocity=cstate.Matrix.m.k.Normalize() * 6
  131. bullet.Position=_bullets[i].Position
  132. bullet.RigidBody.LinearVelocity=_bullets[i].Basis.k * 6
  133. ' bullet.RigidBody.ApplyImpulse( _bullets[i].Basis.k/90.0 * 6 )
  134. _bullets[i].Visible=False
  135. Endif
  136. Else
  137. _bullets[i].Visible=True
  138. Endif
  139. '***** throw *****
  140. i=1
  141. cstate=_renderer.GetControllerState( i )
  142. If cstate.GetButtonPressed( VRButton.Trigger )
  143. _bullets[i].Visible=True
  144. Else
  145. If _bullets[i].Visible
  146. Local bullet:=_bullets[i].Copy( Null )
  147. bullet.RigidBody.Kinematic=False
  148. bullet.RigidBody.CollisionGroup=1
  149. bullet.RigidBody.CollisionMask=1
  150. bullet.Position=_bullets[i].Position
  151. Local vel:=_renderer.ControllerMatrices[i].t-_ctrls[i].Position
  152. bullet.RigidBody.LinearVelocity=vel * _scene.UpdateRate * 2
  153. _bullets[i].Visible=False
  154. Endif
  155. Endif
  156. _camera.Matrix=_renderer.HeadMatrix
  157. _ctrls[0].Visible=_renderer.Active
  158. _ctrls[1].Visible=_renderer.Active
  159. _ctrls[0].Matrix=_renderer.ControllerMatrices[0]
  160. _ctrls[1].Matrix=_renderer.ControllerMatrices[1]
  161. _bases[0].Matrix=_renderer.BaseStationMatrices[0]
  162. _bases[1].Matrix=_renderer.BaseStationMatrices[1]
  163. If _renderer.Active _scene.Update()
  164. _scene.Render( canvas )
  165. 'Done!
  166. ' _renderer.LeftEyeImage.Scale=New Vec2f( -1,-1 )
  167. canvas.DrawRect( 0,Height,Width,-Height,_renderer.LeftEyeImage )
  168. canvas.Scale( Width/640.0,Height/480.0 )
  169. canvas.DrawText( "Camera.Position="+_camera.Position+", FPS="+App.FPS,0,0 )
  170. End
  171. End
  172. Function Main()
  173. New AppInstance
  174. New MyWindow
  175. App.Run()
  176. End