Hey all, I'm trying to make a basic top down game, and part of that is detecting collisions. However, depending on the object collided with, I want different behaviors. For example, if the player collides with a box, I want to destroy the box. If they collide with an enemy, I want them to take damage. If they collide with a building, I want them to stop moving. I come from the OOP world, so I was imagining implementing it something like this:
Code: Select all
function OnCollision(collideObject)
if (collideObject.type == box) then
collideObject.Destroy()
end
if (collideObject.type == enemy) then
player.TakeDamage()
end
if (collideObject.type == building) then
player.Stop()
end
I've implemented some basic collision detection, and I suppose I could iterate through every object every frame to see if there's a collision with it, but I imagine that will get expensive with lots of objects. I was thinking of implementing a physics library like HC, breezefield, etc. to do more efficient collisions, but I'm not sure if there's a way to get a reference to the original object, not just its collider. For example, if I created a box using something like:
Code: Select all
newBox = {x=100,y=100,type="box",collider=SomeColliderFunction}
and the box's collider was involved with a collision, how would I get a reference to the box object itself?