love2d - love.physics/ love.body/ grid 爱2D - 爱物理/爱身体/网格

你好,我正在玩Lua/Love2D,我想知道能否同时使用网格和love.body。我想制作和测试网格,还想使用world = love.physics.newWorld将重力设置为0, 0,并使用love.body:applyForce使其具有类似太空的感觉。

所以现在我有玩家

<code>
  player = {}
    player.gridx = 64
player.gridy = 64
player.acty = 200
player.speed = 32
</code>

因为我正在使用网格,但我也想添加

  <code>
world = love.physics.newWorld(0, 0, true)

   In   love.load()
  </code>

然后在玩家类中

 <code>
 player = {}
  player.body = love.physics.newBody(world, 200, 550, "dynamic")
  player.body:setMass(100) -- make it pretty light
  player.shape = love.physics.newRectangleShape(0, 0, 30, 15)
  player.fixture = love.physics.newFixture(player.body, player.shape, 2)
  player.fixture:setRestitution(0.4)    -- make it bouncy
</code>

然后使用

<code>
 if love.keyboard.isDown("right") then
 player.body:applyForce(10, 0.0)
 print("moving right")
 elseif love.keyboard.isDown("left") then
 player.body:applyForce(-10, 0.0)
 print("moving left")
 end
if love.keyboard.isDown("up") then
player.body:applyForce(0, -500)
elseif love.keyboard.isDown("down") then
player.body:applyForce(0, 100)
end
</code>

而不是

<code>
  function love.keypressed(key)

  if key == "up" then
    if testMap(0, -1) then
        player.gridy = player.gridy - 32
    end
 elseif key == "down" then
    if testMap(0, 1) then
        player.gridy = player.gridy + 32
     end
   elseif key == "left" then
    if testMap(-1, 0) then
        player.gridx = player.gridx - 32
    end
  elseif key == "right" then
    if testMap(1, 0) then
        player.gridx = player.gridx + 32
    end
  end
 end
</code>
点赞
用户3209243
用户3209243

你绝对可以!

我感觉你想要展示一个受到物理效果影响的身体(在这种情况下是你的玩家精灵),是吗?

如果是这样,在你的 love.draw 块中使用 player.body.getY() 和 player.body.getY() 来绘制你的四边形/网格:

function love.draw()
     love.graphics.circle( "fill", player.body:getX(), player.body:getY(), 50, 100 )

会在屏幕上绘制一个受到物理引擎影响的球。

或者你可能想要进行世界坐标转换。例如:你想要使用 64, 64 而不是 BOX2D 的坐标,这种情况下可以使用 getWorldPoint 方法:

(https://www.love2d.org/wiki/Body:getWorldPoint)

2014-01-18 07:07:31