hellochimpmunk.monkey2 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #Import "<std>"
  2. #Import "<mojo>"
  3. #Import "<chipmunk>"
  4. Using std..
  5. Using mojo..
  6. Using chipmunk..
  7. Class HelloChipmunk Extends Window
  8. Field space:cpSpace Ptr
  9. Field ground:cpShape Ptr
  10. Field ballBody:cpBody ptr
  11. Field ballShape:cpShape Ptr
  12. Method New()
  13. ClearColor=Color.Black
  14. 'Create an empty space.
  15. space=cpSpaceNew()
  16. cpSpaceSetGravity( space,cpv( 0,100 ) )
  17. 'Add a static line segment shape for the ground.
  18. 'We'll make it slightly tilted so the ball will roll off.
  19. 'We attach it to space->staticBody to tell Chipmunk it shouldn't be movable.
  20. ground=cpSegmentShapeNew( cpSpaceGetStaticBody( space ),cpv( -100,15 ), cpv( 100,-15 ),0 )
  21. cpShapeSetFriction( ground,1 )
  22. cpSpaceAddShape( space,ground )
  23. 'Now let's make a ball that falls onto the line and rolls off.
  24. 'First we need to make a cpBody to hold the physical properties of the object.
  25. 'These include the mass, position, velocity, angle, etc. of the object.
  26. 'Then we attach collision shapes to the cpBody to give it a size and shape.
  27. Local radius:=10
  28. Local mass:=1
  29. 'The moment of inertia is like mass for rotation
  30. 'Use the cpMomentFor*() functions to help you approximate it.
  31. Local moment:=cpMomentForCircle( mass,0,radius,cpvzero )
  32. 'The cpSpaceAdd*() functions return the thing that you are adding.
  33. 'It's convenient to create and add an object in one line.
  34. ballBody=cpSpaceAddBody( space,cpBodyNew( mass,moment ) )
  35. cpBodySetPosition( ballBody,cpv( 0,-100 ) )
  36. 'Now we create the collision shape for the ball.
  37. 'You can create multiple collision shapes that point to the same body.
  38. 'They will all be attached to the body and move around to follow it.
  39. ballShape=cpSpaceAddShape( space,cpCircleShapeNew( ballBody,radius,cpvzero ) )
  40. cpShapeSetFriction( ballShape,0.7 )
  41. End
  42. Method OnRender( canvas:Canvas ) Override
  43. App.RequestRender()
  44. 'It is *highly* recommended to use a fixed size time step.
  45. Local timeStep:=1.0/60.0
  46. cpSpaceStep( space,timeStep )
  47. Local rot:=cpBodyGetRotation( ballBody )
  48. Local pos:=cpBodyGetPosition( ballBody )
  49. Local vel:=cpBodyGetVelocity( ballBody )
  50. Print "ball rot="+ATan2( rot.y,rot.x )+", pos.x="+pos.x+", pos.y="+pos.y+", vel.x="+vel.x+", vel.y="+vel.y
  51. canvas.Translate( Width/2,Height/2 )
  52. canvas.DrawCircle( pos.x,pos.y,10 )
  53. canvas.Color=Color.Black
  54. canvas.DrawLine( pos.x,pos.y,pos.x+rot.x*10,pos.y+rot.y*10 )
  55. canvas.Color=Color.Blue
  56. canvas.DrawLine( -100,15,100,-15 )
  57. End
  58. Method Cleanup() 'Yeah, right!
  59. cpShapeFree( ballShape )
  60. cpBodyFree( ballBody )
  61. cpShapeFree( ground )
  62. cpSpaceFree( space )
  63. End
  64. End
  65. Function Main()
  66. New AppInstance
  67. New HelloChipmunk
  68. App.Run()
  69. End