Author Topic: TYPEs question  (Read 2603 times)

Offline bricky91

  • Mc. Print
  • *
  • Posts: 47
    • View Profile
TYPEs question
« 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?

MrTAToad

  • Guest
Re: TYPEs question
« Reply #1 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.

Offline Nobiag

  • Mr. Drawsprite
  • **
  • Posts: 58
    • View Profile
Re: TYPEs question
« Reply #2 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
« Last Edit: 2009-Jul-01 by Nobiag »

Offline bricky91

  • Mc. Print
  • *
  • Posts: 47
    • View Profile
Re: TYPEs question
« Reply #3 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:

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10752
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: TYPEs question
« Reply #4 on: 2009-Jul-02 »
you don't even need the IF line. The FOR will only work if there's a 2nd one left.

Offline Nobiag

  • Mr. Drawsprite
  • **
  • Posts: 58
    • View Profile
Re: TYPEs question
« Reply #5 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 :)