Ball Falls Through Paddle

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
joeknall
Prole
Posts: 2
Joined: Tue Oct 30, 2012 9:21 pm

Ball Falls Through Paddle

Post by joeknall »

Hey, everyone. I'm new to the forums and have been using LOVE for a couple of days now. The version I have is 0.8.0, and I'm attempting to make a game similar to breakout (the game that's kind of like single-player Pong but with a bunch of bricks that the ball breaks as it hits them) only with four paddles that slide along the edges of the screen following the cursor. That's more complicated than it sounds and I think you'll at least understand the paddle movement if you download and run the .LOVE file.

I have downward gravity enabled just so I can test the ball and make sure it bounces off the paddle properly, but it doesn't. It falls right through. I was hoping someone could help me out? ^^

Edit: I figured out part of the problem and changed it. I'm going to upload a second version, which is the version I'm working with now.
Attachments
Break Down 2.love
This is the current version.
(1.29 KiB) Downloaded 163 times
Break Down.love
Ignore this version.
(1.24 KiB) Downloaded 138 times
User avatar
paranoiax
Prole
Posts: 40
Joined: Thu Aug 16, 2012 10:27 am

Re: Ball Falls Through Paddle

Post by paranoiax »

Don't set the position on the shape (you already do so by setting up the body)

The code below should work as intended:

Code: Select all

function love.load()
	print("I'mma runnin mah gaem!")
	love.mouse.setVisible(false)
	love.graphics.setBackgroundColor(0,0,0)
	love.graphics.setColor(255,255,255)
	--Horizontal paddles
	paddleaw = 64
	paddleah = 16
	paddleax = (love.mouse.getX() - paddleaw/2)
	paddleay = 550
	paddlebw = 64
	paddlebh = 16
	paddlebx = (love.mouse.getX() - paddlebw/2)
	paddleby = 50

	--Vertical paddles
	paddlecw = 16
	paddlech = 64
	paddlecx = 50 + (paddlecw / 2) --added half of the width because box2d bodies are drawn from their center
	paddlecy = (love.mouse.getY() - paddlech/2)
	paddledw = 16
	paddledh = 64
	paddledx = 750 + (paddledw / 2) --added half of the width because box2d bodies are drawn from their center
	paddledy = (love.mouse.getY() - paddledh/2)

	--Physics
	love.physics.setMeter(64)
	world = love.physics.newWorld(0,100,false)
	objects = {}
	--That's a paddlin'!
	objects.paddlea = {}
	objects.paddlea.body = love.physics.newBody(world, paddleax,paddleay, "static")
	objects.paddlea.shape = love.physics.newRectangleShape(paddleaw,paddleah)
	objects.paddlea.fixture = love.physics.newFixture(objects.paddlea.body,objects.paddlea.shape,1)
	objects.paddleb = {}
	objects.paddleb.body = love.physics.newBody(world, paddlebx,paddleby, "static")
	objects.paddleb.shape = love.physics.newRectangleShape(paddlebw,paddlebh)
	objects.paddleb.fixture = love.physics.newFixture(objects.paddleb.body,objects.paddleb.shape,1)
	objects.paddlec = {}
	objects.paddlec.body = love.physics.newBody(world, paddlecx,paddlecy, "static")
	objects.paddlec.shape = love.physics.newRectangleShape(paddlecw,paddlech)
	objects.paddlec.fixture = love.physics.newFixture(objects.paddlec.body,objects.paddlec.shape,1)
	objects.paddled = {}
	objects.paddled.body = love.physics.newBody(world,paddledx,paddledy, "static")
	objects.paddled.shape = love.physics.newRectangleShape(paddledw,paddledh)
	objects.paddled.fixture = love.physics.newFixture(objects.paddled.body,objects.paddled.shape,1)	
	--Dropping the Ball
	objects.ball = {}
	objects.ball.body = love.physics.newBody(world,love.graphics.getWidth()/2,love.graphics.getHeight()/2, "dynamic")
	objects.ball.shape = love.physics.newCircleShape(8)
	objects.ball.fixture = love.physics.newFixture(objects.ball.body,objects.ball.shape,1)
	objects.ball.fixture:setRestitution(1)
	objects.ball.fixture:setFriction(0)
end

function love.draw()
	love.graphics.circle("fill", objects.ball.body:getX(), objects.ball.body:getY(), objects.ball.shape:getRadius()) --use a variable which allows for easier changes later on!
	--use polygons to draw rectangles!
	love.graphics.polygon("fill", objects.paddlea.body:getWorldPoints(objects.paddlea.shape:getPoints()))
	love.graphics.polygon("fill", objects.paddleb.body:getWorldPoints(objects.paddleb.shape:getPoints()))
	love.graphics.polygon("fill", objects.paddlec.body:getWorldPoints(objects.paddlec.shape:getPoints()))
	love.graphics.polygon("fill", objects.paddled.body:getWorldPoints(objects.paddled.shape:getPoints()))
end

function love.update(dt) --everything not related to "drawing stuff" should go here!
	objects.paddlea.body:setX(love.mouse.getX())
	objects.paddleb.body:setX(love.mouse.getX())
	objects.paddlec.body:setY(love.mouse.getY())
	objects.paddled.body:setY(love.mouse.getY())
	world:update(dt)

	if objects.paddlea.body:getX() > (702 + paddleaw / 2) then --no need to check for both paddles X-position as they are always the same
		objects.paddlea.body:setX(702+paddleaw / 2)
		objects.paddleb.body:setX(702+paddlebw / 2)
	end
	
	if objects.paddlea.body:getX() < (50 + paddleaw / 2) then --again, no need to check for both paddles X-position as they are always the same
		objects.paddlea.body:setX(50+paddleaw / 2)
		objects.paddleb.body:setX(50+paddlebw / 2)
	end
	
	if objects.paddlec.body:getY() > ((500 - paddlecw / 2) + paddlech / 2) then --subtracted half of the height because box2d bodies are drawn from their center
		objects.paddlec.body:setY((500 - paddlecw / 2) + paddlech / 2)
		objects.paddled.body:setY((500 - paddledw / 2) + paddledh / 2)
	end
	
	if objects.paddlec.body:getY() < (50 + paddlech / 2) then
		objects.paddlec.body:setY(50 + paddlech / 2)
		objects.paddled.body:setY(50 + paddledh / 2)
	end
	
	--Reset Ball Position
	if objects.ball.body:getX() < (0 - objects.ball.shape:getRadius()) or objects.ball.body:getX() > (love.graphics.getWidth() + objects.ball.shape:getRadius()) or objects.ball.body:getY() < (0 - objects.ball.shape:getRadius()) or objects.ball.body:getY() > (love.graphics.getHeight() + objects.ball.shape:getRadius()) then
		objects.ball.body:setPosition(love.graphics.getWidth() / 2, love.graphics.getHeight() / 2)
	end
end

function love.focus(bool)
end

function love.keypressed( key, unicode )
	--Stop doing things, game!
	if key == "escape" then
		love.event.quit()
	elseif key == " " then --Press Space to restart the game
		love.load()
	end
end

function love.keyreleased( key, unicode )
end

function love.mousepressed( x, y, button )
end

function love.mousereleased( x, y, button )
end

function love.quit()
end
Edit: and use love.graphics.polygon to draw a Rectangle so you don't have to mess with the offset. (box2d bodies are drawn from their center)

Edit2: updated the code with comments. Also the "boundaries" are now working properly.
joeknall
Prole
Posts: 2
Joined: Tue Oct 30, 2012 9:21 pm

Re: Ball Falls Through Paddle

Post by joeknall »

Thank you. :awesome:
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: Ball Falls Through Paddle

Post by T-Bone »

It seems a bit overkill to use love.physics for something like this...
User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Re: Ball Falls Through Paddle

Post by substitute541 »

T-Bone wrote:It seems a bit overkill to use love.physics for something like this...
Agreed. It's totally unnecessary to use love.physics in such a simple game.
Currently designing themes for WordPress.

Sometimes lurks around the forum.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 5 guests