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
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