Page 1 of 1
Requesting Assistance With AABB Collision Response
Posted: Sun Nov 18, 2018 3:19 am
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,
Re: Requesting Assistance With AABB Collision Response
Posted: Sun Nov 18, 2018 5:56 am
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
Re: Requesting Assistance With AABB Collision Response
Posted: Tue Nov 20, 2018 10:44 am
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!