Reputation: 5988
I'm currently trying to create a simple 2D car in LOVE2D but I'm having problems figuring out how to attach a wheel joint to the body.
https://love2d.org/wiki/love.physics.newWheelJoint
joint = love.physics.newWheelJoint( body1, body2, x, y, ax, ay, collideConnected )
To create a wheel joint, you pass 4 numbers ( x, y, ax, ay ), but I don't know what those values should be. Every time my car hits the ground, the entire thing freaks out and shoots off.
As you can see below, I'm just using a simple PolygonShape
, and two CircleShapes
and I'm trying to attach them.
function Car:create()
local object = {}
local x = 300
local y = 300
local width = 120
local height = 40
local size = 20
local hasCollision = false
object.frame = Car:createFrame(x, y, width, height)
object.frontWheel = Car:createWheel(x + width/4, y + height, size)
object.backWheel = Car:createWheel(x - width/4, y + height, size)
-- Unsure what values I should use for these.
frontJoint = love.physics.newWheelJoint(object.frame.body, object.frontWheel.body, 400, 300, 400, 300, hasCollision)
rearJoint = love.physics.newWheelJoint(object.frame.body, object.backWheel.body, 350, 300, 0, 0, hasCollision)
-- Trying to adjust the values to see if that helps, unsure what these should be.
frontJoint:setSpringFrequency(1)
frontJoint:setSpringDampingRatio(2)
return object
end
function Car:createFrame(x, y, width, height)
local frame = {}
frame.body = love.physics.newBody(world, x, y, "dynamic")
frame.shape = love.physics.newRectangleShape(0, 0, width, height)
frame.fixture = love.physics.newFixture(frame.body, frame.shape, 5) -- A higher density gives it more mass.
return frame
end
function Car:createWheel(x, y, size)
local wheel = {}
wheel.body = love.physics.newBody(world, x, y, "dynamic")
wheel.shape = love.physics.newCircleShape(size)
wheel.fixture = love.physics.newFixture(wheel.body, wheel.shape, 1)
wheel.fixture:setRestitution(0.25)
return wheel
end
Upvotes: 1
Views: 350
Reputation: 5988
I had to look more into Box2D's documentation and was able to get an idea of where I was going wrong. I was able to get it working using the follow code.
function Car:createJoint(frame, wheel, motorSpeed, maxMotorTorque)
local hasCollision = false
local freq = 7
local ratio = .5
local hasChanged = true
local yOffset = wheel:getY() - frame:getY()
local joint = love.physics.newWheelJoint(frame, wheel, wheel:getX(), wheel:getY(), 0, 1, hasCollision)
joint:setSpringFrequency(freq)
joint:setSpringDampingRatio(ratio)
joint:setMotorSpeed(motorSpeed)
joint:setMaxMotorTorque(maxMotorTorque)
return joint
end
Hopefully this helps someone else looking for newWheelJoint
.
Upvotes: 2