Page 1 of 2

Collision errors

Posted: Sun Feb 12, 2012 3:48 pm
by toaster468
I have made a game called BitDodger and all of the mechanics are pretty much in place, so I just have to do the HUD, but I am having a problem with collisions. If you would like to help look at the attached .love file. Basically I made it so you are invincible for 5 seconds, then you can be hit, that is all working but when I go to the top "row" or coord y0 it does not print if I am being hit by a block or not. Which is strange considering I can be hit when I am on the lower rows. The game has the console activated so it will spam not hit or hit, obviously telling you when you are hit or not.

I also have another bug that I would like some guidance on, which now that I think about it may be related to the first bug, whenever I spawn and don't move I cannot be hit.

Should I take the bounding box snippet from the wiki and run it through a for loop and check collisions for each tile against the player? I figured that would be way too resource intensive for what it is.

Re: Collision errors

Posted: Wed Feb 15, 2012 11:13 am
by molul
Hi toaster. This is the problem: at line 101 you typed:

if ply.y > 0 then

So what happens when you are in the first row (that is, when ply.y == 0)? Nothing, as ply.y is not bigger than 0.

So how to fix it? Just change it to

if ply.y >= 0 then

Re: Collision errors

Posted: Mon Feb 20, 2012 6:00 pm
by toaster468
Ok, that worked but I am still getting false positives, when I try to use the players x coordinate to get the tile that he is currently on it gives me the wrong feedback, I am using this:

Code: Select all

ply.x / tile_w
to get the map table data from the player coordinate. Is that correct?

Re: Collision errors

Posted: Mon Feb 20, 2012 6:31 pm
by molul
That will most likely return a float number, and it could give errors.

You should get just the integer part of that division. Type math.floor(ply.x/tile_w) instead and let me know if it works.

Re: Collision errors

Posted: Wed Feb 22, 2012 10:32 pm
by toaster468
I am already using math.floor I forgot to add it to the snippet, sorry. Any other ideas?

Re: Collision errors

Posted: Thu Feb 23, 2012 12:13 am
by molul
D'oh! I'm clueless now, but if you post your current code I'll have a look ;)

Re: Collision errors

Posted: Thu Feb 23, 2012 1:46 am
by toaster468
Oh sorry.

Re: Collision errors

Posted: Thu Feb 23, 2012 9:02 am
by molul
No probs! I'll check it when I come back from work this afternoon ;)

Re: Collision errors

Posted: Thu Feb 23, 2012 7:54 pm
by molul
I'm back! I've fixed most of your code. Here's what I did:

-I created a new variable ply.status, to save the status "hit", "not hit" and "ignoring, invincible". Thus I can print it on screen. Therefore, I disabled the console as you won't need by now.

-I changed your love.update() function, as it had some errors. For instance, you were only detecting collisions when ply.y>0, so the first row was never taken into account. It should be ply.y>=0 instead. That means always (as your player will never go above y=0), so I just removed that "if" (not sure if you intended that row not to be taken into account on purpose). There are more changes, so have a look to it and let me know any doubts you might have :)

-ply.u seemed not to do anything, so I removed it from the debug messages.

-I modified the move() and love.keypressed functions so the player never goes further than map_w and map_h.


I hope this is closer to what you wanted to do. May I ask what your project is about? Or are you just learning? :)


EDIT: the attached zip only contains "conf.lua" and "main.lua". Just copy and overwrite them and you'll be ready to go!

Re: Collision errors

Posted: Thu Feb 23, 2012 11:24 pm
by toaster468
Well my friend made a version in another language so I just felt it was something easy to port over to Lua meant only as a learning experience. And thank you, I will look at the code and use it as a base and maybe I'll rewrite it (the third time!) since as you can see my variables do not reflect the data (Like ply.d, ply.d stood for player down ie the tile under him)

Not to mention it really isn't as lightweight as it could be I have dupilcates of some files, I have a whole library for 1 function. Thank you again.