Difference between revisions of "Talk:World:setCallbacks"

m
Line 9: Line 9:
 
world = love.physics.newWorld(800,600)
 
world = love.physics.newWorld(800,600)
 
world:setGravity(0,20)
 
world:setGravity(0,20)
 +
--The four callback names can be anything you want.
 
world:setCallbacks(add, persist, rem, result)
 
world:setCallbacks(add, persist, rem, result)
 
ball = {}
 
ball = {}

Revision as of 01:31, 27 February 2010

Is this example too long? --Technocat

--[[
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)
	--The four callback names can be anything you want.
	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 persist(a, b, coll)
	text = text..a.." touching "..b.."\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

Add Callback

This callback is called every time two objects are newly touching.

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

Persist Callback

This callback is called every time two objects are still touching.

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

Remove Callback

This callback is called every time two objects are no longer touching.

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

Result Callback

This callback is called every time two objects ???.

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