If we presume you insert the shape first, and then the body, and we rewrite the nested ipairs to a print, like this:
Code: Select all
for i, Body in ipairs(objects.bullets) do
for j, Shape in ipairs(objects.bullets) do
print(i, j, Body, Shape)
end
end
We'll see the output is:
Code: Select all
1, 1, Shape, Shape
1, 2, Shape, Body
2, 1, Body, Shape
2, 2, Body, Body
As you may now realise, the nested ipairs must not be doing what you expect them to, since this always means both 'Body' and 'Shape' will have a value of the other type at some point.
The best way to fix this is to not store the two together, perhaps by storing more structured data instead:
Code: Select all
local body = love.physics.newBody(world, objects.player.body:getX(), objects.player.body:getY())
local shape = love.physics.newRectangleShape(2, 2)
table.insert(objects.bullets, {body = body, shape = shape})
for i, bullet in ipairs(objects.bullets) do
bullet.fixture = love.physics.newFixture(bullet.body, bullet.shape, 0)
bullet.body:setLinearVelocity(400, 0)
end
Of course since you probably don't want to recreate fixtures for every bullet whenever a new one is fired, you're probably better off not using a loop just calling newFixture once.
As some extra explanation, the names given in the for loop for ipairs are
not filters of any sort. They simply represent the variable names used for the iteration values.