Function
World:setCallback( f )
The callback (if any) will be called every time contact is made between two shapes in the World. The Lua function should accept three parameters: shapedata1, shapedata2 and a Contact object.
Synopsis
World:setCallback( f )
Arguments
f A Lua function.
Returns
(Nothing)
Examples
Example 101: Mini Physics Callbacks
  1. -- Example: Mini Physics Callbacks 
  2.  
  3. text = "No collision yet." 
  4.  
  5. function load() 
  6.  
  7.    -- Set a font. 
  8.    local font = love.graphics.newFont(love.default_font, 12
  9.    love.graphics.setFont(font) 
  10.  
  11.    -- Create a world with size 2000 in every direction. 
  12.    world = love.physics.newWorld(20002000
  13.    world:setGravity(050
  14.  
  15.    -- Create the ground body at (0, 0) with mass 0. 
  16.    ground = love.physics.newBody(world, 000
  17.  
  18.    -- Create the ground shape at (400,500) with size (600,10). 
  19.    ground_shape = love.physics.newRectangleShape(ground, 40050060010
  20.    ground_shape:setData("Ground"-- Set a string userdata 
  21.  
  22.    -- Load the image of the ball. 
  23.    ball = love.graphics.newImage("images/love-ball.png"
  24.  
  25.    -- Create a Body for the circle. 
  26.    body = love.physics.newBody(world, 400200
  27.     
  28.    -- Attatch a shape to the body. 
  29.    circle_shape = love.physics.newCircleShape(body, 28
  30.    circle_shape:setData("Ball"-- Set a string userdata 
  31.     
  32.    -- Calculate the mass of the body based on attatched shapes. 
  33.    -- This gives realistic simulations. 
  34.    body:setMassFromShapes() 
  35.     
  36.    -- Set the collision callback. 
  37.    world:setCallback(collision) 
  38.  
  39. end 
  40.  
  41. function update(dt) 
  42.    -- Update the world. 
  43.    world:update(dt) 
  44. end 
  45.  
  46. function draw() 
  47.    -- Draws the ground. 
  48.    love.graphics.polygon(love.draw_line, ground_shape:getPoints()) 
  49.  
  50.    -- Draw the circle. 
  51.    love.graphics.draw(ball,body:getX(), body:getY(), body:getAngle()) 
  52.  
  53.    -- Draw text. 
  54.    love.graphics.draw(text, 5050
  55. end 
  56.  
  57. function keypressed(k) 
  58.    if k == love.key_space then 
  59.       -- Apply a random impulse 
  60.       body:applyImpulse(100000-math.random(0200000), 0
  61.    end 
  62. end 
  63.  
  64. -- This is called every time a collision occurs. 
  65. function collision(a, b, c) 
  66.    text = "Collided: " .. a .. " and " .. b 
  67. end 
Copyright © 2006-2008 LÖVE Development Team.