Code: Select all
table.insert(objects,newObject)
objects.count = objects.count + 1
for numerically indexed tables, objects.count == #objects
Miken1 wrote:This is my function to create a box and I put everything in a table, but here is the problem, how can I make two objects in the table collide with each other?
Code: Select all
for i = 1, #objects do
local o1 = objects[i]
for j = i + 1, #objects do
local o2 = objects[j]
-- test and resolve collisions between o1 and o2 (do not remove o1 or o2 from the list at this point)
end
end
This iteration method is generally designed for moving objects. If you have static objects that do not move, you don't need to check for collisions between them.
If you have a callback during the collision step, you may want to iterate in reverse ("for i = #objects, 1, -1 do" and "for j = i - 1, 1, -1 do") so you can safely remove o1 or o2 from the list. Edit: Looking the code again: you have to break the inner loop right after destroying o1. Destroying o2, would shift the index of o1 so you generally want to avoid destroying either during the iteration.
Notice that the number of collision checks increases exponentially so if you have a lot of moving objects you may have to use some sort of space partitioning.
If you don't know how to resolve collisions, take a look at my humble tutorials:
http://2dengine.com/doc_1_3_10/gs_collision.html and
http://2dengine.com/doc_1_3_10/gs_intersection.html
Miken1 wrote:Bonus question: how do I make the objects collide with the player?
Generally, you want to treat the player just as another collision object so it shouldn't be a special case.
Miken1 wrote:bonus bonus question: How do I make the objects know if they are falling directly on the players head?
When resolving collisions you will have to find the "collision normal" or the "shortest separation vector".
Code: Select all
function oncollide(o1, o2, nx, ny)
-- check for a player vs box collision
if o1 == player then
player_vs_box(o1, o2, nx, ny)
elseif o2 == player then
-- rotate the collision normal 180 degrees
player_vs_box(o2, o1, -nx, -ny)
end
end
function player_vs_box(player, box, nx, ny)
-- check the y-axis of the collision normal
if ny < 0 then
-- player collided with an object that is pushing down
end
end