你好,我正在玩Lua/Love2D,我想知道是否可以同时使用网格和love.body。我想制作和测试网格,我还想创建一个love.physics.newWorld = love.body:applyForce来设置重力0,0,并使用world给它一种类似空间的感觉。
所以现在我有玩家
<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>然后在player类中
<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>发布于 2014-01-18 15:07:31
你绝对可以!
我感觉你想要的是展示一个受物理影响的身体(在这种情况下,是你的玩家精灵)?
如果是这样,在绘制四边形/网格时,只需在love.draw块中使用player.body.getY()和player.body.getY()即可。
例如:
function love.draw()
love.graphics.circle( "fill", player.body:getX(), player.body:getY(), 50, 100 )会在屏幕上画一个受物理引擎影响的球。
或者,您可能会询问有关世界坐标转换的问题。ie:您希望使用64,64,而不是使用BOX2D坐标,在这种情况下,请查看getWorldPoint方法:
https://stackoverflow.com/questions/19342479
复制相似问题