hellochimpmunk.monkey2 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #Import "<std>"
  2. #Import "<mojo>"
  3. #Import "<chipmunk>"
  4. #Import "chipmunkdebugger"
  5. Using std..
  6. Using mojo..
  7. Using chipmunk..
  8. Class HelloChipmunk Extends Window
  9. Field space:cpSpace
  10. Field ground:cpShape
  11. Field ballBody:cpBody
  12. Field ballShape:cpShape
  13. Field ballBody2:cpBody
  14. Field ballShape2:cpShape
  15. Field polyBody:cpBody
  16. Field polyShape:cpShape
  17. Field debugger:=New ChipmunkDebugger
  18. Method New()
  19. ClearColor=Color.Grey'Black
  20. 'Create a new space and set its gravity to 100
  21. '
  22. space=cpSpaceNew()
  23. space.Gravity=cpv( 0,100 )
  24. 'Add a static line segment shape for the ground.
  25. 'We'll make it slightly tilted so the ball will roll off.
  26. 'We attach it to space->staticBody to tell Chipmunk it shouldn't be movable.
  27. '
  28. ground=cpSegmentShapeNew( space.StaticBody,cpv( -100,15 ),cpv( 100,-15 ),0 )
  29. ground.Friction=1
  30. ground.CollisionType=1
  31. space.AddShape( ground )
  32. Local tshape:=space.AddShape( cpSegmentShapeNew( space.StaticBody,cpv(-Width/2,Height/2-64),cpv(0,Height/2),0 ) )
  33. tshape.Friction=1
  34. tshape.CollisionType=1
  35. tshape=space.AddShape( cpSegmentShapeNew( space.StaticBody,cpv(0,Height/2),cpv(Width/2,Height/2-64),0 ) )
  36. tshape.Friction=1
  37. tshape.CollisionType=1
  38. 'Now let's make a ball that falls onto the line and rolls off.
  39. 'First we need to make a cpBody to hold the physical properties of the object.
  40. 'These include the mass, position, velocity, angle, etc. of the object.
  41. 'Then we attach collision shapes to the cpBody to give it a size and shape.
  42. Local mass:=1.0
  43. Local radius:=10.0
  44. 'The moment of inertia is like mass for rotation
  45. 'Use the cpMomentFor*() functions to help you approximate it.
  46. Local moment:=cpMomentForCircle( mass,0,radius,cpvzero )
  47. 'The cpSpaceAdd*() functions return the thing that you are adding.
  48. 'It's convenient to create and add an object in one line.
  49. ballBody=space.AddBody( cpBodyNew( mass,moment ) )
  50. ballBody.Position=cpv( 0,-100 )
  51. 'Now we create the collision shape for the ball.
  52. 'You can create multiple collision shapes that point to the same body.
  53. 'They will all be attached to the body and move around to follow it.
  54. ballShape=space.AddShape( cpCircleShapeNew( ballBody,radius,cpvzero ) )
  55. ballShape.Friction=0.7
  56. ballShape.CollisionType=2
  57. ballBody2=space.AddBody( cpBodyNew( mass,moment ) )
  58. ballBody2.Position=cpv( 50,-100 )
  59. ballShape2=space.AddShape( cpCircleShapeNew( ballBody2,radius,cpvzero ) )
  60. ballShape2.Friction=0.7
  61. ballShape2.CollisionType=2
  62. 'Now a pentagon...
  63. mass=0.3
  64. radius=30.0
  65. Local NUM_VERTS:=5
  66. Local verts:=New cpVect[NUM_VERTS]
  67. For Local it:=0 Until NUM_VERTS
  68. Local angle:=TwoPi * it / NUM_VERTS
  69. verts[it]=cpv( radius*Cos( angle ),radius*Sin( angle ) )
  70. Next
  71. moment=cpMomentForPoly( mass,NUM_VERTS,verts.Data,cpvzero,0.0 )
  72. polyBody=space.AddBody( cpBodyNew( mass,moment ) )
  73. polyBody.Position=cpv( 50.0,-190.0 )
  74. polyShape=space.AddShape( cpPolyShapeNew( polyBody,NUM_VERTS,verts.Data,cpTransformIdentity,0.0 ) )
  75. polyShape.Friction=0.03
  76. Local handler:=space.AddDefaultCollisionHandler()
  77. 'Add collision handler...
  78. handler.beginFunc=Lambda:cpBool( arbiter:cpArbiter,space:cpSpace,data:cpDataPointer )
  79. Local a:cpShape,b:cpShape
  80. arbiter.GetShapes( Varptr a,Varptr b )
  81. Print "Collision! a="+a.CollisionType+", b="+b.CollisionType
  82. Return True
  83. End
  84. End
  85. Method OnRender( canvas:Canvas ) Override
  86. App.RequestRender()
  87. Const timeStep:=1.0/60.0
  88. space.StepTime( timeStep )
  89. ' Local rot:=ballBody.Rotation
  90. ' Local pos:=ballBody.Position
  91. ' Local vel:=ballBody.Velocity
  92. ' Print "ball rot="+ATan2( rot.y,rot.x )+", pos.x="+pos.x+", pos.y="+pos.y+", vel.x="+vel.x+", vel.y="+vel.y
  93. canvas.Translate( Width/2,Height/2 )
  94. debugger.DebugDraw( canvas,space )
  95. End
  96. Method Cleanup() 'Yeah, right!
  97. cpShapeFree( ballShape )
  98. cpBodyFree( ballBody )
  99. cpShapeFree( ground )
  100. cpSpaceFree( space )
  101. End
  102. End
  103. Function Main()
  104. New AppInstance
  105. New HelloChipmunk
  106. App.Run()
  107. End