Log In  

Cart #gamuretihi-0 | 2019-08-16 | Code ▽ | Embed ▽ | No License
5

I decided to make a space invaders clone in pico-8 over the weekend. I've been having a bit of trouble with a weird collision glitch that only happens when my Alien entities react to colliding with the right side of the screen. I've Worked through the logic in my head and my code should work. I've tried numerous variations of what I currently have in my cart thinking that maybe there is something with the code execution that is causing it to be off.

I'm pretty sure that the problem is related to the bit of code below. However, I'm not 100% sure since I haven't been able to fix it yet :-)

update = function(self)      
  if(self.x <= 0) then
    adr = 1
    self.x = 0
  elseif(self.x >= 120) then
    adr = -1
    self.x = 120
  end
    self.x +=self.v*adr             
end

Please direct me towards a solution if you can. This type of collision detection is usually pretty straightforward to do, but I've spent more time trying to fix this bug than I've spent making everything else.

[Edit]
I was able to fix the problem by moving the collision detection into a different loop to separate it from the position update so that the direction wouldn't be changed before all the entities had finished updating.

Cart #zitomarufe-0 | 2019-08-16 | Code ▽ | Embed ▽ | No License
5

P#66686 2019-08-16 00:51 ( Edited 2019-08-16 20:01)

All Comments

Please Excuse the messiness, I rarely show my code to humans :-)

P#66706 2019-08-16 01:11

You're updating them from left to right. When the last in row collides you change their global direction, which affects this last one right away but the others have moved already. It works on the left because the first in line collides and then the others get the correct dir. You might need to separate the collision detection loop so that everybody moves in the same direction within the same frame.

Hope this helps!

P#66711 2019-08-16 11:13

I fixed it. It was as simple as adding an extra for loop in my update function. Although I tried moving the collision detection to the update function, it was still having the same problem because it wasn't looping all the way through the list of entities before changing directions.

P#66714 2019-08-16 19:35 ( Edited 2019-08-16 19:47)

Thanks for the help @ultrabrite. I'm surprised that I didn't think of that solution sooner. Now hopefully I can get this finished up with the remainder of the weekend :-)

P#66715 2019-08-16 19:52 ( Edited 2019-08-16 20:03)

Actually pretty good ! You're even checking the edge of the LEFT and RIGHT to determine if the aliens can travel that far when their neighbors have been shot, just like original Space Invaders.

Going to STAR this and look forward to seeing this cart in the future !

P#66762 2019-08-18 16:23

Hey, Thanks! Space Invaders and Galaga are two of my favorite classic arcade games. I hoping to make this into a combination of the two. I rarely share the things I make but If you are interested, I might post the finished version. I'm still not sure that I'm happy with the art right now but I have finally convinced myself to just move on and worry about sprites later.

P#66766 2019-08-18 23:28 ( Edited 2019-08-18 23:29)

Hey not bad, liking the sprites

I also had a bit of figuring to do regarding the left/right sides when I was making my clone of Space Invaders.

On mine, I have a bounding/collision box that surrounds the whole enemy group. With each enemy death I check the width and height of the group (since it has to check collision with floor and destructible barriers) and update the collision box accordingly. If box hits wall, drop and change direction.

Also a big fan of Space Invaders and Galaga that when I realized there was no Space Invaders a few years back I had to write one up.

curious to see your take on it.. gl get it finished!

P#66962 2019-08-26 15:33 ( Edited 2019-08-27 02:41)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-28 15:53:57 | 0.015s | Q:28