I have it set up so that when you push a button something will happen if the main character is close enough to the NPC. I'm having a hard time trying to figure out how to check and see if the Main Character's x,y coordinates are within a certain distance of the NPC's x,y coordinates. Heres a piece of my code that i need help with:
if ((cNPC.x <= 200 cMainCharacter.x) && (cNPC.y <= 120 cMainCharacter.y)) {
stuff happens;
}
my idea behind this was simple:
if the NPC's x coordinate is within 200 of the Main Characters x coordinate AND the NPC's y coordinate is within 120 of the Main Characters y coordinate then stuff would happen.
For some reason, AGS doesn't like my code though. Can you guys help me out?
EDIT:
Okay, i'm an idiot. lol. i figured it out ... in case anyone else has this problem the solution i came up with is:
if ((cNPC.x - cMainCharacter.x <=200) && (cNPC.y - cMainCharacter.y <= 120)) {
stuff happens
}
If you want to check distance in an ellipse instead of a rectangle, you can use this function. It will register the y distance as 1.9 as many pixels as it actually is, which should be the equivalent of your x <= 200 and y <= 120, if I did my math right.
function GetDistance(int x1, int y1, int x2, int y2) { //Gets the length (in pixels) of a straight line between x1/y1 and x2/y2. Assumes bird's-eye view where y=1.9x
int xdis = (x1 - x2);
if (xdis < 0) { xdis = (-1 * xdis); }
int ydis = FloatToInt(IntToFloat(y1 - y2)*1.9); //y to x equivalence
if (ydis < 0) { ydis = (-1 * ydis); }
if (ydis == 0) { return xdis; }
else if (xdis == 0) { return ydis; }
else { return FloatToInt(Maths.Sqrt(IntToFloat(((ydis)* (ydis))+(xdis)*(xdis))), eRoundNearest); }
}
EDIT: Oh. Looks like you figured it out. Nevermind, then.
Drew, this won't work as intended.
Your code will only check if the player is within a 200x120 rectangle to the top-right of the NPC.
int xd = player.x - cNPC.x, yd = player.y - cNPC.y;
if (xd >= -200 && xd <= 200 && yd >= -120 && yd <= 120) {
...
}
As for the actual distance, there are several threads dealing with that, including how to check for the NPC being inside an ellipse, like Lyaer explained.
The factor's supposed to be 200/120 = 1.67, btw.
Quote from: Khris on Sun 06/06/2010 18:34:18
Drew, this won't work as intended.
Your code will only check if the player is within a 200x120 rectangle to the top-right of the NPC.
int xd = player.x - cNPC.x, yd = player.y - cNPC.y;
if (xd >= -200 && xd <= 200 && yd >= -120 && yd <= 120) {
...
}
As for the actual distance, there are several threads dealing with that, including how to check for the NPC being inside an ellipse, like Lyaer explained.
The factor's supposed to be 200/120 = 1.67, btw.
yeah i noticed that it wasn't working exactly as i intended as i continued to play with it. I did come up with this though ... which looks basically like what you came up with only my version is longer and more complex where yours is a simplified version. Right?
if ((cNPC.x - cPlayer.x < 200) && (cNPC.x - cPlayer.x > -200) && (cNPC.y - cPlayer.y < 120) && (cNPC.y - cPlayer.y > -120)) {
........
}
I'm fairly new to coding and understanding everything AGS can do but i'm learning ... sometimes i find myself doing things the "long" way when there is a "shorter" way to do it or better way. But thank God we have these wonderful forums where people are willing to help.
THANKS GUYS!