|
|
Example
Mini Physics Contacts
Example 0102: Mini Physics Contacts
-
-
- 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:setRestitution(0.3)
- 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)
-
- local f, r = c:getFriction(), c:getRestitution()
- local s = c:getSeparation()
- local px, py = c:getPosition()
- local vx, vy = c:getVelocity()
- local nx, ny = c:getNormal()
-
- text = "Last Collision:\n"
- text = text .. "Shapes: " .. a .. " and " .. b .. "\n"
- text = text .. "Position: " .. px .. "," .. py .. "\n"
- text = text .. "Velocity: " .. vx .. "," .. vy .. "\n"
- text = text .. "Normal: " .. nx .. "," .. ny .. "\n"
- text = text .. "Friction: " .. f .. "\n"
- text = text .. "Restitution: " .. r .. "\n"
- text = text .. "Separation: " .. s .. "\n"
-
- end
|