Ooops
May 31, 2020, 08:53:14 PM

Author Topic: Collison Detection  (Read 113 times)

Offline Lavalamp

  • Jr. Member
  • **
  • Posts: 7
Collison Detection
« on: May 17, 2020, 09:32:05 PM »
Hi Guys, having slow down issues with BlitzMax, I would if there is example code out there how to check all objects for collision. My way is pretty basic:

Code: [Select]
For check = EachIn Sprites
For missile = EachIn Sprites
missile.collision(check.x, check.x, check.w, check.h)
Next
check.update()
Next

In the method check.update() is has a huge select/case list for actions taken on each type of object. Seems this could be the problem...

Any pointers would be appreciated, thank you!

Offline hackball

  • Jr. Member
  • **
  • Posts: 26
Re: Collison Detection
« Reply #1 on: May 17, 2020, 09:46:28 PM »
It looks like you are doing a simple rectangle check.
You could improve the performance by grouping targets together into surrounding rectangles and check then if there is a chance the missile is close enough to those in the first place.

Offline Derron

  • Hero Member
  • *****
  • Posts: 2975
Re: Collison Detection
« Reply #2 on: May 17, 2020, 09:51:52 PM »
Code: [Select]
For check = EachIn Sprites
For missile = EachIn Sprites

First of all - check if checking against itself. also sometimes it is good to only check for "collideable" (missile against missile?)
Also I would think about what is checked : missiles hitting objects or if objects are getting hit by missiles. Why is this important?
What happens if 10 missiles collide with a ship? If the first has destroyed the ship, would the other 10 missiles still hit? would they explode in the explosion wave of the first? ...

Variant 1:
Code: [Select]
For check = EachIn Sprites
For missile = EachIn Sprites
if missile = check then continue 'skip self-self check

Variant 2
Code: [Select]
Type TElement
  Field alive:Int = True
End Type

Type TShip extends TElement
End Type

Type TMissile extends TElement
End Type

local elementDied:int = True
For local missile:TMissile = EachIn elements
For ship:TShip = EachIn elements
if not ship.alive then continue 'already killed
                if ship.Collide(missile)
                        ship.Boom()
                        ship.alive = False
                        'at least one ship (or missile) died... so clean up after the loops
                        elementDied = True
                        'missile hit a ship, it wont hit another or does it? so leave the ship-for-loop and continue with next missile
                        exit
                endif
Next
Next

if elementDied
For e:TElement = EachIn elements
                if not e.alive Then elements.Remove(e)
        Next
        elementDied = False 'ready for next Boom() Boom() Boom() ;D
endif


Depending on the amount of "elements" lookups could see a speedup by "clustering" the world into "blocks" (eg a 1000x1000 world has 10x10 grid cells/blocks of 100*100 each). Each block stores its missiles/elements ... and you only check your current entities cell plus the surrounding ones (assuming they just hit the border of their cell).


bye
Ron

Offline Lavalamp

  • Jr. Member
  • **
  • Posts: 7
Re: Collison Detection
« Reply #3 on: May 17, 2020, 11:46:04 PM »
Fantastic, assigning a missile tag fixed the issue, Thanks Ron! ;D

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal