插图移动但物体静止在Love2d相机移动中

时间:2018-05-29 16:21:33

标签: lua game-engine love2d

我正在尝试创建一个游戏,主要对象(摩托车)保持静止,但主要对象周围的世界移动(给出相机移动的幻觉。我能够得到地面的图示(绿色矩形)在底部移动,但地面的物体保持静止。你可以在下面的图片中看到地面的动画已经移动,但摩托车仍然在它上面休息。enter image description here

如果您将主要物体放回到足够的位置,则会从屏幕的背面掉下来。

我的代码在

下面

main.lua

require("Camera")

function love.load()
  love.physics.setMeter(64) --the height of a meter our worlds will be 64px
  world = love.physics.newWorld(0, 9.81*64, true) --create a world for the bodies to exist in with horizontal gravity of 0 and vertical gravity of 9.81

  objects = {} -- table to hold all our physical objects

  objects.ground = {}
  ground = {}
  ground.x = 1700/2 + Camera.x
  ground.y = 1000-50 + Camera.y
  ground.width = 1700
  ground.height = 50
  ground.body = love.physics.newBody(world, ground.x, 1000-50/2, "static") --remember, the shape (the rectangle we create next) anchors to the body from its center, so we have to move it to (1700/2, 1000-50/2)
  ground.shape = love.physics.newRectangleShape(ground.width, ground.height) --make a rectangle with a width of 1700 and a height of 50
  ground.fixture = love.physics.newFixture(ground.body, ground.shape, 1); --attach shape to body, give it a density of 1. 
  table.insert(objects.ground, ground)

--creating the motorcycle
  objects.ball = {}  
  objects.ball.x = 1700/2
  objects.ball.y = 1000/2
  objects.ball.width = 100
  objects.ball.height = 50
  objects.ball.body = love.physics.newBody(world, objects.ball.x, objects.ball.y, "dynamic") --place the body in the center of the world and make it dynamic, so it can move around
  objects.ball.shape = love.physics.newRectangleShape(objects.ball.width, objects.ball.height) --the ball's shape has a radius of 20
  objects.ball.fixture = love.physics.newFixture(objects.ball.body, objects.ball.shape, 1) -- Attach fixture to body and give it a density of 1.
  objects.ball.img = love.graphics.newImage('images/motorcycle.png')

  --initial graphics setup
  love.graphics.setBackgroundColor(0.41, 0.53, 0.97) --set the background color to a nice blue
  love.window.setMode(1700, 1000) --set the window dimensions to 650 by 650
end

function love.update(dt)
  world:update(dt) --this puts the world into motion
  Camera.update(dt) 
end

function love.draw()
  love.graphics.setColor(0.28, 0.63, 0.05) -- set the drawing color to green for the ground

  for _, groundPiece in pairs(objects.ground) do
    love.graphics.rectangle("fill", groundPiece.x - 850 - Camera.x, groundPiece.y - Camera.y, groundPiece.width, groundPiece.height) -- draw a "filled in" polygon using the ground's coordinates
  end

  love.graphics.draw(objects.ball.img, objects.ball.body:getX(), objects.ball.body:getY()-(objects.ball.height/objects.ball.img:getHeight())/2, objects.ball.body:getAngle(), objects.ball.height/objects.ball.img:getHeight())

end

Camera.lua

Camera = {
    x = 0,
    y = 0
}

function Camera.update(dt)

    if love.keyboard.isDown("right") then --RIGHT ARROW BUTTON IS DOWN then
        Camera.x = Camera.x + 5
    elseif love.keyboard.isDown("left") then
        Camera.x = Camera.x - 5
    end

    if love.keyboard.isDown("up") then
        Camera.y = Camera.y - 5
    elseif love.keyboard.isDown("down") then
        Camera.y = Camera.y + 5
    end

end

我有一个类似的项目here,我可以在屏幕上绘制线条并使其随着相机移动而移动。在这个新项目中,我不明白我在做什么。

1 个答案:

答案 0 :(得分:0)

您正在根据相机的偏移绘制矩形。但物理世界中没有任何东西能够了解你的相机。我没有看到你改变地面的x坐标(这是静态的,可能不会让你那么做)或自行车。

因此,对于物理引擎,自行车和地面的相对位置根本不会改变。

此外,如果您希望自行车脱落,您可能需要先设置世界重力。