2DPhysics_Contacts.lua 836 B

1234567891011121314151617181920212223242526
  1. collisionSound = Sound("Resources/collision.wav")
  2. scene = PhysicsScene2D(0.1, 60)
  3. shape = ScenePrimitive(ScenePrimitive.TYPE_VPLANE, 1.0,0.05)
  4. shape:setColor(0.0,0.0,0.1,1.0)
  5. shape:setPosition(0, -0.3)
  6. scene:addPhysicsChild(shape, PhysicsScene2DEntity.ENTITY_RECT, true)
  7. for i=0,200 do
  8. shape = ScenePrimitive(ScenePrimitive.TYPE_VPLANE, 0.08,0.02)
  9. shape:setRoll(random(360))
  10. shape:setColor(random(), random(), random(), 1.0)
  11. shape:setPosition(-0.3 + (random()*0.6), random())
  12. scene:addPhysicsChild(shape, PhysicsScene2DEntity.ENTITY_RECT, false)
  13. end
  14. function onSolveCollision(t, event)
  15. physicsEvent = safe_cast(event, PhysicsScene2DEvent)
  16. if physicsEvent.impactStrength > 1.0 then
  17. collisionSound:Play()
  18. end
  19. end
  20. scene:addEventListener(test, onSolveCollision, PhysicsScene2DEvent.EVENT_SOLVE_SHAPE_COLLISION)