[Solved] 0.6.1 Physics Collisions

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
Xcmd
Party member
Posts: 211
Joined: Fri Feb 13, 2009 10:45 pm

[Solved] 0.6.1 Physics Collisions

Post by Xcmd »

So as ever I'm toying with the physics engine. I've got to say that I'm happy it seems to run a lot faster these days, and certain things seem to function correctly. But along with the changes have come a new way of doing things, and that takes time to learn and the documentation isn't as filled with code-snippets as it used to be.

How do I handle collisions, now? I think it has something to do with World:setCallbacks() but I'm not really fully understanding it and it's something that's important. I also think I need to use the setData() feature to give each object a name... but then I'm lost from there.
Last edited by Xcmd on Tue Feb 23, 2010 4:38 am, edited 1 time in total.
We don't borrow, we don't read, we don't rent, we don't lease, we take the minds!
User avatar
Xcmd
Party member
Posts: 211
Joined: Fri Feb 13, 2009 10:45 pm

Re: 0.6.1 Physics Collisions

Post by Xcmd »

Collisions remain unchanged from version 0.5.0, you just have more types now. You now have "add", "persistent" and "remove", as well as "result" which appears (as the 0.6.1 documentation states) to do absolutely nothing. Here.

Code: Select all

-- Grabbity by Xcmd
math.randomseed( os.time() )

function love.load()
	myWorld = love.physics.newWorld(800, 600)
	myWorld:setGravity(0, 15)
	myWorld:setCallbacks( acol, pcol, rcol, rtcol )
	
	myBallBody = love.physics.newBody( myWorld, 300, 400 )
	myBallShape = love.physics.newCircleShape( myBallBody, 0, 0, 16 )
	myBallBody:setMass(0,0,1,0)
	myBallShape:setData("ball")
	
	myWinBody = love.physics.newBody( myWorld, math.floor(math.random(0, 800)), math.floor(math.random(0, 600))  )
	myWinShape = love.physics.newRectangleShape( myWinBody, 0, 0, 16, 16, 0 )
	myWinShape:setData("win")
	
	text = ""
end

function love.update( dt )
	myWorld:update( dt )
end

function love.draw()
	love.graphics.circle("line", myBallBody:getX(), myBallBody:getY(), myBallShape:getRadius())
	love.graphics.polygon("fill", myWinShape:getPoints())
	love.graphics.print( text, 5, 15 )
end

function love.keypressed( key )
	if key == "up" then
		myWorld:setGravity(0, -15)
	elseif key == "down" then
		myWorld:setGravity(0, 15)
	elseif key == "left" then
		myWorld:setGravity(-15, 0)
	elseif key == "right" then
		myWorld:setGravity(15, 0)
	end
	
	if key == "r" then
		love.load()
	end
end

function acol( a, b, c )
	coll( a, b, c, "Add" )
end

function pcol( a, b, c )
	coll( a, b, c, "Persistent" )
end

function rcol( a, b, c )
	coll( a, b, c, "Remove" )
end

function rtcol( a, b, c )
	coll( a, b, c, "Result" )
end

function coll( a, b, c, ctype )
	local f, r = c:getFriction(), c:getRestitution() 
	local s = c:getSeparation() 
	local px, py = c:getPosition() 
	local vx, vy = c:getVelocity() 
	local nx, ny = c:getNormal()
	
	text = ctype .. " Collision:\n" 
	text = text .. "Shapes: " .. a .. " and " .. b .. "\n" 
	text = text .. "Position: " .. px .. "," .. py .. "\n" 
	text = text .. "Velocity: " .. vx .. "," .. vy .. "\n" 
	text = text .. "Normal: " .. nx .. "," .. ny .. "\n" 
	text = text .. "Friction: " .. f .. "\n" 
	text = text .. "Restitution: " .. r .. "\n" 
	text = text .. "Separation: " .. s .. "\n"
end
Collision code taken whole-cloth from the 0.5.0 documentation.
We don't borrow, we don't read, we don't rent, we don't lease, we take the minds!
Xoria
Citizen
Posts: 61
Joined: Sun Jan 31, 2010 1:24 am

Re: 0.6.1 Physics Collisions

Post by Xoria »

After someone answers his Q, can someone tell me how to implement Newtons Gravity law (Like, space) into this?
User avatar
Xcmd
Party member
Posts: 211
Joined: Fri Feb 13, 2009 10:45 pm

Re: 0.6.1 Physics Collisions

Post by Xcmd »

I answered my own question. You might want to go ahead and make a whole new topic so your question doesn't get buried with this one.
We don't borrow, we don't read, we don't rent, we don't lease, we take the minds!
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: 0.6.1 Physics Collisions

Post by Robin »

Xoria wrote:After someone answers his Q, can someone tell me how to implement Newtons Gravity law (Like, space) into this?
You don't. You roll your own. Box2D works best for surface-of-the-earth simulations (e.g. you can only have gravity in one direction).
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: slime and 40 guests