physics - Problem with body's second collision, weird angles

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
SimonSays
Prole
Posts: 2
Joined: Thu Jan 06, 2011 11:16 pm

physics - Problem with body's second collision, weird angles

Post by SimonSays »

Hello! I've looked around the forums and can't seem to find anyone else who's had this problem, so here goes nothing...

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
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: physics - Problem with body's second collision, weird an

Post by TechnoCat »

I'm not really sure, but if your bounce angles come out as the surface normal I might suspect friction is a suspect.
http://love2d.org/wiki/Shape:setFriction
SimonSays
Prole
Posts: 2
Joined: Thu Jan 06, 2011 11:16 pm

Re: physics - Problem with body's second collision, weird an

Post by SimonSays »

That was it, I set the Friction on everything to a low number, 0.01 for now, and everything works great. Thanks TechnoCat!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Semrush [Bot] and 6 guests