Help with Collisions

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
joaovvr
Prole
Posts: 4
Joined: Tue Jun 24, 2014 8:09 pm

Help with Collisions

Post 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 150 times
User avatar
bdjnk
Citizen
Posts: 81
Joined: Wed Jul 03, 2013 11:44 pm

Re: Help with Collisions

Post 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.
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: Help with Collisions

Post 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. :)
joaovvr
Prole
Posts: 4
Joined: Tue Jun 24, 2014 8:09 pm

Re: Help with Collisions

Post 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:
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: Help with Collisions

Post 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. :)
joaovvr
Prole
Posts: 4
Joined: Tue Jun 24, 2014 8:09 pm

Re: Help with Collisions

Post by joaovvr »

Thanks man, this is just awesome. Thanks a lot :awesome:
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 3 guests