2013-10-13 15 views
0

你好我正在玩Lua/Love2D,我想知道是否可以一起使用網格和love.body。我想製作和測試網格,並且我還想創建一個world = love.physics.newWorld來設置引力0,0並使用love.body:applyForce爲它提供一個類似感覺的空間。love2d - love.physics/love.body/grid

所以現在我有玩家

<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> 
+0

這不是我清楚你不能做什麼。你是說你想使用Box2d物理,但只能進入謹慎的網格位置?或者是其他東西 – Alex

回答

1

你絕對可以!

我覺得你想要的是展示受物理影響的物體(在這種情況下,你的玩家精靈)?

如果是這樣,在您的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