add gravity to my world and make an image jump.

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
goosingout
Prole
Posts: 7
Joined: Thu Feb 27, 2014 10:42 pm

add gravity to my world and make an image jump.

Post by goosingout »

my character curently runs left to right at a speed of 200. if ou hold down "right" it speed up to 400 and if you hold down ~"left is speed goes to 50. That and the background and world scrolling all work perfectly to the world.speed. I am trying to get my character to obay the laws of physics and gravity and be able to jump.

Code: Select all

loader = require("AdvTiledLoader.Loader")

function state:init( )
	windowx = love.graphics.getWidth()
	
	bg1 = {}
	bg1.img = love.graphics.newImage("/images/Citybackgroundnight.png")
	bg1.x = 0
	bg1.width = bg1.img:getWidth()

	bg2 = {}
	bg2.img = love.graphics.newImage("/images/Citybackgroundnight.png")
	bg2.x = windowx
	bg2.width = bg2.img:getWidth()

	ninja1 = love.graphics.newImage("/images/Character1.png")
	ninja2 = love.graphics.newImage("/images/Character2.png")
	ninja3 = love.graphics.newImage("/images/Character3.png")
	
	tx = 0
	ty = 0
	loader.path = "maps/"
	map = loader.load("level1.tmx")
	map:setDrawRange (tx,ty, map.width * map.tileWidth, map.height * map.tileHeight)

	world = 	{
			speed = 200,
			gravity = -400,
			}
	
	player = 	{
			x = 50,
			y = 300,
			y_vel = 0,
			speed = 200,
			jump_vel = -500,
			jumpheight = 300,
			grounded = false,
			}
		
end

Code: Select all

function state:update( dt )
	if dt > 0.05 then
		dt = 0.05
	end

	bg1.x = bg1.x - world.speed * dt
	bg2.x = bg2.x - world.speed * dt
	
	if bg1.x < -800 then
		bg1.x = bg1.x + bg1.width
	end
	if bg2.x < 0 then
		bg2.x = bg2.x + bg2.width
	end

	if love.keyboard.isDown("right") then
		world.speed = 400
	elseif love.keyboard.isDown("left") then
		world.speed = 50
	elseif love.keyboard.isDown("up") then
		player:jump()
	end
	
	tx = tx - (world.speed * dt)	
end

Code: Select all

function state:draw( )
	love.graphics.setColor(255,255,255,255)
	love.graphics.draw(bg1.img, bg1.x, 0)
	love.graphics.draw(bg2.img, bg2.x, 0)
	
	love.graphics.print("FPS: "..tostring(love.timer.getFPS( )), 10, 10)
	
	local ftx, fty = math.floor(tx), math.floor(ty)
	love.graphics.push()
	love.graphics.scale(scale)
	love.graphics.translate(ftx, fty)
	map:draw()
	love.graphics.pop()
	
	love.graphics.draw(ninja1, player.x, player.y,0,1,1,64,103 )
end
PS. I am using Hump, AdvTiledLoader, and .tmx tilemap
thebassinator143
Prole
Posts: 4
Joined: Sat Mar 01, 2014 10:06 pm

Re: add gravity to my world and make an image jump.

Post by thebassinator143 »

I'm sort of n00b but I'll try to help. :D

Anyways, think of gravity as constant downward force. When you jump, you are temporarily overcoming this force, until finally it brings you back down. So how to simulate this? Simply add the following code to your state.update function!

Code: Select all

player.y_vel = player.y_vel - (world.gravity*dt)
Now the world.gravity will subtract from the player's y_vel over each instance of dt until eventually your character begins to fall.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: add gravity to my world and make an image jump.

Post by micha »

To me it looks like you are moving the background and not the player. Is that correct? In that case I highly recommend moving the player instead.

The equations of movement are

Code: Select all

player.vx = player.vx + player.ax * dt
player.vy = player.vy + player.ay * dt
player.x = player.x + player.vx * dt
player.y = player.y + player.vy * dt
If you want to implement gravity, then assign a non-zero value to player.ay (ay stands for acceleration in y-direciton)
goosingout
Prole
Posts: 7
Joined: Thu Feb 27, 2014 10:42 pm

Re: add gravity to my world and make an image jump.

Post by goosingout »

@micha yeah the background and the map are moving. I tried to move the player but i need the background to scroll continuously. that why I tried it this way round.

@thebass i have tried that and it doesnt work for some reason

there must be a way to allow the one image to have gravity. and then in turn all it to be able to jump with the "up" button
User avatar
Autophoenix
Prole
Posts: 40
Joined: Thu Mar 06, 2014 4:18 pm
Location: Rio de Janeiro, Brazil

Re: add gravity to my world and make an image jump.

Post by Autophoenix »

Well, i have done this in a recent game mine:

In love.load() i put 2 variables. It controlls the jump time and if the player can jump

Code: Select all

jumpTime = 0
jump = true
Now, in love.update, I put two IF conditions and the variable 'jumpTime' will increase at every moment:

Code: Select all

jumpTime = jumpTime + 1

if love.keyboard.isDown("up") and jump == true then
    player.y = player.y - VELOCITY*dt
    if jumpTime > LIMIT_OF_JUMPING_TIME then -- Remeber to change the 'LIMIT_OF_JUMPING_TIME' to a number.
        jump = false
    end
elseif not love.keyboard.isDown("up") then
    jump = true
end
Remember to modify the code. Try to do something like 'if the player is at the ground then jump will be true'
a² = b² + c²
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: add gravity to my world and make an image jump.

Post by Robin »

You'd want to use:

Code: Select all

jumpTime = jumpTime + dt
Otherwise the time you can jump is framerate-dependent (and adjust LIMIT_OF_JUMPING_TIME accordingly).

Also, the code as-is is more flying than jumping, and you can get to any height by repeatedly holding the up arrow key.
Help us help you: attach a .love.
User avatar
Autophoenix
Prole
Posts: 40
Joined: Thu Mar 06, 2014 4:18 pm
Location: Rio de Janeiro, Brazil

Re: add gravity to my world and make an image jump.

Post by Autophoenix »

I know, and that's why i said:
Remember to modify the code. Try to do something like 'if the player is at the ground then jump will be true'
a² = b² + c²
Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests