【问题标题】:Disable Gravity for certain physic bodies禁用某些物理实体的重力
【发布时间】:2017-08-03 00:56:16
【问题描述】:

如何使用 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

【问题讨论】:

  • 你能发布更多代码吗?比如你如何定义集合。
  • hastebin.com/ofusilozim.lua 我知道这不是最好看的代码,但这是我的第一个 lua 游戏之一,所以我还在学习
  • 没关系。它是一种有趣的学习语言。不过,能够查看完整的代码很有用。我建议将该行添加到您的问题中,例如 View Full Code: --> link
  • 请在问题中包含一个最小的可执行示例,而不是链接到外部网站。外部链接容易出现比特腐烂,将来可能无法使用。有关详细信息,请参阅 Meta 上的 this discussion。

标签: lua love2d


【解决方案1】:

假设您使用的是 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.

在他们网站的第二个 sn-p 中,他们将球的密度 (mass) 设置为 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}

希望其中一个对你有用:)。

【讨论】:

    猜你喜欢
    • 2018-06-20
    • 1970-01-01
    • 1970-01-01
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-18
    • 1970-01-01
    相关资源
    最近更新 更多