2DPhysics_CollisionOnly.lua 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. screen = PhysicsScreen(10, 60)
  2. checkShape = ScreenShape(ScreenShape.SHAPE_RECT, 90,10)
  3. screen:addCollisionChild(checkShape, PhysicsScreenEntity.ENTITY_RECT)
  4. for i=0,50 do
  5. shape = ScreenShape(ScreenShape.SHAPE_RECT, 30,15)
  6. shape:setRotation(random(360))
  7. shape:setPosition(random(640), random(480))
  8. screen:addCollisionChild(shape, PhysicsScreenEntity.ENTITY_RECT)
  9. end
  10. function onNewCollision(t, event)
  11. physicsEvent = safe_cast(event, PhysicsScreenEvent)
  12. if physicsEvent.entity1 == checkShape then
  13. physicsEvent:getFirstEntity():setColor(1.0, 0.0, 0.0, 1.0)
  14. physicsEvent:getSecondEntity():setColor(1.0, 0.0, 0.0, 1.0)
  15. end
  16. end
  17. function onEndCollision(t, event)
  18. physicsEvent = safe_cast(event, PhysicsScreenEvent)
  19. physicsEvent:getFirstEntity():setColor(1.0, 1.0, 1.0, 1.0)
  20. physicsEvent:getSecondEntity():setColor(1.0, 1.0, 1.0, 1.0)
  21. end
  22. screen:addEventListener(nil, onNewCollision, PhysicsScreenEvent.EVENT_NEW_SHAPE_COLLISION)
  23. screen:addEventListener(nil, onEndCollision, PhysicsScreenEvent.EVENT_END_SHAPE_COLLISION)
  24. function onMouseMove(x,y)
  25. checkShape:setPosition(x,y)
  26. end
  27. function Update(e)
  28. checkShape:setRotation(checkShape:getRotation() + (e*100))
  29. end