Difference between revisions of "Talk:World:setCallbacks"

m
(Example was changed into a tutorial)
 
Line 1: Line 1:
Is this example too long? --[[User:Technocat|Technocat]]
 
<source lang="lua">
 
--[[
 
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
 
</source>
 
 
===Add Callback===
 
This callback is called every time two objects are newly touching.
 
<source lang="lua">
 
function add(a, b, coll)
 
text = text..a.." collding with "..b.." at an angle of "..coll:getNormal().."\n"
 
end
 
</source>
 
===Persist Callback===
 
This callback is called every time two objects are still touching.
 
<source lang="lua">
 
function persist(a, b, coll)
 
text = text..a.." touching "..b.."\n"
 
end
 
</source>
 
===Remove Callback===
 
This callback is called every time two objects are no longer touching.
 
<source lang="lua">
 
function rem(a, b, coll)
 
text = text..a.." uncolliding "..b.."\n"
 
end
 
</source>
 
===Result Callback===
 
This callback is called every time two objects ???.
 
<source lang="lua">
 
function result(a, b, coll)
 
text = text..a.." hit "..b.."resulting with "..coll:getNormal().."\n"
 
end
 
</source>
 

Latest revision as of 19:16, 27 February 2010