如何禁用 LOVE Physics for Lua 中某些物理体的重力?

我如何使用 LOVE Physics for Lua 禁用某些物理体的重力?

blocks.ground.body = love.physics.newBody(world, 0, blocks.ground.y, "dynamic")
blocks.ground.shape = love.physics.newRectangleShape(500, 50)
blocks.ground.fixture = love.physics.newFixture(blocks.ground.body, blocks.ground.shape)
blocks.ground.color = {86,176,0}

这是我当前的代码,我还需要它保持“动态”,因为我需要移动它的 X

查看完整代码: code

点赞
用户8150685
用户8150685

假设你正在使用 ** LÖVE 0.8.0+:**

选项 1:

你的代码:

blocks.ground.body = love.physics.newBody(world, 0, blocks.ground.y, "dynamic")
blocks.ground.shape = love.physics.newRectangleShape(500, 50)
blocks.ground.fixture = love.physics.newFixture(blocks.ground.body, blocks.ground.shape)
blocks.ground.color = {86,176,0}

注意: 在你的代码中 love.physics.newFixture(blocks.ground.body, blocks.ground.shape)

来自 LOVE 的网站(1):

objects.ground.fixture = love.physics.newFixture(objects.ground.body, objects.ground.shape) --attach shape to body

还来自 LOVE 的网站(2):

objects.ball.fixture = love.physics.newFixture(objects.ball.body, objects.ball.shape, 1) -- Attach fixture to body and give it a density of 1.

在他们网站的第二段代码中,他们将球的密度(_质量_)设置为 1。同样,您应该能够将质量设置为 0,这种情况下重力将不会对该对象产生影响。但是,如果其他质量为“0”的对象与您的对象发生碰撞,则我不确定会发生什么奇怪的动作。

选项 2:

另一种选择是创建具有重力为0的新世界:

love.physics.setMeter(64) --the height of a meter our worlds will be 64px
  worldNoGravity = love.physics.newWorld(0, 0, true)

然后将身体添加到该世界中:

blocks.ground.body = love.physics.newBody(worldNoGravity , 0, blocks.ground.y, "dynamic")
blocks.ground.shape = love.physics.newRectangleShape(500, 50)
blocks.ground.fixture = love.physics.newFixture(blocks.ground.body, blocks.ground.shape)
blocks.ground.color = {86,176,0}

希望其中之一适合您 :)。

2017-08-02 15:25:55