So in my new game I’m using hit detection that works well.
In the alien class there here’s the hit detect which works
for a,b in ipairs(bullets) do
if b.y >= self.y-scalerY*7.5 and b.y <= self.y+scalerY*7.5 and b.x >= self.x-scalerX*7 and b.x <= self.x+scalerX*7 then
table.insert(hits, Hit(b.x,self.y-scalerY*6) )
sound(SOUND_EXPLODE, 61, 0.5)
It scans the table bullets and sees if any hits the alien comparing the alien x pos (self.x) with bullet positon (b.x)
That works great. You’ll also see the line
That line inserts a hit class (a hit animation) at the point of impact on the alien. Currently for some reason it’s not picking it up properly and is half a screen away despite being accurately used to detect the hit.
What I want to do is log the position of the bullet where it hit the alien object and attach the hit class to that x position.
I’ve tried self.x and all it does it attach to the center of the alien and not the actual pixel position it hit the alien within that hit criteria of
b.x >= self.x-scalerX*7 and b.x <= self.x+scalerX*7
Your wisdom as ever is greatly appreciate!
First off, you probably do not want to use table.remove in this case. Instead, call
bullets[a] = nil
Next, it seems like it should be working correctly, unless this is a table.insert usage issue which I dont think it is. Is each element in the bullet table a class? If so, make sure you have a variable called self.x and that it actually is where the bullet is. Otherwise, there must be a problem with your hit class, for there is no other problem I can see. Maybe copy/paste it into a new project, and just call Hit(WIDTH/2,HEIGHT/2)?
Also, could the problem be you are inserting b.x rather than b.x-scalerX*7?
The self.x is the position of the alien in the alien class and is set up within that class.
B.x should be the position of the bullet in the table using a,b in ipairs
The issue with
is that it would take the position of b.x and then minusing off the variable
(scalerX is a variable that represents the width of the screen /100) and so it would place the hit at where it thinks b.x is and then move it over 7 percent of the screen to the left. Theoretically the hit could be anywhere within
as the alien is 14% of the width of the screen wide.
I think somehow b.x if going screwy after the hit detect… Hm…
It wasn’t the b.x that was the issue, that is correct it was the code I had written into the hit class that had some wrong math in it. Complicated by the fact I’m using gravity to control the ship.
Thanks for looking and posting, @TheSoldierKing, your confirmation helped me indentify the problem was elsewhere and not in the hit detect.