function love.load()
--create a new world
world = love.physics.newWorld(0,200,true)
--These callback function names can be almost any you want:
world:setCallbacks(beginContact,endContact,preSolve,postSolve)
-- we'll use this to put info text on the screen later
text = ""
-- we'll use this to store the state of repeated callback calls
persisting = 0
--create a ball with bouncy
ball = {}
ball.b = love.physics.newBody(world, 400,50,"dynamic")
ball.b:setMass(10)
ball.s = love.physics.newCircleShape(20)
ball.f = love.physics.newFixture(ball.b,ball.s)
ball.f:setRestitution(0.4)
ball.f:setUserData("BALL")
--
static = {}
static.b = love.physics.newBody(world,400,400,"static")
static.s = love.physics.newRectangleShape(200,50)
static.f = love.physics.newFixture(static.b,static.s)
static.f:setUserData("STATIC")
end
function love.update(dt)
--let the world have the update event
world:update(dt)
if love.keyboard.isDown("right") then
ball.b:applyForce(1000, 0)
elseif love.keyboard.isDown("left") then
ball.b:applyForce(-1000, 0)
end
if love.keyboard.isDown("up") then
ball.b:applyForce(0, -5000)
elseif love.keyboard.isDown("down") then
ball.b:applyForce(0, 1000)
end
if string.len(text) > 768 then -- cleanup when 'text' gets too long
text = ""
end
end
function love.draw()
--画圆
love.graphics.setColor(255,0,0)
love.graphics.circle("fill", ball.b:getX(),ball.b:getY(), ball.s:getRadius(), 20)
--画矩形
love.graphics.setColor(0,255,0)
love.graphics.polygon("fill", static.b:getWorldPoints(static.s:getPoints()))
love.graphics.print(text, 10, 10)
end
--a is the first fixture object in the collision.
--b is the second fixture object in the collision.
--coll is the contact object created.
function beginContact(a,b,coll)
x,y = coll:getNormal()
text = text.."\n"..a:getUserData().." crashing with "..b:getUserData().." with a vector normal of: "..x..", "..y
end
function endContact(a,b,coll)
persisting = 0 -- reset since they're no longer touching
text = text.."\n"..a:getUserData().." uncolliding with "..b:getUserData()
end
function preSolve(a,b,coll)
-- only say when they first start touching
if persisting == 0 then
text = text.."\n"..a:getUserData().." touchin-- only say when they first start touchingg "..b:getUserData()
elseif persisting < 20 then -- then just start counting
text = text.." "..persisting
end
persisting = persisting + 1 -- keep track of how many updates they've been touching for
end
function postSolve(a,b,coll)
end