I'm making a pretty simple top-down physics game where you flick a small ball (or puck) around, bouncing it off walls and around corners, etc.
Everything works great, but if my Puck bounces off a second wall, it loses all of its inertia, either vertical or horizontal (never both). For example, let's say the ball is bouncing off the wall on the bottom of the screen and comes up to hit the right side. After this second collision (with the right side of the screen) it will lose all Y momentum and just slide straight to the left along the X axis.
I'm using the love.physics stuff, rectangles and a circle.
Here's my code in it's entirety, I'd appreciate any help you could lend me. You'll need a "puck.png" in your main.lua's folder for this to work.
Thanks again!
Code: Select all
-- Simon's Puckbots game. -
----- ------------------------------------------------------------
function love.load()
love.graphics.setCaption( "Puckbots! (Press ESC to quit)" );
love.graphics.setFont(12);
love.mouse.setGrab( true ) ;
currentMousePositionX = nil;
currentMousePositionY = nil;
PUCKSTARTX = 400;
PUCKSTARTY = 300;
DAMPING = 1; -- Dictates how quickly the puck slows down.
BOUNCINESS = 0.1; -- Bounciness of the puck
WALLBOUNCE = 0.2; -- Bounciness of the walls
PUCKRADIUS = 25;
PUCKMASS = 20;
puck = love.graphics.newImage("puck.png");
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
-- New physics and collision stuff -- -- -- -- -- -- -- -- -- -- --
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
world = love.physics.newWorld(0, 0, 800, 600, 0, 0, false);
world:setGravity(0, 0);
world:setMeter(65);
boxBodies = {}
boxShapes = {}
-- love.physics.newBody( which world it's in, xPos, yPos, mass, rotational inertia)
boxBodies[1] = love.physics.newBody(world, 400, 0, 0, 0); -- roof
boxBodies[2] = love.physics.newBody(world, 400, 600, 0, 0); -- floor
boxBodies[3] = love.physics.newBody(world, 0, 300, 0, 0); -- left wall
boxBodies[4] = love.physics.newBody(world, 800, 300, 0, 0); -- right wall
boxBodies[5] = love.physics.newBody(world, 400, 0, 0, 0); -- middle wall
-- love.physics.newRectangleShape(body to attach to, x-offset, y-offset, width, height, angle)
boxShapes[1] = love.physics.newRectangleShape(boxBodies[1], 0, 0, 800, 10, 0); -- roof
boxShapes[2] = love.physics.newRectangleShape(boxBodies[2], 0, 0, 800, 20, 0); -- floor
boxShapes[3] = love.physics.newRectangleShape(boxBodies[3], 0, 0, 20, 600, 0); -- left wall
boxShapes[4] = love.physics.newRectangleShape(boxBodies[4], 0, 0, 20, 600, 0); -- right wall
boxShapes[5] = love.physics.newRectangleShape(boxBodies[5], 0, 0, 200, 200, 0); -- middle wall
puckBodies = {}
puckShapes = {}
puckBodies[1] = love.physics.newBody(world, PUCKSTARTX, PUCKSTARTY, PUCKMASS, 0);
puckShapes[1] = love.physics.newCircleShape(puckBodies[1], 0, 0, PUCKRADIUS);
boxShapes[1]:setRestitution(WALLBOUNCE);
boxShapes[2]:setRestitution(WALLBOUNCE);
boxShapes[3]:setRestitution(WALLBOUNCE);
boxShapes[4]:setRestitution(WALLBOUNCE);
puckBodies[1]:setLinearDamping(DAMPING);
puckShapes[1]:setRestitution(BOUNCINESS);
end
function DrawBoxes()
if #boxShapes > 0 then
for index, object in ipairs(boxShapes) do -- this returns the corner coords of my SHAPES
local x1, y1, x2, y2, x3, y3, x4, y4 = object:getBoundingBox() -- gets x y coords of all 4 corners of the box
boxwidth = x3- x2;
boxheight = y2 - y1;
love.graphics.rectangle("fill", x1, y1, boxwidth, boxheight);
end
end
end
function DrawPuck()
if #puckBodies > 0 then
testTrace = true;
for index, object in ipairs(puckBodies) do
local x, y = object:getPosition();
love.graphics.draw(puck, x, y, 0, 1, 1, 25, 25);
end
end
end
function ShowMousePosition()
local x, y = love.mouse.getPosition();
currentMousePositionX = x;
currentMousePositionY = y;
end
function love.update (dt)
world:update(dt);
ShowMousePosition();
if love.keyboard.isDown("up") then
puckBodies[1]:applyForce(0, -800, 20, 20);
end
if love.keyboard.isDown("down") then
puckBodies[1]:applyForce(0, 800);
end
if love.keyboard.isDown("left") then
puckBodies[1]:applyForce(-800, 0);
end
if love.keyboard.isDown("right") then
puckBodies[1]:applyForce(800, 0);
end
if love.keyboard.isDown("escape") then --- Closes the game if ESC is pressed
love.event.push("q")
end
end
function love.draw()
DrawBoxes();
DrawPuck();
-------------------------------------------------------------------------------------
----DEBUG STUFF ---------------------------------------------------------------------
--Shows current mouse position
--love.graphics.print(("Current mouse position is "..currentMousePositionX..", "..currentMousePositionY.."."), 12, 40);
--Kindly reminders
love.graphics.print(("Use the arrow keys to apply force."), 12, 20);
love.graphics.print(("Press ESC to quit."), 680, 20);
---/DEBUG STUFF ---------------------------------------------------------------------
-------------------------------------------------------------------------------------
end