I need help with Hardon Collider...

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.
User avatar
Cucurbitacée
Prole
Posts: 47
Joined: Fri Dec 14, 2012 10:22 am
Location: Frankfurt am Main

I need help with Hardon Collider...

Post by Cucurbitacée »

Hi everyone,

I'm still working on my first game, I rewrote it from scratch as I think I'm beginning to getting how Lua and LÖVE are working. :joker:

Browsing the Wiki and the forum I come to the conclusion that using BoundingBox.lua for a screen full of enemies and bullets would not be the better, from a performance point of view.

So I decided to have a look at Hardon Collider. So far I could implement it without problem, all the shapes are providing a callback when there is a collision.

My problem is how to resolve the collision now. Let me explain, I'd like to manage several type of collisions:
  • Player with enemy: will kill the player instantly, damage the enemy.
  • Player with power-up: will power the player up, remove the power-up icon.
  • Player with scenery: will kill the player, no effect on scenery.
  • Player's bullets with enemy: will damage the enemy, remove the bullet.
  • Enemy's bullet with player: will kill the player, remove the bullet.
  • Enemy's bullet with scenery: will remove the bullet, no effect on scenery.
Please not that at the moment there is no scenery and that the placement of the enemies is random. The enemies will have fixed path when I will be in level design phase. :roll:

I've just put a test in my function on_collide and I can see that the shape are colliding as they should. But now, how do I determine which shape collides with which shape?
So far I've been trying to do it by iterating all the tables one after the other and assigning to the shapes to temp ones to compare them... It's not working and I feel it's not the right way to do it. :brows:

If you look at the code, good luck, it's a bit of a mess at the moment as I was working on the interface more than everything and a lot of thing are not in place yet. :P

Thanks in advance. :D
Attachments
ZeroContinuity.love
(3.2 MiB) Downloaded 142 times
scutheotaku
Party member
Posts: 235
Joined: Sat Dec 15, 2012 6:54 am

Re: I need help with Hardon Collider...

Post by scutheotaku »

First of all, sorry I can't look at your code right now (at work), but I'll try to point you in the right direction...
I'm more partial to bump.lua, but with HC I believe you could do something like this (NOT TESTED):

Code: Select all

--load hardon collider
HC = require 'hardoncollider'

function love.load()
    collider = HC(100, on_collide)

    --create player
    player = collider:addRectangle(0,0,32,32)
    --give the player a "type" value
    player.type = "player"

    --setup table to contain enemies
    enemy = {}

    --setup first enemy
    enemy[1] = collider:addRectangle(32,32,32,32)
    --give first enemy a type value of "enemy"
    enemy[1].type = "enemy"

    --setup second enemy
    enemy[2] = collider:addRectangle(128,96,32,32)
    --give second enemy a type value of "enemy"
    enemy[2].type = "enemy"
end

function love.update(dt)
    collider:update(dt)
end

function love.draw()
    --draw player
    player:draw('fill')

    --iterate through enemy table and draw each enemy
    for i,v in ipairs(enemy)
        v:draw('fill')
    end
end

function on_collide(dt, shape_a, shape_b)
    if shape_a.type == "player" then
        PlayerCollision(shape_b)
    elseif shape_b.type == "player" then
        PlayerCollision(shape_a)
    end
end

function PlayerCollision(shape)
    if shape.type == "enemy" then
        print("player is colliding with enemy!")
    end
end
Hopefully that helps and isn't confusing :)
User avatar
RedHot
Citizen
Posts: 87
Joined: Mon May 27, 2013 2:43 pm
Location: Poland

Re: I need help with Hardon Collider...

Post by RedHot »

Dude, haDRon :rofl: . Hardon Collider is the most brilliant title for a gay porn I can think of. Well done.

http://www.urbandictionary.com/define.p ... %20hard-on . Please take a great note of this.
User avatar
Cucurbitacée
Prole
Posts: 47
Joined: Fri Dec 14, 2012 10:22 am
Location: Frankfurt am Main

Re: I need help with Hardon Collider...

Post by Cucurbitacée »

RedHot wrote:Dude, haDRon :rofl: . Hardon Collider is the most brilliant title for a gay porn I can think of. Well done.

http://www.urbandictionary.com/define.p ... %20hard-on . Please take a great note of this.
You made me doubt, but I'm not drunk yet, so it's in fact:
http://love2d.org/wiki/HardonCollider
:crazy:
User avatar
Cucurbitacée
Prole
Posts: 47
Joined: Fri Dec 14, 2012 10:22 am
Location: Frankfurt am Main

Re: I need help with Hardon Collider...

Post by Cucurbitacée »

scutheotaku wrote:First of all, sorry I can't look at your code right now (at work), but I'll try to point you in the right direction...
I'm more partial to bump.lua, but with HC I believe you could do something like this (NOT TESTED):

Code: Select all

--load hardon collider
HC = require 'hardoncollider'

function love.load()
    collider = HC(100, on_collide)

    --create player
    player = collider:addRectangle(0,0,32,32)
    --give the player a "type" value
    player.type = "player"

    --setup table to contain enemies
    enemy = {}

    --setup first enemy
    enemy[1] = collider:addRectangle(32,32,32,32)
    --give first enemy a type value of "enemy"
    enemy[1].type = "enemy"

    --setup second enemy
    enemy[2] = collider:addRectangle(128,96,32,32)
    --give second enemy a type value of "enemy"
    enemy[2].type = "enemy"
end

function love.update(dt)
    collider:update(dt)
end

function love.draw()
    --draw player
    player:draw('fill')

    --iterate through enemy table and draw each enemy
    for i,v in ipairs(enemy)
        v:draw('fill')
    end
end

function on_collide(dt, shape_a, shape_b)
    if shape_a.type == "player" then
        PlayerCollision(shape_b)
    elseif shape_b.type == "player" then
        PlayerCollision(shape_a)
    end
end

function PlayerCollision(shape)
    if shape.type == "enemy" then
        print("player is colliding with enemy!")
    end
end
Hopefully that helps and isn't confusing :)
Thanks for the reply. In fact I began by doing something like this. As the player have only one instance, this is way the to go. But I'm a bit puzzled when it comes to determine the collision from a bullet from a table with an enemy from another table. Anyway, I didn't think of adding a "type" to the objects, this may be a way to reduce redundant check... :)
scutheotaku
Party member
Posts: 235
Joined: Sat Dec 15, 2012 6:54 am

Re: I need help with Hardon Collider...

Post by scutheotaku »

Cucurbitacée wrote:
scutheotaku wrote:First of all, sorry I can't look at your code right now (at work), but I'll try to point you in the right direction...
I'm more partial to bump.lua, but with HC I believe you could do something like this (NOT TESTED):

Code: Select all

--load hardon collider
HC = require 'hardoncollider'

function love.load()
    collider = HC(100, on_collide)

    --create player
    player = collider:addRectangle(0,0,32,32)
    --give the player a "type" value
    player.type = "player"

    --setup table to contain enemies
    enemy = {}

    --setup first enemy
    enemy[1] = collider:addRectangle(32,32,32,32)
    --give first enemy a type value of "enemy"
    enemy[1].type = "enemy"

    --setup second enemy
    enemy[2] = collider:addRectangle(128,96,32,32)
    --give second enemy a type value of "enemy"
    enemy[2].type = "enemy"
end

function love.update(dt)
    collider:update(dt)
end

function love.draw()
    --draw player
    player:draw('fill')

    --iterate through enemy table and draw each enemy
    for i,v in ipairs(enemy)
        v:draw('fill')
    end
end

function on_collide(dt, shape_a, shape_b)
    if shape_a.type == "player" then
        PlayerCollision(shape_b)
    elseif shape_b.type == "player" then
        PlayerCollision(shape_a)
    end
end

function PlayerCollision(shape)
    if shape.type == "enemy" then
        print("player is colliding with enemy!")
    end
end
Hopefully that helps and isn't confusing :)
Thanks for the reply. In fact I began by doing something like this. As the player have only one instance, this is way the to go. But I'm a bit puzzled when it comes to determine the collision from a bullet from a table with an enemy from another table. Anyway, I didn't think of adding a "type" to the objects, this may be a way to reduce redundant check... :)
If you will be having multiple players and such, your best bet (in my opinion) would be to have a class system in place. If you want to use a library for this (instead of "rolling your own"), I highly recommend MiddleClass: https://love2d.org/wiki/MiddleClass

As far as "But I'm a bit puzzled when it comes to determine the collision from a bullet from a table with an enemy from another table." - it depends on what you need to do. For the most flexibility, again a class system would be ideal. In that case, you could just do something like:

Code: Select all

function on_collide(dt, shape_a, shape_b)
    shape_a:Collide(shape_b)
    shape_b:Collide(shape_a)
end
Where "Collide" is a method that all "collidable" classes have. That method would take as the first argument (well, technically the second, but don't worry about that for now...) the shape that it is colliding with.
So, if shape_a is a bullet and shape_b is an enemy then-
1. shape_a, an instance of the bullet class, would run its collision method. That collision method would determine the class of shape_b (or, alternatively, you could use a "type" system like I used in my last post), and then act accordingly.
2. shape_b, an instance of the enemy class, would do the same, only it would run ITS class' collision method.
Sorry if that doesn't make much sense :D

But, in most cases (at least for simpler games), processing collision for both entities is probably pointless. For example, you could just do something like this:

Code: Select all

function on_collide(dt, shape_a, shape_b)
    if shape_a.type == "enemy" then
        EnemyCollision(shape_a,shape_b)
    elseif shape_b.type == "enemy" then
        EnemyCollision(shape_b,shape_a)
    end
end

function EnemyCollision(enemy,shape)
    if shape.type == "bullet" then
        print("an enemy is colliding with a bullet!")
        
        --kill enemy
        enemy.dead = true
        print("enemy killed by bullet!")

        --kill bullet
        bullet.dead = true
        print("bullet killed!")
    end
end
So, instead of having "bullet collision" functions, you handle any bullet removals in the other collision functions.

NOTE: you probably don't want the "collision" functions in my examples to be global functions, unless your game is very small.

EDIT:
Also, while HC is an EXCELLENT library which has a lot of useful features, if your project only requires simpler collisions (e.g., only axis-aligned boxes), you may want to take a look at Bump.lua. I personally prefer its simplicity, and love using it for smaller projects in particular. HC is excellent though!
https://github.com/kikito/bump.lua
Last edited by scutheotaku on Thu Jun 13, 2013 8:23 pm, edited 1 time in total.
User avatar
RedHot
Citizen
Posts: 87
Joined: Mon May 27, 2013 2:43 pm
Location: Poland

Re: I need help with Hardon Collider...

Post by RedHot »

Well apparently someone has come up witha horrible pun :shock:

https://en.wikipedia.org/wiki/Large_Hadron_Collider
User avatar
Cucurbitacée
Prole
Posts: 47
Joined: Fri Dec 14, 2012 10:22 am
Location: Frankfurt am Main

Re: I need help with Hardon Collider...

Post by Cucurbitacée »

Thanks for all the info, I'll have a look to all of this and try to figure out what's best for me. :D
scutheotaku
Party member
Posts: 235
Joined: Sat Dec 15, 2012 6:54 am

Re: I need help with Hardon Collider...

Post by scutheotaku »

Cucurbitacée wrote:Thanks for all the info, I'll have a look to all of this and try to figure out what's best for me. :D
Just curious, what is your experience? If you're fairly new to this type of thing, then it may be better if you skip the whole "class system in Lua" thing for now, just for simplicity's sake. I personally prefer a "traditional" class system for things like this (though recently the concept of entity-component relationships has started to win me over), but in a language like Lua this can make things harder than the need to be for a newcomer :)
Last edited by scutheotaku on Thu Jun 13, 2013 8:26 pm, edited 1 time in total.
User avatar
Cucurbitacée
Prole
Posts: 47
Joined: Fri Dec 14, 2012 10:22 am
Location: Frankfurt am Main

Re: I need help with Hardon Collider...

Post by Cucurbitacée »

RedHot wrote:Well apparently someone has come up witha horrible pun :shock:

https://en.wikipedia.org/wiki/Large_Hadron_Collider
Eh! Between LÖVE, AnAL, hump and probably a lot of other libraries I don't know about, I find Hardon Collider pretty consistent. ^^
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 39 guests