I am trying to create a game where the main object(a motorcycle) stays stationary but the world around the main object moves (giving the illusion of camera movement. I am able to get the illustration of the ground(the green rectangle at the bottom) to move, but the physical object of the ground remains stationary. You can see in the picture below that the animation of the ground has moved but the motorcycle is still rested on top of it.
My code is below
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)
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
objects.ball.body:getY(), objects.ball.shape:getRadius())
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
I have a similar project at https://git.cs.usask.ca/sjg970/Line_rider_love_2d.git where I was able to draw lines on the screen and make them move with Camera movements. I don't understand what I am doing differently in this new project.
If you back the main object back enough, you fall off the back side of the screen.Illustration moving but object stationary in Love2d Camera movements
-
- Prole
- Posts: 1
- Joined: Tue May 29, 2018 5:21 pm
- yetneverdone
- Party member
- Posts: 448
- Joined: Sat Sep 24, 2016 11:20 am
- Contact:
Re: Illustration moving but object stationary in Love2d Camera movements
Please wrap your code so everyone can read it properly and easily
My GameDev Website
Going Home:A Pixelated Horror Game
My Repositories!
Follow me lovingly!
Nga pala, pinoy ako.
Going Home:A Pixelated Horror Game
My Repositories!
Follow me lovingly!
Nga pala, pinoy ako.
-
- Party member
- Posts: 234
- Joined: Mon Aug 29, 2016 8:51 am
Re: Illustration moving but object stationary in Love2d Camera movements
I'm kinda confused. The motorcycle is stationary in the center and the world around it should 'move' so that it seems as the camera is following the motorcycle.
But with a green rectangle as the ground you can't possibly convey the feeling of movement. It'd be noticable if it were light green -> dark green -> light green etc.
I read it several times but I'm still not sure what you're trying to fix.
Also wrap your code in [ code ] [\ code ] (without spaces)
But with a green rectangle as the ground you can't possibly convey the feeling of movement. It'd be noticable if it were light green -> dark green -> light green etc.
I read it several times but I'm still not sure what you're trying to fix.
Also wrap your code in [ code ] [\ code ] (without spaces)
Who is online
Users browsing this forum: Google [Bot] and 2 guests