Page 1 of 1

Hardon Collider - Identifying objects

Posted: Sat Jun 15, 2013 7:03 pm
by nordbjerg
Hey!

Are there any neat ways to wrap the different collision scenarios into maintanable and extendable code? In C++, for instance, I would probably add a virtual method to a class. How can I achieve this using HC?

Basically what I want to do is identify e.g. what enemy is colliding with e.g. the wall, the ground etc. and call e.g. the collision method for that corresponding enemy. I am not sure how to go about this.

Example:

Code: Select all

local function _collision(dt, a, b, dx, dy)
    if a.collision ~= nil then
        a:collision(b, dx, dy)
    end
    if b.collision ~= nil then
        b:collision(a, dx, dy)
    end
end

Code: Select all

entity = {}
entity.__index = {}

...

function entity:collision(other, dx, dy)
     print("Collision!")
end
Thanks!

Re: Hardon Collider - Identifying objects

Posted: Sun Jun 16, 2013 7:34 am
by Robin
Something you could do is make a and b have "kind" field or something:

Code: Select all

local function _collision(dt, a, b, dx, dy)
    if a.collision ~= nil then
        (a.collision[b.kind] or a.collision.default)(a, b, dx, dy)
    end
    if b.collision ~= nil then
        (b.collision[a.kind] or b.collision.default)(b, a, dx, dy)
    end
end

Code: Select all

entity.collision = {}
function entity.collision:default(other, dx, dy)
     print("Collision!")
end
function entity.collision:bullet(other, dx, dy)
     print("Collision with a bullet!")
end
Something like that.

To explain the syntax:

Code: Select all

        (a.collision[b.kind] or a.collision.default)(a, b, dx, dy)
This calls a.collision[b.kind] if it exists, otherwise it calls a.collision.default.

Does that help?

Re: Hardon Collider - Identifying objects

Posted: Sun Jun 16, 2013 10:00 pm
by nordbjerg
Well, a bit, but I still don't know e.g. what enemy collided with the player, how can I do that?

Re: Hardon Collider - Identifying objects

Posted: Mon Jun 17, 2013 6:39 am
by Plu
Store a reference to the player inside the specific body. In addition to 'kind', you can also put in 'data' or something and then store the table containing the enemy there. As long as it's consistent, you should be able to access it.

(Also keep in mind that the example posted would call 2 functions for each collission; a touching b and b touching a, I'm not sure if that's what you'd want.)