Collision of a Table of Bullets
Posted: Sun Dec 25, 2011 4:59 am
Hey everyone! I'm starting off with the physics module, so I decided to be original and write my own Asteroids game. I only have one question regarding collision detection with the ship's bullets.
In order to store the bullets' data, I created a variable which holds all the bullets and another which records how many bullets are active:
Every time the spacebar is pressed, a new bullet is created, like so:
The way I understand it (but please correct me if I'm wrong) is that upon setting callbacks to the world, once a collision is detected, the data set in shape:setData() is passed onto the collision function, like so:
Since only the string "Bullet" is being passed, how can I know which bullet collided with said *something*? I was thinking on modifying the setData() part and changing it to
so that once a collision is detected, I can get the characters after "Bullet" to obtain which bullet collided, but I don't know if I'm over-complicating myself. Is there another way I can do it?
Thanks! And happy holidays to everyone!
In order to store the bullets' data, I created a variable which holds all the bullets and another which records how many bullets are active:
Code: Select all
bullets = {}
bulletNo = 0
Code: Select all
if key == ' ' then
bulletNo = bulletNo + 1
table.insert(bullets, {})
bullets[bulletNo].body = love.physics.newBody(world, player.body:getX()+32*math.cos(player.angle), player.body:getY()+32*math.sin(player.angle), 1, 0)
bullets[bulletNo].shape = love.physics.newCircleShape(bullets[bulletNo].body, 0, 0, 2)
bullets[bulletNo].body:applyImpulse(10*math.cos(player.angle)+3*player.force.x, 10*math.sin(player.angle)+3*player.force.y)
bullets[bulletNo].lifespan = 0
bullets[bulletNo].shape:setCategory(2)
bullets[bulletNo].shape:setData("Bullet")
end
Code: Select all
function add(a, b, collision)
--If a collision is detected between *something* and a bullet, then either a or b would be "Bullet".
end
Code: Select all
bullets[bulletNo].shape:setData("Bullet" .. bulletNo)
Thanks! And happy holidays to everyone!