Page 1 of 1

[Solved] 0.6.1 Physics Collisions

Posted: Sun Feb 21, 2010 3:45 pm
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.

Re: 0.6.1 Physics Collisions

Posted: Sun Feb 21, 2010 10:41 pm
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.

Re: 0.6.1 Physics Collisions

Posted: Mon Feb 22, 2010 10:48 pm
by Xoria
After someone answers his Q, can someone tell me how to implement Newtons Gravity law (Like, space) into this?

Re: 0.6.1 Physics Collisions

Posted: Tue Feb 23, 2010 4:38 am
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.

Re: 0.6.1 Physics Collisions

Posted: Tue Feb 23, 2010 7:02 am
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).