Example 0101: Mini Physics Callbacks
-
-
- text = "No collision yet."
-
- function load()
-
-
- local font = love.graphics.newFont(love.default_font, 12)
- love.graphics.setFont(font)
-
-
- world = love.physics.newWorld(2000, 2000)
- world:setGravity(0, 50)
-
-
- ground = love.physics.newBody(world, 0, 0, 0)
-
-
- ground_shape = love.physics.newRectangleShape(ground, 400, 500, 600, 10)
- ground_shape:setData("Ground")
-
-
- ball = love.graphics.newImage("images/love-ball.png")
-
-
- body = love.physics.newBody(world, 400, 200)
-
-
- circle_shape = love.physics.newCircleShape(body, 28)
- circle_shape:setData("Ball")
-
-
-
- body:setMassFromShapes()
-
-
- world:setCallback(collision)
-
- end
-
- function update(dt)
-
- world:update(dt)
- end
-
- function draw()
-
- love.graphics.polygon(love.draw_line, ground_shape:getPoints())
-
-
- love.graphics.draw(ball,body:getX(), body:getY(), body:getAngle())
-
-
- love.graphics.draw(text, 50, 50)
- end
-
- function keypressed(k)
- if k == love.key_space then
-
- body:applyImpulse(100000-math.random(0, 200000), 0)
- end
- end
-
-
- function collision(a, b, c)
- text = "Collided: " .. a .. " and " .. b
- end