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.