Collision no worky :(

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Doodee
Prole
Posts: 3
Joined: Wed Feb 24, 2010 11:31 am
Location: Dublin, Ireland
Contact:

Collision no worky :(

Post by Doodee »

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.

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
Any ideas?
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Collision no worky :(

Post by kikito »

This might be quite an stupid suggestion but... have you tried declaring the callbacks at the beginning, before love.load?
When I write def I mean function.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Collision no worky :(

Post by bartbes »

That is not required, what is required though is that you define them outside of your love.load function, I see no reason for doing what you are doing anyway.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Collision no worky :(

Post by kikito »

ha! I didn't see that one. You can close the love.load function just after this line:

Code: Select all

bodies.body2:setMassFromShapes()
And remove the "end" at the end. It will probably work.
When I write def I mean function.
User avatar
Doodee
Prole
Posts: 3
Joined: Wed Feb 24, 2010 11:31 am
Location: Dublin, Ireland
Contact:

Re: Collision no worky :(

Post by Doodee »

Thanks. I am a little more enlightened now :D


I think I'm getting somewhere now. It crashes now due to concatenating a nil value.
I'll keep at it though.
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: Collision no worky :(

Post by nevon »

Doodee wrote:I think I'm getting somewhere now. It crashes now due to concatenating a nil value.
I'll keep at it though.
You're trying to concatenate the uninitialized variable text with a in your world callbacks.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Collision no worky :(

Post by Robin »

A quick fix would be to replace it by:

Code: Select all

    text = (text or '')..a.." touching "..b.."\n"
(be sure to do that in every place)
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 3 guests