Function
Contact:getPosition( )
Synopsis
x, y = Contact:getPosition( )
Arguments
(None)
Returns
number The x-component of the point.
number The y-component of the point.
Examples
Example 102: Mini Physics Contacts
  1. -- Example: Mini Physics Contacts 
  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:setRestitution(0.3-- More bounce 
  31.    circle_shape:setData("Ball"-- Set a string userdata 
  32.     
  33.    -- Calculate the mass of the body based on attatched shapes. 
  34.    -- This gives realistic simulations. 
  35.    body:setMassFromShapes() 
  36.     
  37.    -- Set the collision callback. 
  38.    world:setCallback(collision) 
  39.  
  40. end 
  41.  
  42. function update(dt) 
  43.    -- Update the world. 
  44.    world:update(dt) 
  45. end 
  46.  
  47. function draw() 
  48.    -- Draws the ground. 
  49.    love.graphics.polygon(love.draw_line, ground_shape:getPoints()) 
  50.  
  51.    -- Draw the circle. 
  52.    love.graphics.draw(ball,body:getX(), body:getY(), body:getAngle()) 
  53.  
  54.    -- Draw text. 
  55.    love.graphics.draw(text, 5050
  56. end 
  57.  
  58. function keypressed(k) 
  59.    if k == love.key_space then 
  60.       -- Apply a random impulse 
  61.       body:applyImpulse(100000-math.random(0200000), 0
  62.    end 
  63. end 
  64.  
  65. -- This is called every time a collision occurs. 
  66. function collision(a, b, c) 
  67.     
  68.    local f, r = c:getFriction(), c:getRestitution() 
  69.    local s = c:getSeparation() 
  70.    local px, py = c:getPosition() 
  71.    local vx, vy = c:getVelocity() 
  72.    local nx, ny = c:getNormal() 
  73.  
  74.    text = "Last Collision:\n" 
  75.    text = text .. "Shapes: " .. a .. " and " .. b .. "\n" 
  76.    text = text .. "Position: " .. px .. "," .. py .. "\n" 
  77.    text = text .. "Velocity: " .. vx .. "," .. vy .. "\n" 
  78.    text = text .. "Normal: " .. nx .. "," .. ny .. "\n" 
  79.    text = text .. "Friction: " .. f .. "\n" 
  80.    text = text .. "Restitution: " .. r .. "\n" 
  81.    text = text .. "Separation: " .. s .. "\n" 
  82.  
  83. end 
Copyright © 2006-2008 LÖVE Development Team.