Avoid walkpath to chaser

Started by Rocco, Sun 02/10/2011 10:42:48

Previous topic - Next topic

Rocco

I try to integrate a little chase sequenz,
the problem is, i dont know how to mathematically code that the new waypoint avoids the path direct to the chaser (player).

Code: KHRIS_related
  bool found;
  int x,y;
  
  float dx = IntToFloat(cEscape.x - player.x); 
  float dy = IntToFloat(cEscape.y - player.y);
  float d = Maths.Sqrt(dx*dx + dy*dy);  // distance
  
   if (d < 30.0)
    if(!cEscape.Moving) {
     while(!found) {
     // HERE SHOULD COME THE CALCULATION TO TRY TO AVOID THE WAY TO THE PLAYER
      x = Random(Room.Width);
      y = Random(Room.Height);
      
      if (Region.GetAtRoomXY(x, y) == region[2]) found = true;

     }
    cEscape.Walk(x, y);
  }


Khris

To get away from player, basically use what you already had:

Code: ags
  x = cEscape.x - player.x;
  y = cEscape.y - player.y;


This is the vector pointing from the player to cEscape. Move its base to cEscape and you have the direction away from player.
You should probably multiply this by 10 or something, or increase the length to 300 or so.

Code: ags
  int l = FloatToInt(Maths.Sqrt(IntToFloat(x*x + y*y)));
  x = x*300/l;
  y = y*300/l;

Rocco

#2
thanks, but i unfortunatly dont understand exactly whats going on in your example wiht l.  :P

The problem is also i cant use absolut values,
cause the calculacted waypoint can be either not one an walkable are or outside the room,
the other thing is that its rarely possible that theres no other way for cEscape,
as direct in playerdirection if he is standing in a corner with only one way out.

so there should be randomly calculted x and y coords, somewhere in the room but not in player direction.
(cEscape could stand still if there is no waypoint outside player direction available, or after 1000 unsuccessful while-loops it could turn off the restriction and give all directions to the calc.)

Khris

The second piece just stretches the vector to being 300 pixels long without changing its direction.

If you need random movement away from the player, use Random() to get any point within a rectangle and put the rectangle's center away from the player.

Code: ags
  x = cEscape.x - player.x;
  y = cEscape.y - player.y;

  // stretch vector's length to 50
  int l = FloatToInt(Maths.Sqrt(IntToFloat(x*x + y*y)));
  x = x*50/l;
  y = y*50/l;

  // randomize result
  x += Random(40) - 20;
  y += Random(40) - 20;

SMF spam blocked by CleanTalk