Characters colliding question

Started by Pax Animo, Sun 19/03/2023 21:26:20

Previous topic - Next topic

Pax Animo

Heya,

I'm trying to do basic collision detection but the characters colliding are random, how can i check which characters are colliding so i can pass their id into another function. Something like:

Code: ags
if (character[?].IsCollidingWithChar(character[?])) {
    if (character[?].View != 4) {
      character[?].ChangeView(4);
      character[?].FollowCharacter(character[?], FOLLOW_EXACTLY);
    }
    //
    int saved_id = ?;
  }
Misunderstood

Snarky

AGS doesn't come with collision detection out of the box, but there is a very old module that adds it (this dl link is still active).

Matti

Snarky, I think you misunderstood the question. I don't think Pax Animo meant that the collision detection seems random, but that random collisions are supposed to happen in the game and they want to find out who's colliding.


I started to write some code using arrays, but I was unsure how exactly you want this to work.
 
I guess there are a bunch of characters that can randomly collide with each other and the basic idea is to have two arrays that iterate through all the characters that can collide. But who should follow whom exactly, if two of them collide? Who should change their view and who's ID is supposed to be passed?

Pax Animo

#3
Quote from: Matti on Mon 20/03/2023 09:46:57Snarky, I think you misunderstood the question. I don't think Pax Animo meant that the collision detection seems random, but that random collisions are supposed to happen in the game and they want to find out who's colliding.


I started to write some code using arrays, but I was unsure how exactly you want this to work.
 
I guess there are a bunch of characters that can randomly collide with each other and the basic idea is to have two arrays that iterate through all the characters that can collide. But who should follow whom exactly, if two of them collide? Who should change their view and who's ID is supposed to be passed?

Yes pretty much that. The change view and following part of the code is not so important, I mainly need to know exactly which characters collide if and when they do.

I've noticed in the console when characters do collide it gives a read out of the collision. I want to be able to save the Id of those colliding.

Sorry for the poor description, I know what I want but I'm not great at explaining.

Thanks in advance.
Misunderstood

Matti

Something like this should work (untested). You have to make sure that all the characters that can collide have a consecutive ID.

Code: ags
int i;
int i2;

for (i = 10; i < 16; i++) // iterate through characters 10 to 15 (as an example)
{
  for (i2 = i + 1; i2 < 16; i2++) // iterate again without checking the same pair of characters again
  {
    if (character[i].IsCollidingWithChar(character[i2])) 
    {
      // character[i] and character[i2] are colliding
    }
  }
}

Pax Animo

#5
Quote from: Matti on Mon 20/03/2023 11:46:03Something like this should work (untested). You have to make sure that all the characters that can collide have a consecutive ID.

Code: ags
int i;
int i2;

for (i = 10; i < 16; i++) // iterate through characters 10 to 15 (as an example)
{
  for (i2 = i + 1; i2 < 16; i2++) // iterate again without checking the same pair of characters again
  {
    if (character[i].IsCollidingWithChar(character[i2])) 
    {
      // character[i] and character[i2] are colliding
    }
  }
}

Quickly tested, looks to be working exactly how I want. I'll be trying it out further later, thanks ever so much.
Misunderstood

SMF spam blocked by CleanTalk