I am starting out with my first love2d (using LöVE 0.8) physics program. I am just creating some ground/floor, a car body and attaching two wheels with revolute joints and setting the motors a-go.
However, the car stands still, whereas I expect it to move. Can someone glance over the code below and see what I am doing wrong?
UPDATE See below!
function love.load()
-- Set up world
love.physics.setMeter(50)
world = love.physics.newWorld(0, 9.81*50, true)
-- Window dimensions
windim = {height = love.graphics.getHeight(), width = love.graphics.getWidth()}
objects = {}
-- Ground/Floor
objects.ground = {}
objects.ground.body = love.physics.newBody(world, 300, windim.height)
objects.ground.shape = love.physics.newRectangleShape(2000,20)
objects.ground.fixture = love.physics.newFixture(objects.ground.body, objects.ground.shape)
objects.ground.fixture:setFriction(3)
-- Body of the car; a rectangle
objects.carbody = {}
objects.carbody.body = love.physics.newBody(world, 300, 500, "dynamic")
objects.carbody.shape = love.physics.newRectangleShape(100,40)
objects.carbody.fixture = love.physics.newFixture(objects.carbody.body, objects.carbody.shape)
objects.carbody.fixture:setFriction(3)
objects.carbody.fixture:setDensity(3)
objects.carbody.fixture:setRestitution(0)
-- First wheel
objects.wheel = {}
objects.wheel.body = love.physics.newBody(world, 330, 520, "dynamic")
objects.wheel.shape = love.physics.newCircleShape(20)
objects.wheel.fixture = love.physics.newFixture(objects.wheel.body, objects.wheel.shape)
objects.wheel.fixture:setFriction(3)
objects.wheel.fixture:setDensity(3)
objects.wheel.fixture:setRestitution(0.6)
-- Second wheel
objects.wheel2 = {}
objects.wheel2.body = love.physics.newBody(world, 270, 520, "dynamic")
objects.wheel2.shape = love.physics.newCircleShape(20)
objects.wheel2.fixture = love.physics.newFixture(objects.wheel2.body, objects.wheel2.shape)
objects.wheel2.fixture:setFriction(3)
objects.wheel2.fixture:setDensity(3)
objects.wheel2.fixture:setRestitution(0.6)
-- Create joint between car body and first wheel
objects.carbody.wheeljoint1 = love.physics.newRevoluteJoint(objects.carbody.body, objects.wheel.body, 330, 520)
objects.carbody.wheeljoint1:enableMotor()
objects.carbody.wheeljoint1:setMaxMotorTorque(10000) -- Following example from http://www.emanueleferonato.com/2011/08/22/step-by-step-creation-of-a-box2d-cartruck-with-motors-and-shocks/
-- Create joint between car body and second wheel
objects.carbody.wheeljoint2 = love.physics.newRevoluteJoint(objects.carbody.body, objects.wheel2.body, 270, 520)
objects.carbody.wheeljoint2:enableMotor()
objects.carbody.wheeljoint2:setMaxMotorTorque(10000)
end
function love.update(dt)
-- Set contstant speed for the wheels
objects.carbody.wheeljoint1:setMotorSpeed(20)
objects.carbody.wheeljoint2:setMotorSpeed(20)
world:update(dt)
end
function love.draw()
love.graphics.print(objects.carbody.wheeljoint1:getMotorSpeed(), 10, 10)
-- Render ground
love.graphics.setColor(72, 160, 14)
love.graphics.polygon("fill", objects.ground.body:getWorldPoints(objects.ground.shape:getPoints()))
-- Render car body
love.graphics.setColor(72, 160, 200)
love.graphics.polygon("fill", objects.carbody.body:getWorldPoints(objects.carbody.shape:getPoints()))
-- Render wheels
love.graphics.setColor(193, 47, 14)
love.graphics.circle("line", objects.wheel.body:getX(), objects.wheel.body:getY(), objects.wheel.shape:getRadius())
love.graphics.circle("line", objects.wheel2.body:getX(), objects.wheel2.body:getY(), objects.wheel2.shape:getRadius())
end
UPDATE I added a line to print the angle of one of the wheels (see below). When this line is present, suddenly the car flips over from the wheels spinning when it hits the ground. This leads me to believe that something did not get initialized properly first time around.
This is the line added:
function love.draw()
love.graphics.print(objects.wheel.body:getAngle(), 10, 10) -- Line added
love.physicsuntil they're comfortable with the rest of love2d, but you clearly know what you're doing. :D \$\endgroup\$