Collision no worky :(
Posted: Wed Apr 14, 2010 2:53 pm
Howdy,
Been mulling over posting this for a while as I had hoped to figure it out myself but so far I have had no luck.
I am trying to get collision detection working, I have tried a few different methods but resorted to a modification of the tutorial.
Any ideas?
Been mulling over posting this for a while as I had hoped to figure it out myself but so far I have had no luck.
I am trying to get collision detection working, I have tried a few different methods but resorted to a modification of the tutorial.
Code: Select all
function love.load()
world = love.physics.newWorld(2000, 1000) --Create a physical word
world:setGravity(0, 50) --Add the gravity
world:setCallbacks(add, persist, rem, result)
ground = love.physics.newBody (world, 0, 0, 0) --Add a body for the ground
ground_shape = love.physics.newRectangleShape(ground, 400, 590, 600, 10) -- Create the ground shape at (400,500) with size (600,10).
--love.graphics.setBackgroundColor(127, 195, 247)
bodies = {}
shapes = {}
bodies.body1 = love.physics.newBody(world, 700, 200) -- add a body for the ball
bodies.body2 = love.physics.newBody(world, 700, 400) -- add a body for the ball2
-- 100 units is roughly 1 meter (3 feet)
shapes.ball_1 = love.physics.newCircleShape(bodies.body1, 0,0, 20)
shapes.ball_1:setData("Ball1")
shapes.ball_2 = love.physics.newCircleShape(bodies.body2, 0, 0, 10.5)
shapes.ball_2:setData("Ball2")
bodies.body1:setMassFromShapes() -- Set the mass according to the shapes.
bodies.body2:setMassFromShapes()
function love.update(dt)
world:update(dt)
local speed_up = -2
if love.keyboard.isDown('up') then
local x, y = bodies.body1:getPosition()
local w,z = bodies.body1:getLinearVelocity()
bodies.body1:setLinearVelocity(w, z + speed_up)
end
if love.keyboard.isDown('down') then
local x, y = bodies.body1:getPosition()
local w,z = bodies.body1:getLinearVelocity()
-- local speed_up = -30
bodies.body1:setLinearVelocity(w, z - speed_up)
end
if love.keyboard.isDown('left') then
local x, y = bodies.body1:getPosition()
local w,z = bodies.body1:getLinearVelocity()
-- local speed_up = -30
bodies.body1:setLinearVelocity(w + speed_up, z)
end
if love.keyboard.isDown('right') then
local x, y = bodies.body1:getPosition()
local w,z = bodies.body1:getLinearVelocity()
-- local speed_up = -30
bodies.body1:setLinearVelocity(w - speed_up, z)
end
end
function love.draw()
love.graphics.setColor(22, 213, 58)
love.graphics.circle("fill", bodies.body1:getX(), bodies.body1:getY(), shapes.ball_1:getRadius(), 5000)
love.graphics.setColor(255,128,0)
love.graphics.circle("line", bodies.body1:getX(), bodies.body1:getY(), shapes.ball_1:getRadius())
love.graphics.setColor(222, 113, 58)
love.graphics.circle("fill", bodies.body2:getX(), bodies.body2:getY(), shapes.ball_2:getRadius(), 5000)
love.graphics.setColor(255,228,0)
love.graphics.circle("line", bodies.body2:getX(), bodies.body2:getY(), shapes.ball_2:getRadius())
love.graphics.print(text,0,12)
end
text = ""
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
end