Page 1 of 1

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

Posted: Mon Nov 05, 2012 1:17 am
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

Re: Help With Gravity & A Jump Function?

Posted: Tue Nov 06, 2012 7:08 pm
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'
?

Re: Help With Gravity & A Jump Function?

Posted: Thu Nov 08, 2012 12:24 am
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:

Re: Help With Gravity & A Jump Function?

Posted: Thu Nov 08, 2012 6:03 pm
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..

Re: Help With Gravity & A Jump Function?

Posted: Fri Nov 09, 2012 12:35 am
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.

Re: Help With Gravity & A Jump Function?

Posted: Sat Nov 10, 2012 5:13 pm
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

Re: Help With Gravity & A Jump Function?

Posted: Sat Nov 10, 2012 9:03 pm
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

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

Posted: Sat Nov 10, 2012 10:53 pm
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