Help With Gravity & A Jump Function?(75% done vol jump :D)

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
User avatar
TheP3
Citizen
Posts: 62
Joined: Tue Dec 20, 2011 9:20 pm

Help With Gravity & A Jump Function?(75% done vol jump :D)

Post by TheP3 »

I made some basic collision detection, however, it's vary buggy... If anyone can help? Great! The way it works is it loads a collision map, black and white. Then, checks for every block if the player collides top, bottom, left, or right. I use a one pixel padding for detection. The core of it is in col.lua. You can look at the other code to if you want. It's all nicely documented.

The bugs are:
If i set the gravity(variable grav in player.lua) any higher, then the player falls into the block making it unable to move... :S
The above problem is what is getting me stuck, and stooping me form making my jump function... I also, would like to know if someone could help me make
a jump function "realistic" (meaning applies physics).

Also, sorry for being a dick and blocking out all the graphics... You can use the code tho

TheP3
Attachments
game.love
(11.38 KiB) Downloaded 408 times
Last edited by TheP3 on Sat Nov 10, 2012 10:52 pm, edited 2 times in total.
I write rules for my computer... I'm not the ruler of the world(yet)... Also, I livestream stuff http://NateAGeek.com/Live
User avatar
qaisjp
Party member
Posts: 490
Joined: Tue Sep 04, 2012 10:49 am
Location: United Kingdom
Contact:

Re: Help With Gravity & A Jump Function?

Post by qaisjp »

Code: Select all

Error: col.lua:15: Attempt to get out-of-range pixel!
stack traceback:
        [C]: in function 'getPixel'
        col.lua:15: in function 'load_ColMap'
        main.lua:15: in function 'load'
        [string "boot.lua"]:378: in function <[string "boot.lua"]:373>
        [C]: in function 'xpcall'
?
Lua is not an acronym.
User avatar
TheP3
Citizen
Posts: 62
Joined: Tue Dec 20, 2011 9:20 pm

Re: Help With Gravity & A Jump Function?

Post by TheP3 »

qaisjp wrote:

Code: Select all

Error: col.lua:15: Attempt to get out-of-range pixel!
stack traceback:
        [C]: in function 'getPixel'
        col.lua:15: in function 'load_ColMap'
        main.lua:15: in function 'load'
        [string "boot.lua"]:378: in function <[string "boot.lua"]:373>
        [C]: in function 'xpcall'
?
You stepped out of the map, I'm going to fix this soon... Just have got to write some code. hehe C:
I write rules for my computer... I'm not the ruler of the world(yet)... Also, I livestream stuff http://NateAGeek.com/Live
User avatar
qaisjp
Party member
Posts: 490
Joined: Tue Sep 04, 2012 10:49 am
Location: United Kingdom
Contact:

Re: Help With Gravity & A Jump Function?

Post by qaisjp »

TheP3 wrote:
qaisjp wrote:

Code: Select all

Error: col.lua:15: Attempt to get out-of-range pixel!
stack traceback:
        [C]: in function 'getPixel'
        col.lua:15: in function 'load_ColMap'
        main.lua:15: in function 'load'
        [string "boot.lua"]:378: in function <[string "boot.lua"]:373>
        [C]: in function 'xpcall'
?
You stepped out of the map, I'm going to fix this soon... Just have got to write some code. hehe C:
When I first start the game I got that..
Lua is not an acronym.
User avatar
YellowAfterlife
Prole
Posts: 29
Joined: Mon Jan 23, 2012 4:05 pm
Contact:

Re: Help With Gravity & A Jump Function?

Post by YellowAfterlife »

On-start error is caused by line 14 in col.lua.
It needs rounding, like:

Code: Select all

local r, g, b, a = ColMapSource:getPixel(math.floor(x/blockscale), math.floor(y/blockscale))
I'm currently tinkering with collision function. Will post results once done.
yal.cc
Contains things I work on. Also gets Love2d examples time to time.
User avatar
TheP3
Citizen
Posts: 62
Joined: Tue Dec 20, 2011 9:20 pm

Re: Help With Gravity & A Jump Function?

Post by TheP3 »

YellowAfterlife wrote:On-start error is caused by line 14 in col.lua.
It needs rounding, like:

Code: Select all

local r, g, b, a = ColMapSource:getPixel(math.floor(x/blockscale), math.floor(y/blockscale))
I'm currently tinkering with collision function. Will post results once done.
:D Thanx
I write rules for my computer... I'm not the ruler of the world(yet)... Also, I livestream stuff http://NateAGeek.com/Live
User avatar
TheP3
Citizen
Posts: 62
Joined: Tue Dec 20, 2011 9:20 pm

Re: Help With Gravity & A Jump Function?

Post by TheP3 »

YellowAfterlife wrote:On-start error is caused by line 14 in col.lua.
It needs rounding, like:

Code: Select all

local r, g, b, a = ColMapSource:getPixel(math.floor(x/blockscale), math.floor(y/blockscale))
I'm currently tinkering with collision function. Will post results once done.
I fixed the collision. Just made a conditional to test if the player is in the block push him out by 1px at a time. then, when he is out of the block set velocity to 0. Just really need a jump function....

Changes in player.lua

Code: Select all

require"col"

--Loads Image
ImgPlayer = love.graphics.newImage("GFX/Player/blob.png")
ImgPlayer:setFilter("nearest", "nearest")
--Player Image Quad
PlayerImgQ = {
	right = {
		Quad(0,  0, 32, 32, 128, 128);
		Quad(32, 0, 32, 32, 128, 128);
		Quad(64, 0, 32, 32, 128, 128);
		Quad(96, 0, 32, 32, 128, 128);
	},
	left = {
		Quad(0,  32, 32, 32, 128, 128);
		Quad(32, 32, 32, 32, 128, 128);
		Quad(64, 32, 32, 32, 128, 128);
		Quad(96, 32, 32, 32, 128, 128);
	},
	up = {
		Quad(0,  96, 32, 32, 128, 128);
		Quad(32, 96, 32, 32, 128, 128);
		Quad(64, 96, 32, 32, 128, 128);
		Quad(96, 96, 32, 32, 128, 128);
	},
	rest = {
		Quad(0,  64, 32, 32, 128, 128);
		Quad(32, 64, 32, 32, 128, 128);
		Quad(64, 64, 32, 32, 128, 128);
		Quad(96, 64, 32, 32, 128, 128);
	}
}

--Vars used
xspeed = 2
yspeed = 0
inair = 0
grav = 1
dir = "rest"
playerframe = 1
anispeed = 0.3
playertimer = 0
jumptime = 0
onground = false
isjumping = false
	
--Draws Player
function draw_player(px,py)
	
	love.graphics.drawq(ImgPlayer, PlayerImgQ[dir][playerframe], px - (32*2/2), py - (32*2/2), 0, 2, 2)
	
end

--Updates Player
function player_update(dt)

	--Sets the timer for frame
	if not love.keyboard.isDown() then
		playertimer = playertimer + dt
		if playertimer >= anispeed then
			playerframe = playerframe + 1
			if playerframe >= 4 then
				playerframe = 1
			end
			playertimer = 0
		end
	end
	
	if checkCol(Player, nil, "bottom") then
		Player.move(0, -grav)
		yspeed = 0
	end
	
	--If key is not down "rest"
	if not love.keyboard.isDown() then
		dir = "rest"
	end
	
	--Moves player up
	if love.keyboard.isDown('w') or love.keyboard.isDown('up') and checkCol(Player, nil, "top") == false then
		dir = "up"
		Player.move(0, -10)
	end
	print(jumptime)
	--Moves player left
	if love.keyboard.isDown('a') or love.keyboard.isDown('left') and checkCol(Player, nil, "left") == false then
		dir = "left"
		Player.move(-xspeed, 0)
	end
	
	--Checks in player hit the ground
	if checkCol(Player, nil, "bottom") == false then
		if yspeed + grav >= 5 then
			falling = true
		elseif yspeed + grav <= 5 then
			yspeed = yspeed + grav
			falling = false
		end
		Player.move(0, yspeed)
	end
	print(falling)
	--Moves player right
	if love.keyboard.isDown('d') or love.keyboard.isDown('right') and checkCol(Player, nil, "right") == false then
		dir = "right"
		Player.move(xspeed, 0)
	end
	
end
I write rules for my computer... I'm not the ruler of the world(yet)... Also, I livestream stuff http://NateAGeek.com/Live
User avatar
TheP3
Citizen
Posts: 62
Joined: Tue Dec 20, 2011 9:20 pm

Re: Help With Gravity & A Jump Function?(75% done vol jump :

Post by TheP3 »

Made a jump timer, however, now just need to add vol... Little stuck on this...

Code: Select all

require"col"

--Loads Image
ImgPlayer = love.graphics.newImage("GFX/Player/blob.png")
ImgPlayer:setFilter("nearest", "nearest")
--Player Image Quad
PlayerImgQ = {
	right = {
		Quad(0,  0, 32, 32, 128, 128);
		Quad(32, 0, 32, 32, 128, 128);
		Quad(64, 0, 32, 32, 128, 128);
		Quad(96, 0, 32, 32, 128, 128);
	},
	left = {
		Quad(0,  32, 32, 32, 128, 128);
		Quad(32, 32, 32, 32, 128, 128);
		Quad(64, 32, 32, 32, 128, 128);
		Quad(96, 32, 32, 32, 128, 128);
	},
	up = {
		Quad(0,  96, 32, 32, 128, 128);
		Quad(32, 96, 32, 32, 128, 128);
		Quad(64, 96, 32, 32, 128, 128);
		Quad(96, 96, 32, 32, 128, 128);
	},
	rest = {
		Quad(0,  64, 32, 32, 128, 128);
		Quad(32, 64, 32, 32, 128, 128);
		Quad(64, 64, 32, 32, 128, 128);
		Quad(96, 64, 32, 32, 128, 128);
	}
}

--Vars used
xspeed = 2
yspeed = 0
vol = 0
volinc = 0.7
inair = 0
grav = 1
dir = "rest"
playerframe = 1
anispeed = 0.3
playertimer = 0
jumptimemax = 0.5
jumptime = 0
onground = false
isjumping = false
	
--Draws Player
function draw_player(px,py)
	
	love.graphics.drawq(ImgPlayer, PlayerImgQ[dir][playerframe], px - (32*2/2), py - (32*2/2), 0, 2, 2)
	
end



--Updates Player
function player_update(dt)
	if dt > 0.3 then dt = 0.3 end
	--Sets the timer for frame
	if not love.keyboard.isDown() then
		playertimer = playertimer + dt
		if playertimer >= anispeed then
			playerframe = playerframe + 1
			if playerframe >= 4 then
				playerframe = 1
			end
			playertimer = 0
		end
	end
	
	if checkCol(Player, nil, "bottom") then
		Player.move(0, -grav)
		vol = 0
		yspeed = 0
		jumptime = 0
	end
	
	--If key is not down "rest"
	if not love.keyboard.isDown() then
		dir = "rest"
	end
	
	if checkCol(Player, nil, "top") then
		jumptime = jumptimemax + 1
	end
	--Moves player up
	if love.keyboard.isDown('w') or love.keyboard.isDown('up') and checkCol(Player, nil, "top") == false and jumptime <= jumptimemax then
		dir = "up"
		jumptime = jumptime + dt
		Player.move(0, -10)
	end
	print(jumptime)
	--Moves player left
	if love.keyboard.isDown('a') or love.keyboard.isDown('left') and checkCol(Player, nil, "left") == false then
		dir = "left"
		Player.move(-xspeed, 0)
	end
	
	--Checks in player hit the ground
	if checkCol(Player, nil, "bottom") == false then
		if yspeed + grav >= 5 then
			falling = true
			yspeed = yspeed - grav
		elseif yspeed + grav <= 5 then
			yspeed = yspeed + grav
			falling = false
		end
		Player.move(0, yspeed)
	end
	print(falling)
	print(vol)
	--Moves player right
	if love.keyboard.isDown('d') or love.keyboard.isDown('right') and checkCol(Player, nil, "right") == false then
		dir = "right"
		Player.move(xspeed, 0)
	end
	
end
I write rules for my computer... I'm not the ruler of the world(yet)... Also, I livestream stuff http://NateAGeek.com/Live
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 3 guests