我目前正在尝试在LOVE2D中创建一个简单的2D汽车,但是在弄清楚如何将车轮关节连接到车身时遇到了问题。
https://love2d.org/wiki/love.physics.newWheelJoint
joint = love.physics.newWheelJoint( body1, body2, x, y, ax, ay, collideConnected )
要创建车轮关节,您需要传递4个数字(x,y,ax,ay),但是我不知道这些值应该是多少。每当我的汽车撞到地面上时,整个东西都会发疯并发动射击。
如下所示,我仅使用一个简单的PolygonShape
和两个CircleShapes
,并尝试附加它们。
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
答案 0 :(得分:2)
我不得不深入研究Box2D的文档,并且能够大致了解我要去哪里。我可以使用以下代码使其正常工作。
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
希望这可以帮助其他人寻找newWheelJoint
。