hit boxes not working :(

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
thewifitree
Prole
Posts: 32
Joined: Sat Dec 21, 2013 5:16 pm

hit boxes not working :(

Post by thewifitree »

I watched youtube videos and tried checkcollision and hitboxes aren't working :( My little brother wants me to make him a video game for Christmas but the spikes in the game have hitboxes that are way too big. please help! I don't know what size to make them! the function is called at 214 and is defined in commands.lua
Attachments
jewel.love
The game is kind of wierd
(204.39 KiB) Downloaded 210 times
User avatar
BozoDel
Party member
Posts: 164
Joined: Fri Nov 01, 2013 10:28 pm

Re: hit boxes not working :(

Post by BozoDel »

Hi, welcome to the forums!

I see your code is very well commented, I like that :megagrin:

The reason why you're getting a "hit" every frame is that you're calling the collision detection the wrong way:

Code: Select all

	--spike hit box
	insidebox(playerx, playery, spikex, spikey, 45, 51)
	if insidebox then
		print("hit")
	end
What you're doing there is
1- calling the insidebox function, but it doesn't really do anything, because there's nothing receiving what it returns
2- checking if insidebox exists. Well, insidebox exists, so hit hit hit hit hit hit hit...

What you want to do is:

Code: Select all

	--spike hit box
	if insidebox(playerx, playery, spikex, spikey, 45, 51) then
		print("hit")
	end
Now the the "if" is checking insidebox's result, not insidebox itself.

But the hit detection is still weird, and this is why:

Code: Select all

	--draw the spike
	for i=1,spiketotal do	
		love.graphics.setColor(255, 255, 255, 255)
		love.graphics.draw(spike, spikex+i*102, spikey)
	end
You're drawing the spike in one place, different from the place where it really is. You should change the spikex somewhere else, not misplace it in the draw function. This would be correct:

Code: Select all

	--draw the spike
	for i=1,spiketotal do	
		love.graphics.setColor(255, 255, 255, 255)
		love.graphics.draw(spike, spikex, spikey)
	end
And now the collision is making some sense, but there's still one problem. You're only detecting collision for the player's origin (its uppermost left corner, in this case). Let's change the insidebox function so that it detects collision for all four corners of the player:

Code: Select all

function insidebox(px, py, x, y, wx, wy)
	local playerwidth = 53
	local playerheight = 60
	if (px > x and px < x + wx)
	or (px + playerwidth> x and px + playerwidth < x + wx) then
		if (py > y and py < y + wy)
		or (py + playerheight > y and py + playerheight < y + wy) then
			return true
		end
	end
	return false
end
That's much better! Now you might think the hitbox might be a bit large... It is advised for platformers and stuff to keep hitboxes of things that hurt the player a bit smaller than the sprite size. I think you can try to tweak that yourself, but feel free to come back here if you have any more problems.
thewifitree
Prole
Posts: 32
Joined: Sat Dec 21, 2013 5:16 pm

Re: hit boxes not working :(

Post by thewifitree »

Thanks! very helpful, but is there a way to draw multiple spikes with hitboxes by using a for loop?
User avatar
BozoDel
Party member
Posts: 164
Joined: Fri Nov 01, 2013 10:28 pm

Re: hit boxes not working :(

Post by BozoDel »

Yes, of course. You'd have to create the spike objects first, let's see. In level1.lua you have:

Code: Select all

	--spike position and other data
	spiketotal = 1
	spikex = keyx-120
	spikey = tiley
That's just one. We should make a table with all spikes in it:

Code: Select all

	--spike position and other data
	spikes = {} --this creates the table
	spikes[1] = {} --this creates a table inside that table, but we give it a number instead of a name. that's our first spike
	spikes[1].x = 400
	spikes[1].y = 0 --those are the x and y of spike 1
	spikes[2] = {} --and so on
	spikes[2].x = 460
	spikes[2].y = 60 --EDIT: THIS WAS WRONG BUT NOW I FIXED IT
This one has just two spikes, but you can have more if you want. And there are probably more elegant ways to write this, but it depends on what you have in mind.

Notice this:
When we put a variable inside a table, we write it like this: table.variable
But when that variable has a number instead of a name, we do it like this: table[1]

Now, in main.lua you already have a way of drawing the spikes:

Code: Select all

	for i=1,spiketotal do	
		love.graphics.setColor(255, 255, 255, 255)
		love.graphics.draw(spike, spikex, spikey)
	end
We'll just have to change a few thing accordingly:

Code: Select all

	love.graphics.setColor (255, 255, 255, 255)
	for i, v in ipairs (spikes) do
		love.graphics.draw (spike, spikes[i].x, spikes[i].y
	end
We put the setColor outside of the for loop, as calling it once is enough. The ipairs thingie goes through all numbered variables in the spikes table, and "i" will be the index of each variable.

We've managed to create and draw the different spikes (just two in this case, but could be more), now we need to make the collision detection work with all spikes. I think you should try to do this yourself, based on what we've seen here (but you can come back if you still have any doubts).

There's one thing I must warn you of though. If you have few spikes on the level, it's ok to check for collisions with each individually. But if there are many of them, things can get slow. I know that there are more efficient ways to deal with collision, but I don't know them yet... so I advise you to stick to few spikes.

Oh, I almost forgot, you're gonna have to change the spikes' movement in love.update too, of course.
Last edited by BozoDel on Mon Dec 23, 2013 1:17 pm, edited 1 time in total.
thewifitree
Prole
Posts: 32
Joined: Sat Dec 21, 2013 5:16 pm

Re: hit boxes not working :(

Post by thewifitree »

I tried this and the error, levels/level1.lua.lua:36 '=' expected near 'flashx'
Although it might have to do with some minor changes i made
Attachments
jewel_2.01.love
(413.15 KiB) Downloaded 206 times
User avatar
BozoDel
Party member
Posts: 164
Joined: Fri Nov 01, 2013 10:28 pm

Re: hit boxes not working :(

Post by BozoDel »

Oh, that's because I made a mistake :crazy:

I wrote this

Code: Select all

   spikes[2].y
when I should have written this

Code: Select all

   spikes[2].y = 60
LÖVE was complaining about a problem in line 36, but the problem was actually in line 33. So remember to check the surrounding lines if you can't find the problem in the one indicated.
thewifitree
Prole
Posts: 32
Joined: Sat Dec 21, 2013 5:16 pm

Re: hit boxes not working :(

Post by thewifitree »

I never use tables because i think they're complicated so pardon my noobiness but is there a way to modify the x and y coordanites for when the player moves? Thanks!
EDIT: Never mind answered my own question!
User avatar
BozoDel
Party member
Posts: 164
Joined: Fri Nov 01, 2013 10:28 pm

Re: hit boxes not working :(

Post by BozoDel »

Don't say that, ~TaBlEs ArE fUn~!

I understand your stance, I'm going through the same thing with closures, but sometimes we just have to learn to use them.

Btw, next time, when you're not in such a hurry, you might wanna try going through more basic tutorials. I have a list of the ones I liked so far, if you want. I find the written ones better than video, but that's probably a matter of taste.
Post Reply

Who is online

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