Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Gepard on Thu 03/03/2011 23:04:32

Title: SOLVED - map travel and time
Post by: Gepard on Thu 03/03/2011 23:04:32
Hi there!

In my game I have a time system. This is why I need to calculate a distance between two points on a Gui map. I have buttons on my map that represent a certain points. Lets say we have points A and B. Player is currently in point B which is located at 100 x and 50 y. He wants to travel to point A which is located at 50 x and 100 y. I can not sum x and y for each place and than do A-B = time cuz I will get 0. I tried almost everything but still have no clue what to do. How to count a distance between two points on a 2D map???

Thanks.
Title: Re: map travel and time
Post by: Khris on Thu 03/03/2011 23:16:10
It's good ol' Pythagoras:

int distance(int x1, int y1, int x2, int y2) {

 float a = IntToFloat(x1 - x2);
 float b = IntToFloat(y1 - y2);

 return FloatToInt(Maths.Sqrt(a*a + b*b), eRoundNearest);
}


 (http://upload.wikimedia.org/wikipedia/commons/thumb/d/d2/Pythagorean.svg/220px-Pythagorean.svg.png)      a² + b² = c²
Title: Re: map travel and time
Post by: Jared on Thu 03/03/2011 23:24:52
EDIT: Hey, somebody smart did post! I'll put mine up anyway... good to know I learnt something from highschool, though..

Sounds like math is the issue - you need to calculate a hypotenuse as far as I can tell. So... I can't code it off the top of my head. But in pseudo-pseudo code


if(pointB.y == pointA.y){
    //bypass code
}else if(pointA.x == pointB.x){
  //bypass code
}else{
  sumA = pointA.x - pointB.x ;
  sumB = pointB.y - pointB.y ;
  sumC = [sumA.squared] + [sumB.squared]
  distance = [sumC.squareRoot]
}

Somebody with proper math skills now tell me why I'm idiot.

If you had a LucasArts style character on the map, you'd be able to use counters to bypass this -

while(ego.IsMoving() == true){
  counter++ ;
}

journeyTime = counter / 100;


Or something like that, pls note that'd be quite crude. Also, pathfinding issues come into it, then. Hope this was somewhat helpful.
Title: Re: map travel and time
Post by: Gepard on Fri 04/03/2011 08:04:51
Thanks both for suggestions. One problem is that it can not be a character that defines travel time (thought about it though). The other problem is :) and now do not hate me for being stupid (: that when I place the code Khris gave me under button click script it gives me the error: Nested functions not supported. Please help.
Title: Re: map travel and time
Post by: mode7 on Fri 04/03/2011 08:22:59
Put Khris' code on top of your script. When ever you need the distance in your main script  just type  distance (..SUPPLY THE PARAMETERS...) and you will be fine. pretty useful isnt it?
Title: Re: map travel and time
Post by: Gepard on Fri 04/03/2011 09:24:09
Quote from: mode7 on Fri 04/03/2011 08:22:59
Put Khris' code on top of your script.

Now that I did that, it says: Type or identifier differs from original declaraion...  :-[
Title: Re: map travel and time
Post by: monkey0506 on Fri 04/03/2011 09:29:42
Do you have any imports for "distance", "x1", "y1", "x2", "y2", "a", or "b" in your script header? Coz that error indicates that you have some import somewhere but then it's encountering something with the same name that isn't the same thing as what you were importing before.
Title: Re: map travel and time
Post by: Gepard on Fri 04/03/2011 11:58:14
Thanks a lot everyone! It is working now that removed it from header.