Difference between revisions of "Talk:World:setCallbacks"

(Created page with 'Is this example too long? <source lang="lua"> text = "" function love.load() world = love.physics.newWorld(800,600) world:setGravity(0,20) world:setCallbacks(add, persist, re…')
 
Line 1: Line 1:
 
Is this example too long?
 
Is this example too long?
 
<source lang="lua">
 
<source lang="lua">
 +
--[[
 +
Displays text after any callback event for collisions in a world.
 +
--]]
 
text = ""
 
text = ""
  

Revision as of 01:17, 27 February 2010

Is this example too long?

--[[
Displays text after any callback event for collisions in a world.
--]]
text = ""

function love.load()
	world = love.physics.newWorld(800,600)
	world:setGravity(0,20)
	world:setCallbacks(add, persist, rem, result)
	ball = {}
		ball.b = love.physics.newBody(world, 400,200, 10,0)
		ball.s = love.physics.newCircleShape(ball.b, 0,0, 50)
		ball.s:setData("Ball")
	static = {}
		static.b = love.physics.newBody(world, 400,400, 0,0)
		static.s = love.physics.newRectangleShape(static.b, -100,-25, 200,50, 0)
		static.s:setData("Block")
end

function love.update(dt)
	world:update(dt)
end

function love.draw()
	love.graphics.circle("line", ball.b:getX(),ball.b:getY(), ball.s:getRadius())
	local x1,y1, _,_, x3,y3, _,_ = static.s:getBoundingBox()
	local w = x3-x1
	local h = y3-y1
	love.graphics.rectangle("line",
		static.b:getX()-w/2,static.b:getY(),
		w,h, 0)
	love.graphics.print(text,0,12)
end

--Refer to http://love2d.org/wiki/Contact for more information on collision objects
--'coll' is an object created by the collision
--'a' is the first object in the collision and 'b' is the second

function add(a, b, coll)
	text = text..a.." collding with "..b.." at an angle of "..coll:getNormal().."\n"
end

function persist(a, b, coll)
	text = text..a.." touching "..b.."\n"
end

function rem(a, b, coll)
	text = text..a.." uncolliding "..b.."\n"
end

function result(a, b, coll)
	text = text..a.." hit "..b.."resulting with "..coll:getNormal().."\n"
end