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.
[Solved] 0.6.1 Physics Collisions
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
[Solved] 0.6.1 Physics Collisions
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!
Re: 0.6.1 Physics Collisions
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.
Collision code taken whole-cloth from the 0.5.0 documentation.
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
We don't borrow, we don't read, we don't rent, we don't lease, we take the minds!
Re: 0.6.1 Physics Collisions
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
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!
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: 0.6.1 Physics Collisions
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).Xoria wrote:After someone answers his Q, can someone tell me how to implement Newtons Gravity law (Like, space) into this?
Help us help you: attach a .love.
Who is online
Users browsing this forum: No registered users and 4 guests