One character close to other respons problem

Started by geork, Wed 27/05/2009 22:00:52

Previous topic - Next topic

geork

 Hey All!
   I'm trying to trigger an event when one character gets close to the other (say about, 5x and 5y away). I tried the "IsCollidingWithChar" method:
                 
Code: ags

                if(cChar1.IsCollidingWithChar(cEgo) == 1)//tried to also put in numbers like 50 etc
                

                 but one character just walks round the other. I also tried the "AreThingsOverlapping", but that seems to only work with objects.
                Is there a way to solve this?
                   Thanks ;)

Khris

AreThingsOverlapping() should work, just keep in mind to pass the characters' IDs:
Code: ags
  if (AreThingsOverlapping(player.ID, cGuy.ID)) { ...

Since it does a rectangular check, it doesn't work well for checking the distance though.

I think .IsCollidingWithChar checks the BlockingWidth/Height of the two characters. So if both characters are .Solid, this might never be true.

The arguably best way is calculating the distance of their positions:

Code: ags
bool CloseTo(this Character*, Character c, int dist) {
  int x = this.x - c.x, y = this.y - c.y;
  return x*x + y*y <= dist*dist;
}

// inside rep_ex
  if (player.CloseTo(cGuy, 5)) {
    ...

Wonkyth

You could probably do something with SSHes PixelPerfect Collision Detection module, I had a look at it recently, and it probably wouldn't take much retrofitting to get it to do that.
"But with a ninja on your face, you live longer!"

Trent R

Quote from: wonkyth on Wed 27/05/2009 23:38:07
You could probably do something with SSHes PixelPerfect Collision Detection module, I had a look at it recently, and it probably wouldn't take much retrofitting to get it to do that.
But Pixel Perfect is literally Pixel Perfect, and therefore a lot slower. Besides, it's already based off of the existing functions.

But our good old friend Pythagoras is the easiest and best solution.

~Trent
To give back to the AGS community, I can get you free, full versions of commercial software. Recently, Paint Shop Pro X, and eXPert PDF Pro 6. Please PM me for details.


Current Project: The Wanderer
On Hold: Hero of the Rune

geork

Thanks guys!
  it works now, my myyysticcc prrroooojjjeeccct!!! thx for all the help :)

SMF spam blocked by CleanTalk