2DPhysics_Contacts.lua 840 B

1234567891011121314151617181920212223242526
  1. -- makes a sound when a collision impact stronger than 5 happens
  2. screen = PhysicsScreen(10, 60)
  3. shape = ScreenShape(ScreenShape.SHAPE_RECT, 600,30)
  4. shape:setColor(0.0,0.0,0.0,1.0)
  5. shape:setPosition(640/2, 400)
  6. screen:addPhysicsChild(shape, PhysicsScreenEntity.ENTITY_RECT, true)
  7. for i=0,50 do
  8. shape = ScreenShape(ScreenShape.SHAPE_RECT, 20,5)
  9. shape:setRotation(random(360))
  10. shape:setPosition(random(640), random(300))
  11. screen:addPhysicsChild(shape, PhysicsScreenEntity.ENTITY_RECT, false)
  12. end
  13. collisionSound = Sound("Resources/collision.wav")
  14. function onSolveCollision(t, event)
  15. physicsEvent = safe_cast(event, PhysicsScreenEvent)
  16. if physicsEvent.impactStrength > 5 then
  17. collisionSound:Play()
  18. end
  19. end
  20. screen:addEventListener(test, onSolveCollision, PhysicsScreenEvent.EVENT_SOLVE_SHAPE_COLLISION)