issue with linked list

Advanced game related topics
decoder
New User
New User
Posts: 3
Joined: Mon Jun 13, 2005 12:36 pm

issue with linked list

Post by decoder »

So I've created 5000 or so 16x16 objects on the screen and now want to see if a simple collision detection can be performed without impairing performance too much.
The piece of code below doesn't work for more or less obvious reasons. How can a collision detection be performed against members of the same linked list?

ResetList(Bacter())
While NextElement(Bacter())
ResetList(Bacter())
While NextElement(Bacter())
If SpriteCollision(Bacter()\Image, Bacter()\x, Bacter()\y, Bacter()\Image, Bacter()\x, Bacter()\y)
routines go here
EndIf

Wend

Wend
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

You can get a pointer to the current list element, and then loop trough the
list and still access the one element through the pointer at the same time.

Like this:

Code: Select all

ForEach Bacter()
  *Current.<structure of your list> = @Bacter()  ; get the pointer to the current element

  ForEach Bacter() ; loop through the list
    If *Current <> @Bacter()  ; avoid checking against the item itself
      If SpriteCollision(*Current\Image, *Current\x, *Current\y, Bacter()\Image, Bacter()\x, Bacter()\y)
        ; do stuff
      EndIf
    EndIf
  Next Bacter()

  ; very important, set the list to the current element again, so the outer
  ; loop continues as it should
  ChangeCurrentElement(Bacter(), *Current)

Next Bacter()
quidquid Latine dictum sit altum videtur
decoder
New User
New User
Posts: 3
Joined: Mon Jun 13, 2005 12:36 pm

Post by decoder »

cheers!
Post Reply