GLBasic forum

Main forum => GLBasic - en => Topic started by: bricky91 on 2009-Jul-01

Title: TYPEs question
Post by: bricky91 on 2009-Jul-01
Hey guys, here I am again. I'll try to explain what I want to do. I'm making a game like "mario vs donkey kong 2" (for the DS). So I sometimes have more than one player to control. The problem is that I have to make them change their direction when they hurt each other. But I can't understad how to do it. I tried to do like this

FOREACH player in players[]
//code...
FOREACH player2 in players[]
//code to reverse the horizontal direction
NEXT
//other code...
NEXT

but, it doesn't work well. I think it's because in the second FOREACH is included also the player of the first FOREACH... So bsically the object checks for collisions with itself, and obviously the object always collides with itself... Any ideas on how to check this kind of collisions?
Title: Re: TYPEs question
Post by: MrTAToad on 2009-Jul-01
You would only need to do the collision detection once - and then change their direction if they collide.  Or you could decide which one to change direction depending of the distance between the two.
Title: Re: TYPEs question
Post by: Nobiag on 2009-Jul-01
Code: (glbasic) [Select]
For mastercheck=0 to bounds(players[],0)-1

 if mastercheck<bounds(players[],0)-1
  For slavecheck=mastercheck+1 to bounds(players[],0)-1
   //blabla... check collision...
  next
 endif
next

Thats how i would do it. This way every combination of players will just be checked once, because the second "For to..." starts one step after the mastercheck.
With 4 Players it would look like this:

1+2
1+3
1+4

2+3
2+4

3+4
Title: Re: TYPEs question
Post by: bricky91 on 2009-Jul-01
Code: (glbasic) [Select]
For mastercheck=0 to bounds(players[],0)-1

 if mastercheck<bounds(players[],0)-1
  For slavecheck=mastercheck+1 to bounds(players[],0)-1
   //blabla... check collision...
  next
 endif
next

Thats how i would do it. This way every combination of players will just be checked once, because the second "For to..." starts one step after the mastercheck.
With 4 Players it would look like this:

1+2
1+3
1+4

2+3
2+4

3+4

Pretty cool, works great! Thanks a lot!  :good:
Title: Re: TYPEs question
Post by: Kitty Hello on 2009-Jul-02
you don't even need the IF line. The FOR will only work if there's a 2nd one left.
Title: Re: TYPEs question
Post by: Nobiag on 2009-Jul-02
you don't even need the IF line. The FOR will only work if there's a 2nd one left.

The if line was what i added when i edited the post, cause i was not sure of that. First i wrote "bounds(players[],0)-2" in the first "for to" line, but i thought you would need code for that last player apart from the collision, so i added that if line.
So a "for to" does nothing when it goes backwards (without step -1)? Good to know that :)