SOLVED - map travel and time

Started by Gepard, Thu 03/03/2011 23:04:32

Previous topic - Next topic

Gepard

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.
Drink up me 'arties! Yo ho!

Khris

#1
It's good ol' Pythagoras:

Code: ags
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);
}


       a² + b² = c²

Jared

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.

Gepard

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.
Drink up me 'arties! Yo ho!

mode7

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?

Gepard

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...  :-[
Drink up me 'arties! Yo ho!

monkey0506

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.

Gepard

Thanks a lot everyone! It is working now that removed it from header.
Drink up me 'arties! Yo ho!

SMF spam blocked by CleanTalk