[solved] Referencing game object from collision detection shape

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
OnACoffeeBreak
Prole
Posts: 28
Joined: Tue Apr 11, 2017 11:19 am

[solved] Referencing game object from collision detection shape

Post by OnACoffeeBreak »

I'm new to Lua and I am new to gamedev... I have a base class for all visible game objects that handles some of the update and all of drawing that covers screen wrapping. I am trying to use HardonCollider (HC) to improve collision detection. The base GameObject class constructor is passed a global HC instance. The GameObject constructor creates a rectangular collision detection shape in HC that matches the position, dimensions and rotation of the GameObject image that is also passed into the constructor.

So, now all of my game objects have a collision detection polygon overlaid (invisibly) on top of the images. Great!

The problem is that HC doesn't know anything about my game objects. All it knows about are collision detection shapes that have been created in its spacial hash by game objects. Collision detection is done in the main file similar to this:

Code: Select all

-- check for collisions
for shape, delta in pairs(HC.collisions(player.collisionShape)) do
	-- How do I get from shape to a GameObject that has this shape 
	-- so that I can remove the GameObject from a table of enemies
end
... as the comment says, how do I find which GameObject instance to remove from a table of active enemies based on the shape returned by HC?

I can search through the table of active enemies looking for a collision shape with the same coords as the shape returned by HC. That seems slow.

I can add a parent reference to GameObject inside the shape, but that still leaves the problem of where the GameObject is in the table. I suppose, I could add an index to every GameObject that identifies where in the list it is, but then I would have to update indexes as enemies are removed form the table. Maybe there's a way I can use a unique key (or ID) to store enemies in the table. This would then allow me to get a GameObject reference from shape and get a GameObject key that is then used to remove the GameOject instance form the table.

Any ideas?
Last edited by OnACoffeeBreak on Tue Apr 25, 2017 9:32 pm, edited 1 time in total.
User avatar
OnACoffeeBreak
Prole
Posts: 28
Joined: Tue Apr 11, 2017 11:19 am

Re: Referencing game object from collision detection shape

Post by OnACoffeeBreak »

Figured it out! HardonCollider has another way of checking for collisions. Each shape in HC has a collidesWith() method. Using it instead of HC.collisions() removes the need to have references to game objects from collision shapes.

Also, I learned how to remove elements from tables while using the Lua pairs() iterator.

Code: Select all

    -- update each asteroid. check for collision with bullets
    for ai, asteroid in pairs(asteroids) do
      asteroid:update(dt)
      for bi, bullet in pairs(bullets) do
        -- check bullet collision with asteroids
        if bullet.collisionShape:collidesWith(asteroid.collisionShape) then
          destroyAsteroid(ai)
          bullets[bi] = nil
        end
      end
    end
Post Reply

Who is online

Users browsing this forum: No registered users and 40 guests