Hardon Collider - Identifying objects

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
nordbjerg
Prole
Posts: 12
Joined: Thu Jun 13, 2013 9:17 pm

Hardon Collider - Identifying objects

Post 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!
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Hardon Collider - Identifying objects

Post 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?
Help us help you: attach a .love.
nordbjerg
Prole
Posts: 12
Joined: Thu Jun 13, 2013 9:17 pm

Re: Hardon Collider - Identifying objects

Post by nordbjerg »

Well, a bit, but I still don't know e.g. what enemy collided with the player, how can I do that?
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: Hardon Collider - Identifying objects

Post 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.)
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 2 guests