Page 1 of 1

Help with Collisions

Posted: Wed Jul 09, 2014 4:02 am
by joaovvr
Hey guys, i've already read some tutorials about collisions, but my question still remains.

I'm a Love2D newbie, and i'm having troubles with collisions.
I'm making a game like Asteroid, i've already did the spaceship, the bullets and a star. The thing is, the star must disappear when touched by the bullet (the bullet must disappear too), and i dont know how to do it. I know this is an easy thing, but after reading "a thousand" tutorials about collision, i still dont get it how to do this.
I really need your help guys.

P.S.: All my commands are in portuguese, sorry about that :rofl:
P.S.S.: Forgive my english, i know its really bad :death:
my-first-game.love
(104.41 KiB) Downloaded 154 times

Re: Help with Collisions

Posted: Wed Jul 09, 2014 6:02 am
by bdjnk
Looking at your code, it's nicely structured, and even with the variable names and comments in something I don't read I was able to quickly see what was happening. Here's the main thing I noticed: You didn't even try to detect the collision.

Let's talk about what's necessary. There are really two separate problems. First is detecting the collision; second is reacting appropriately (with the star and bullet disappearing).

The collision is the easy part. All I want to know, is if any part of box one, overlaps any part of box two, because that's a collision. I'll give you the code, because I'm too lazy to explain further.

Code: Select all

for i,v in ipairs(tiros) do
   if v.x < estrela.x + estrela.img:getWidth() and
      v.x + v.width > estrela.x and
      v.y < estrela.y + estrela.img:getHeight() and
      v.y + v.height > estrela.y then
...
At this point you have a collision, so now you must decide how to get rid of your bullet and star.

Re: Help with Collisions

Posted: Wed Jul 09, 2014 9:38 am
by MadByte
Removing the objects also isn't that hard. All you have to do is searching for a good way to handle all your objects. Using seperate tables for each kind of object (i.e spaceships, enemies, astroids) is a common way to handle that. You just need to create the "container table" and loop through all objects inside of it. If a collision is detected you simply remove it from the table.

Code: Select all

-- Main
bullets = {}
-- now somehow insert your objects into the table
function love.update(dt)
  for i, bullet in ipairs(bullets) do
    -- Update your bullets here somehow and
    -- Check for a collision(here is an example)
    if boxCollision(bullet, player) then 
      table.remove(bullets, i)
      player:damage(20) -- example
    end
  end
end

If you want to see an fully working example .love, feel free to ask. :)

Re: Help with Collisions

Posted: Wed Jul 09, 2014 2:35 pm
by joaovvr
Thanks a lot guys, you two helped me a lot and finally I have an idea of how to check collisions and remove objects.
Thank you very much.

And MadByte, i really want to see a fully working example please :rofl:

Re: Help with Collisions

Posted: Wed Jul 09, 2014 5:31 pm
by MadByte
Sure.
CollisionHandlingExample.love
LEFT / RIGHT: Arrow Keys
SHOOT: Space
EXIT: Escape

I wrote some, hopefully helpful, comments for you. If I forgot something or you have any other questions
feel free to send me a PM. :)

Re: Help with Collisions

Posted: Thu Jul 10, 2014 1:24 am
by joaovvr
Thanks man, this is just awesome. Thanks a lot :awesome: