Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lua 插图在Love2d相机移动中移动但对象静止_Lua_Game Engine_Love2d - Fatal编程技术网

Lua 插图在Love2d相机移动中移动但对象静止

Lua 插图在Love2d相机移动中移动但对象静止,lua,game-engine,love2d,Lua,Game Engine,Love2d,我正在尝试创建一个游戏,其中主要对象(摩托车)保持静止,但主要对象周围的世界移动(给相机移动的错觉)。我能够得到地面的插图(底部的绿色矩形)移动,但地面的物理对象保持静止。您可以在下图中看到,地面动画已移动,但摩托车仍停在其上 如果你把主对象向后退得足够远,你就会从屏幕的背面掉下来 我的代码在下面 梅因·卢阿 require("Camera") function love.load() love.physics.setMeter(64) --the height of a meter ou

我正在尝试创建一个游戏,其中主要对象(摩托车)保持静止,但主要对象周围的世界移动(给相机移动的错觉)。我能够得到地面的插图(底部的绿色矩形)移动,但地面的物理对象保持静止。您可以在下图中看到,地面动画已移动,但摩托车仍停在其上

如果你把主对象向后退得足够远,你就会从屏幕的背面掉下来

我的代码在下面

梅因·卢阿

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 = {
    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


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

您正在根据相机的偏移量绘制矩形。但在现实世界中,没有人知道您的相机。我看不到您一直在更改
x
地面坐标(这是静态的,可能不会让你这么做)或自行车的坐标

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


此外,如果你认为自行车会掉下来,你可能需要先设置世界重力。

我该如何实现这一点,我认为地面的x坐标没有改变,但我不知道为什么。