Collision errors

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.
User avatar
toaster468
Citizen
Posts: 77
Joined: Sat Nov 06, 2010 11:30 pm

Collision errors

Post 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.
Attachments
bitdodger.love
Bit Dodger alpha 1
(6.16 MiB) Downloaded 131 times
User avatar
molul
Party member
Posts: 264
Joined: Sun Feb 05, 2012 6:51 pm
Location: Valencia, Spain
Contact:

Re: Collision errors

Post 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
User avatar
toaster468
Citizen
Posts: 77
Joined: Sat Nov 06, 2010 11:30 pm

Re: Collision errors

Post 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?
User avatar
molul
Party member
Posts: 264
Joined: Sun Feb 05, 2012 6:51 pm
Location: Valencia, Spain
Contact:

Re: Collision errors

Post 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.
User avatar
toaster468
Citizen
Posts: 77
Joined: Sat Nov 06, 2010 11:30 pm

Re: Collision errors

Post by toaster468 »

I am already using math.floor I forgot to add it to the snippet, sorry. Any other ideas?
User avatar
molul
Party member
Posts: 264
Joined: Sun Feb 05, 2012 6:51 pm
Location: Valencia, Spain
Contact:

Re: Collision errors

Post by molul »

D'oh! I'm clueless now, but if you post your current code I'll have a look ;)
User avatar
toaster468
Citizen
Posts: 77
Joined: Sat Nov 06, 2010 11:30 pm

Re: Collision errors

Post by toaster468 »

Oh sorry.
Attachments
bitdodger.love
(6.16 MiB) Downloaded 108 times
User avatar
molul
Party member
Posts: 264
Joined: Sun Feb 05, 2012 6:51 pm
Location: Valencia, Spain
Contact:

Re: Collision errors

Post by molul »

No probs! I'll check it when I come back from work this afternoon ;)
User avatar
molul
Party member
Posts: 264
Joined: Sun Feb 05, 2012 6:51 pm
Location: Valencia, Spain
Contact:

Re: Collision errors

Post 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!
Attachments
bitdodger_fixed.zip
(1.39 KiB) Downloaded 92 times
User avatar
toaster468
Citizen
Posts: 77
Joined: Sat Nov 06, 2010 11:30 pm

Re: Collision errors

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 138 guests