Implementing collisions

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
User avatar
stouty
Prole
Posts: 3
Joined: Wed Apr 20, 2016 7:35 pm
Location: Appalachian Mountains

Implementing collisions

Post by stouty »

How would I go about checking collisions for the player with an object that is declared in main or some other file?
On top of that, how would I go about creating a parent class like "collidable" to handle all the objects the player could collide with.
To clarify, I know the formula for collision (I believe), I'm just not sure where to implement it.
I'm new to Love, coming from Gamemaker.
Thank you!
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Implementing collisions

Post by ivan »

Hello.
creating a parent class like "collidable" to handle all the objects the player could collide with
That's one way to do it. Personally I recommend moving your collision code into a separate module. This way your game logic is less dependent of the collision code.
To clarify, I know the formula for collision
I'm afraid there is no single formula. It becomes increasingly more complicated depending on the "level of realism" you want.
The approach I would recommend is:
1. Check if the player intersects with the environment
2. Move the player so that he's not intersecting with the environment. Find the shortest separation vector (collision normal and penetration depth).
3. Adjust the player's velocity according to friction and restitution. You need to understand the role of these two coefficients and use them with the collision normal.

Better yet, use love.physics if you're unsure how to implement the code yourself.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Implementing collisions

Post by vrld »

You will need some central place that holds all the objects that can collide. In the simplest case, this is just a global table. In love.update(), you iterate over all the objects and check for collisions. You probably have something like this for drawing your objects:

Code: Select all

for obj1 in pairs(entities) do
    for obj2 in pairs(entities) do
        if obj1 ~= obj2 and do_collide(obj1, obj2) then
            do_stuff()
        end
    end
end
Another method would be to use the signal/observer pattern, where you have all the collidables subscribe to a 'check-collision'-signal (or similar). For each object you want to collide, you simply emit the signal, e.g.:

Code: Select all

-- in collidable constructor
signal.register('check-collision', function(other)
    if self ~= other and do_collide(obj1, obj2) then
        do_stuff()
    end
end

-- in collidable update
signal.emit('check-collision', self)
Note that both solutions are equivalent in terms of computational complexity. The second version just hides the global table.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 2 guests