Hitbox checks (NOT point checks)

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
aliceandsven
Prole
Posts: 13
Joined: Thu Mar 29, 2012 3:15 pm

Hitbox checks (NOT point checks)

Post by aliceandsven »

Hello.

I'm a new lover, in fact I started yesterday and already I've made great progress (in my mind). I have the "skeleton" of my game in which I've set up how objects move, player control, graphics, even a timer for score keeping. So it looks like a game but when you sit down and try to play you'll notice nothing happens.

I've been following a video tutorial so far but the tutorial has seriously diverged, in that it's following a point-and-click sniper game with several instances of hte same entity to compare a single pixel to, and I am making a mouse-avoid game with several "box" entities that need to be compared to the player's "box" with a result when they collide (game over, show time, for example).

I find myself incredibly lost now, I cannot find a good tutorial that explains this well enough for me to wrap my head around it. If anyone could help me out I would really appreciate it. This is a huge hurdle I need to figure out, because after this mouse-avoid game which I'm doing to just get my bearings, I plan on moving on to a platformer that I've been planning for a while now. And platformers are all about multitudes of hitboxes and checks.

Here is my project if you want to look at it's guts and see where I'm coming from:
shapescape.rar
game project
(67.75 KiB) Downloaded 198 times
If anyone could help me over this hurdle, I would greatly appreciate it! :ultraglee:
Projects: 1) shape story xiv: fabula nova questalis [mouse-avoid game]
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Hitbox checks (NOT point checks)

Post by bartbes »

Well, there's two parts to answering if they don't collide, so by extension if they do.
  • If the distance on the x-axis between the two center points of the boxes is more than the sum of their widths, there can't be a collision.
  • If the distance on the y-axis between the two center points of the boxes is more than the sum of their heights, there can't be a collision.
This follows (pseudocode):

Code: Select all

function collide(a, b)
    if |a.x - b.x| > a.width + b,width then
        return false
    fi
    if |a.y - b.y| > a.height + b.height then
        return false
    fi
    return true
end
Now, let's make that lua:

Code: Select all

function collide(a, b) -- where a and b are tables containing an x, y, width and height
-- note that x and y are the center of the hitbox
-- and width and height are actually half of the box height (this makes the calculations easier)
-- this means the box is: (x-width, y-height), (x-width, y+height), (x+width, y+height), (x+width, y-height)
    if math.abs(a.x-b.x) > a.width+b.width then return false end
    if math.abs(a.y-b.y) > a.height+b.height then return false end
    return true
end
Alternatively you could make that one test and/or do inverse tests.
User avatar
aliceandsven
Prole
Posts: 13
Joined: Thu Mar 29, 2012 3:15 pm

Re: Hitbox checks (NOT point checks)

Post by aliceandsven »

THANK YOU I will try it now!

I also solved an issue with my image files, where the images were centered in the middle of the transparency (I have to do the ^2 thing). I'm putting them aligned to the upper-left corner now, since this is the x,y coordinate of objects, it will make my life easier when specifying width. Previously I had to offset x,y with -n

Also if anyone could provide balance feed-back for me that would be great. Are there any instances where it's impossible to survive?

Unfortunately the tutorial I began with did not explain tables/arrays (The Entity sections were breezed through, where it looks like lots of tables are used). Time to learn!

EDIT: I'm lost again. I think I'm just too stupid for this kind of stuff.

How do I assign the x,y,wx,wy of only my bad objects to one table, and the x,y,wx,wy of my player object to the other table? I understand that the code you posted will compare all the entrees in these two tables and return a result. I'm hung up on how to grab the data though and organize it in the way that the code will be able to read.
Projects: 1) shape story xiv: fabula nova questalis [mouse-avoid game]
meoiswa
Prole
Posts: 48
Joined: Wed Mar 14, 2012 8:13 pm
Location: Venezuela

Re: Hitbox checks (NOT point checks)

Post by meoiswa »

Post your game in a .love and people will gladly play it:

Windows:
1) Put all your game files in a folder,
2) Select all the files, right click, and select Move to zipped folder
3) Rename the zipped folder to have .love extension
4) Upload as attachment :)

Others:
Same trick, different procedure, check the Support board's sticky :)
Write the ö of Löve with Alt+numPad(148) or Alt+numPad(153)
User avatar
aliceandsven
Prole
Posts: 13
Joined: Thu Mar 29, 2012 3:15 pm

Re: Hitbox checks (NOT point checks)

Post by aliceandsven »

like this??
shapescape.love
(110.28 KiB) Downloaded 206 times
let me see if I get this straight:

Code: Select all

hitbox = { }
hitbox.player = x,y,wx,wy
hitbox.badguy1 = x,y,wx,wy
hitbox.badguyj2 = x,y,wx,wy
Somehow I create a code somewhere that grabs x,y,wx,wy from every object and sends it to each table index (which is in main.lua??). I think I shouldn't have made entities, I don't know how to grab all this data that is all over the place in different .lua files.

then I do the math code in main.lua

Code: Select all

function collide(a, b) -- where a and b are tables containing an x, y, width and height
-- note that x and y are the center of the hitbox
-- and width and height are actually half of the box height (this makes the calculations easier)
-- this means the box is: (x-width, y-height), (x-width, y+height), (x+width, y+height), (x+width, y-height)
    if math.abs(a.x-b.x) > a.width+b.width then return false end
    if math.abs(a.y-b.y) > a.height+b.height then return false end
    return true
end
then I call this function somewhere a couple times for every bad-guy

Code: Select all

...

collide(hitbox.player, hitbox.badguy1)
     if true then
          you crashed, game over
     else
          collide(hitbox.player, hitbox.badguy2)
          if true then
               you crashed, game over
          end
     end

...
I have a feeling I took a wrong turn somewhere in getting started with LUA/LOVE
Last edited by aliceandsven on Thu Mar 29, 2012 8:58 pm, edited 2 times in total.
Projects: 1) shape story xiv: fabula nova questalis [mouse-avoid game]
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Hitbox checks (NOT point checks)

Post by tentus »

Not... quite. I'm not sure what happened, but I had to extract and rezip your files in order to make them work. Weird. Are you sure you didn't rar them or something? The internal format of the .love is fine.
Kurosuke needs beta testers
User avatar
aliceandsven
Prole
Posts: 13
Joined: Thu Mar 29, 2012 3:15 pm

Re: Hitbox checks (NOT point checks)

Post by aliceandsven »

woops oh I .rar'd them

also I edited my last post
zippingoffagain.love
(155.35 KiB) Downloaded 785 times
Projects: 1) shape story xiv: fabula nova questalis [mouse-avoid game]
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Hitbox checks (NOT point checks)

Post by bartbes »

Alright, it seems you mistook the pseudocode for lua, grab the second snippet ;).
Then, it is obvious you need to look up how tables and if statements work in lua.
User avatar
aliceandsven
Prole
Posts: 13
Joined: Thu Mar 29, 2012 3:15 pm

Re: Hitbox checks (NOT point checks)

Post by aliceandsven »

Sorry, I meant to post the LUA code. It's really stressing me out now so I guess it was a careless mistake.

I read that web document on Tables that you posted, I'm still kind of confused.

EDIT:

For the sake of progress, I've forgone the hitbox for the player. Since it's only a 12px radius circle I can get away with using a point for now.

When I actually have a game that can be played, I'll move onto making a hitbox for larger player objects.

Here is the first "playable" iteration of the game. If you hit a bar or evil orb, the debug console will let you know. Idk how .love files work but maybe you can get the code and look at how I did it (if you want to critique, or if you need help too). It's some code with minor adjustments from that sniper game I was talking about earlier.
shapestory.love
(155.42 KiB) Downloaded 206 times
Thanks everyone for the assistance so far!
Projects: 1) shape story xiv: fabula nova questalis [mouse-avoid game]
Post Reply

Who is online

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