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
P.S.S.: Forgive my english, i know its really bad
Help with Collisions
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Help with Collisions
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.
At this point you have a collision, so now you must decide how to get rid of your bullet and star.
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
...
Re: Help with Collisions
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.
If you want to see an fully working example .love, feel free to ask.
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
Re: Help with Collisions
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
Thank you very much.
And MadByte, i really want to see a fully working example please
Re: Help with Collisions
Sure.
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.
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
Thanks man, this is just awesome. Thanks a lot
Who is online
Users browsing this forum: Majestic-12 [Bot] and 5 guests