[SOLVETH] Physics - can't set mass of a body.
Posted: Thu Jun 14, 2012 11:39 pm
Hello there.
I've been playing with the physics tutorial code, and I couldn't find a way to change the mass of a body. I've tried everything but the thing always stays the same, like if the masses are more or less equal. If i have a 5000kg ball and a 1kg brick, the brick won't stop the ball easily, i'm trying to reproduce something like this.
Here is the code:
Any clues?
Thank you guys.
I've been playing with the physics tutorial code, and I couldn't find a way to change the mass of a body. I've tried everything but the thing always stays the same, like if the masses are more or less equal. If i have a 5000kg ball and a 1kg brick, the brick won't stop the ball easily, i'm trying to reproduce something like this.
Here is the code:
Code: Select all
function love.load()
love.physics.setMeter(64) --the height of a meter our worlds will be 64px
world = love.physics.newWorld(0, 9.81*64, false) --create a world for the bodies to exist in with horizontal gravity of 0 and vertical gravity of 9.81
objects = {} -- table to hold all our physical objects
--let's create the ground
objects.ground = {}
objects.ground.body = love.physics.newBody(world, 650/2, 650-50/2) --remember, the shape (the rectangle we create next) anchors to the body from its center, so we have to move it to (650/2, 650-50/2)
objects.ground.shape = love.physics.newRectangleShape(650, 50) --make a rectangle with a width of 650 and a height of 50
objects.ground.fixture = love.physics.newFixture(objects.ground.body, objects.ground.shape); --attach shape to body
objects.ground1 = {}
objects.ground1.body = love.physics.newBody(world, 2, 2) --remember, the shape (the rectangle we create next) anchors to the body from its center, so we have to move it to (650/2, 650-50/2)
objects.ground1.shape = love.physics.newRectangleShape(50, 6500) --make a rectangle with a width of 650 and a height of 50
objects.ground1.fixture = love.physics.newFixture(objects.ground1.body, objects.ground1.shape); --attach shape to body
objects.ground1 = {}
objects.ground1.body = love.physics.newBody(world, 648, 2) --remember, the shape (the rectangle we create next) anchors to the body from its center, so we have to move it to (650/2, 650-50/2)
objects.ground1.shape = love.physics.newRectangleShape(50, 6500) --make a rectangle with a width of 650 and a height of 50
objects.ground1.fixture = love.physics.newFixture(objects.ground1.body, objects.ground1.shape); --attach shape to body
--let's create a ball
objects.ball = {}
objects.ball.body = love.physics.newBody(world, 650/2, 650/2, "dynamic") --place the body in the center of the world and make it dynamic, so it can move around
objects.ball.body:setMassData(1,1,1000,0) --give it a mass of 15
objects.ball.shape = love.physics.newCircleShape(20) --the ball's shape has a radius of 20
objects.ball.fixture = love.physics.newFixture(objects.ball.body, objects.ball.shape, 1) --attach shape to body and give it a friction of 1
objects.ball.fixture:setRestitution(0.0) --let the ball bounce
--let's create a couple blocks to play around with
objects.block1 = {}
objects.block1.body = love.physics.newBody(world, 200, 550, "dynamic")
objects.block1.body:setMassData(1,1,1,0) --uff, very heavy block at the bottom
objects.block1.shape = love.physics.newRectangleShape(0, 0, 50, 100)
objects.block1.fixture = love.physics.newFixture(objects.block1.body, objects.block1.shape, 5) --with plenty of friction, will be hard to move around
t={}
for i = -29, -27, 1 do
block2 = {}
block2.body = love.physics.newBody(world, 100, i*-16.5, "dynamic")
block2.body:setMassData(7,7,0.001,10)
block2.shape = love.physics.newRectangleShape(0, 0, 15, 15)
block2.fixture = love.physics.newFixture(block2.body, block2.shape, 2)
table.insert(t,block2)
end
--initial graphics setup
love.graphics.setBackgroundColor(104, 136, 248) --set the background color to a nice blue
love.graphics.setMode(650, 650, false, true, 0) --set the window dimensions to 650 by 650
end
function love.update(dt)
world:update(dt) --this puts the world into motion
--here we are going to create some keyboard events
if love.keyboard.isDown("right") then --press the right arrow key to push the ball to the right
objects.ball.body:applyForce(4000, 0)
elseif love.keyboard.isDown("left") then --press the left arrow key to push the ball to the left
objects.ball.body:applyForce(-4000, 0)
elseif love.keyboard.isDown("up") then --press the up arrow key to set the ball in the air
objects.ball.body:setPosition(650/2, 650/2)
end
end
function love.draw()
love.graphics.setColor(72, 160, 14) -- set the drawing color to green for the ground
love.graphics.polygon("fill", objects.ground.body:getWorldPoints(objects.ground.shape:getPoints())) -- draw a "filled in" polygon using the ground's coordinates
love.graphics.setColor(193, 47, 14) --set the drawing color to red for the ball
love.graphics.circle("fill", objects.ball.body:getX(), objects.ball.body:getY(), objects.ball.shape:getRadius())
love.graphics.setColor(50, 50, 50) -- set the drawing color to grey for the blocks
love.graphics.polygon("fill", objects.block1.body:getWorldPoints(objects.block1.shape:getPoints()))
for index,value in ipairs(t) do
love.graphics.polygon("fill", value.body:getWorldPoints(value.shape:getPoints()))
end
--love.graphics.polygon("fill", objects.block2.body:getWorldPoints(objects.block2.shape:getPoints()))
end
Thank you guys.