Requesting Assistance With AABB Collision Response

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
ThatChillHomie
Prole
Posts: 5
Joined: Sun Jun 10, 2018 1:52 am

Requesting Assistance With AABB Collision Response

Post by ThatChillHomie »

Hello everyone , I am in need of some help with my AABB collision response. I already have the function for the collision detection off of the love site but I need help on how to actually do something with it when its true. I will post my source code below. I am making a space invader like game, this is my first try. I would appreciate the help guys. Thank you so much.

Best Regards, :P
Attachments
Code.txt
(2.95 KiB) Downloaded 54 times
User avatar
ivan
Party member
Posts: 1723
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Requesting Assistance With AABB Collision Response

Post by ivan »

Hello. The basic idea is to check every bullet against every enemy which is usually done by nested iteration:

Code: Select all

for i = #player.bullets, 1, -1 do
  for j = #enemies, 1, -1 do
      local bullet = player.bullets[i]
      local enemy = enemies[j]
      -- check bullet against enemy
      if CheckCollision(bullet, enemy) then
        table.remove(player.bullets, i)
        table.remove(enemies, j)
      end
  end
end
Note that we iterate the tables in reverse so that we can remove elements DURING the iteration.

On an unrelated note, make sure to use dt for every kind of movement:

Code: Select all

for _,b in pairs(player.bullets) do 
  b.y = b.y - 500*dt -- 500 pixels per second
end
User avatar
ivan
Party member
Posts: 1723
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Requesting Assistance With AABB Collision Response

Post by ivan »

First of all you really want to post your .love file so we can test it, right now I am unable to run the code without your .png assets.
Looking at your "Code.txt" file with Notepad++ I can see that your function "love.update(dt)" has a misplaced "end" keyword - if you fix your indentation this will become more obvious.
Regarding the collision code, you're almost there, you just need to call the CheckCollision function properly.
Here is a detailed explanation on how the AABBvsAABB algorithm works (including some alternatives):
https://2dengine.com/?p=intersections#R ... _rectangle
Programming is all about learning new stuff so don't get lost - the important part is understanding how the code works.
Good luck!
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 9 guests