Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Knox on Wed 15/12/2010 23:14:51

Title: Custom FaceCharacter, math for boundaries **SOLVED**
Post by: Knox on Wed 15/12/2010 23:14:51
Hi,

Ive got a question on if this is a good way to start for the following idea:

Id like to do something similar to the function "faceCharacter", as in I want to query the target and source characters' x,y coordinates, do a substraction on them, and depending on the results assign a string (or integer) to that result...its so that depending on where the target character is in relation to the source character, the source character will animate a certain "turning" animation before heading towards the target (Ive got lots of transition turn animation loops and I need to know which one to call depeding on the 2 characters' positions).

Lets say the source character I want to do the turning (to face another character) is "this"...and the "target" character I want "this" to face is the cChar:

Pseudo Code:

void TurnCharacterTowards(this Character*, Character *cChar)
{
 //get coordinates of cChar (b)
 int bX = cChar.x;
 int bY = cChar.y;

 //get current char coordinates (a)
 int aX = this.x;
 int aY = this.y;  

 //Substraction: cChar - current
 int cX = bX - aX;
 int cY = bY - aY;
 //depending on result, put loop number value...
 if (cX is only inside this boundary) int iLoop = 0;
 else if (cX is only inside this boundary) int iLoop = 1;
 etc...
}


The only thing is Im not sure if Im approaching this the right way. Also, Im not too sure how to get the boundaries I need. Look at this pic I did below:

(http://img812.imageshack.us/img812/6411/turnregions.png)

Assume "this" is in the middle of the grid. If the "target" character (cChar) is inside the red boundary, for example, how do I write it so I can get "if cChar is in this boundary, cChar is to the left of "this", run loop 1".

I have a tendency to make life hard for myself so what do you guys think? Is there an easier way?
Title: Re: Custom FaceCharacter, how to write the math for detecting boundaries(?)
Post by: Khris on Thu 16/12/2010 00:25:31
Here's how to get the loop from the positions:

  float xd = IntToFloat(cChar.x - this.x);
  float yd = IntToFloat(cChar.y - this.y);
 
  // perspective correction
  float pc = 2.0;
 
  // gives angle in range ]-180.0; 180.0]
  float an = Maths.RadiansToDegrees(Maths.ArcTan2(yd*pc, xd));
  // 0 = east, -90 = north, 90 = south, -180/180 = west
 
  // turn angle into loop #
  int d = FloatToInt(an/45.0, eRoundNearest); 
  if (d == -4) d = 4;
  d += 3;
  String loops = "73524061";
  loops = loops.Substring(d, 1);
  int loop = loops.AsInt;


Regarding the perspective correction: it is perfectly suitable for a top down view game to use eight sectors of 45° each for the eight directions; with a "standard adventure game perspective" though this might look off.
A good approximation is to simply squash the circle into an ellipse:

   (http://img812.imageshack.us/img812/6411/turnregions.png)

Ratio is 2:1 -> set pc to 2.0

If the ground floor covers even more depth on relatively few Y pixels, simply increase the value further.
Title: Re: Custom FaceCharacter, how to write the math for detecting boundaries(?)
Post by: Knox on Thu 16/12/2010 12:40:37
Khris = genius.

Thanks again, this works awesome!!  :)
Title: Re: Custom FaceCharacter, math for boundaries **SOLVED**
Post by: Khris on Thu 16/12/2010 13:06:41
You're welcome :)