Hitbox Help (Issue With Locations)

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
Loyfe
Prole
Posts: 2
Joined: Mon Apr 09, 2018 8:55 am

Hitbox Help (Issue With Locations)

Post by Loyfe »

Current Code: https://pastebin.com/2xPSE9dL

I just need help with Hitboxes, I don't really get how it works. I've experimented, and tried code from other people but I am doing something wrong since my "bullet { }" x and y is nil. I am trying to get the bullets X and Y location but the table which is meant to add in the variables onto the array is not working the way I want to. I'm quite new, so forgive me if I've made a dumb mistake.

If there are other ways of creating a hitbox, can I know about it? I've looked through the wiki and found physics, but some of the documentation doesn't make sense to me (some documentation don't really have an explanation).

Thanks.
User avatar
NotARaptor
Citizen
Posts: 59
Joined: Thu Feb 22, 2018 3:15 pm

Re: Hitbox Help (Issue With Locations)

Post by NotARaptor »

Hi Loyfe :)

I don't think you have a problem with hitboxes - your entire logic flow seems a bit confused as to what it's trying to achieve.

You declare a global variable `bullet` on line 15 in `love.load`, then inside `love.update` you create (or set) another global variable called `bullets` which seems to represent a single bullet. Your call to `CheckCollision` at line 88 references the `bullet` global as if it represented one bullet (it references .x and .y), but all the other calls treat the `bullet` global as the collection of bullets, not the single.

Whichever way around you want this to work, your `CheckCollision` is only going to check one bullet (currently also against one enemy) - but then immediately after this call you loop through what I assume to mean all the bullets and all the enemies and delete the entry based on an index (`i`) which isn't defined anywhere. This looks like it was lifted whole from some other code.

Also on the enemies there seems to be a lot of confusion - in one place you reference a global `enemies`, but in another you reference `enemies_controller.enemies`.

Apart from the weird nil testing and assigning zero (but then exiting anyway so it doesn't do anything) at the top of `CheckCollision` - which I assume was from debugging - that function seems perfectly fine! It does indeed check if the point (px, py) is inside the rectangle with top-left coordinates (x, y) and size (wx, wy). You're just not calling it with the right parameters.

You need to decide where the enemies are going to be stored - inside `enemies_controller.enemies` or inside the global `enemies` - then stick to that, everywhere.

Ditto on the bullets. I notice that both enemies and the player have a holder for bullets (confusingly called `bullet` on the player and `bullets` on the enemies) - you probably don't need that. For one thing if the enemy is killed his bullets would automatically stop existing too, which probably isn't what you want. You're not really using this anyway, since you're only drawing the bullets in the `bullet` global, not the ones in the enemy member variable.

So... create one table for the enemies, stick to it. Create another for the bullets (if it helps you can have one table for all player bullets, and another for all enemy bullets, if you don't want enemy bullets to hit other enemies).

Once you've got all the enemies in one place, and all the bullets that can hit them in one place, then you can detect the collisions by iterating over both (a nested loop - check each bullet against each enemy).

Hope this helps! I hope I've been clear enough, happy to explain more on anything you didn't understand.
User avatar
NotARaptor
Citizen
Posts: 59
Joined: Thu Feb 22, 2018 3:15 pm

Re: Hitbox Help (Issue With Locations)

Post by NotARaptor »

Oh I didn't mention it above, but it's worth mentioning - Lua uses implicit global scope. What this means, for example, is that your global "bullet" variable which you set in `love.load` on line 15 is being overwritten by the variable called "bullet" in your `enemy:fire()` function on line 154. To avoid this, you should use local variables for temporary ones, like so:

Code: Select all

    function enemy:fire() --simpler way of writing enemy.fire(self)
      if (self.cooldown <= 0) then
        self.cooldown = 20
        local bullet = {}
        bullet.x = self.x
        bullet.y = self.y
        table.insert(self.bullets,bullet)
      end
    end
See that `local` keyword there? That means that the `bullet` defined here will NOT collide (in the namespace sense - nothing to do with hitbox collisions) with your global `bullet`. As it is, it's overwriting it.
Loyfe
Prole
Posts: 2
Joined: Mon Apr 09, 2018 8:55 am

Re: Hitbox Help (Issue With Locations)

Post by Loyfe »

Oh okay, thanks for that. Changing up what I did in about 10 hours, when I'm home. Hopefully I can make it work, thanks!
Post Reply

Who is online

Users browsing this forum: Google [Bot], pgimeno, Yolwoocle and 53 guests