Designing GPS system

Gng

Heyall.

So, I coded a GPS in my game. My world is a sphere with a 0 y-axis representing the south pole and a 360 y-axis representing the north pole. You can't go beyond the poles and must go back the other way.
But for the x-axis, you can go all around the circle; passing the 0 on the x-axis brings you to 359 on the x-axis.

So there are objectives that you can use GPS to guide you in which direction you need to go to reach objective 1 (obj) and objective 2 (obj2) and ultimately will give you the exact coordinates when you are very close.

First, it checks if the player completed obj 2 so that it would only show objective 1. If not, it will check which objective is closer to the player and show its pathway to the player.

It was relatively hell to code this. I'm not kidding I've put "welcome to hell mother***" on loop on YouTube while working on this.
I'm just going to put this here for anyone interested.
Do let me know if there are ways to improve it, which I'm sure there are.

coor = coordinates
obj = objective
d = distance
x and y = x axis and y axis
the distance calculation is for finding the shortest distance; for example, if xcoor (player x coordinates) is 10 and xcoorobj (objective 1 x coordinates) is 350, the x distance from player to objective (dxobj) is 20.

if (gps.reachedobj2 = false) {
  if (Abs(gps.xcoor - gps.xcoorobj) > 180) {
    if (gps.xcoor > gps.xcoorobj) {
      gps.dxobj = gps.xcoorobj - gps.xcoor + 360
    }
    else {
      gps.dxobj = gps.xcoor - gps.xcoorobj + 360
    }
  }
  else {
    gps.dxobj = Abs(gps.xcoor - gps.xcoorobj)
  }
  gps.dyobj = Abs(gps.ycoor - gps.ycoorobj)
  if (Abs(gps.xcoor - gps.xcoorobj2) > 180) {
    if (gps.xcoor > gps.xcoorobj2) {
      gps.dxobj2 = gps.xcoorobj2 - gps.xcoor + 360
    }
    else {
      gps.dxobj2 = gps.xcoor - gps.xcoorobj2 + 360
    }
  }
  else {
    gps.dxobj2 = Abs(gps.xcoor - gps.xcoorobj2)
  }
  gps.dyobj2 = Abs(gps.ycoor - gps.ycoorobj2)
  gps.ddobj2 = Sqrt(Pow( gps.dxobj2, 2) + Pow( gps.dyobj2, 2))
  gps.ddobj = Sqrt(Pow( gps.dxobj, 2) + Pow( gps.dyobj, 2))
}
else {
  gps.ddobj2 = 100
  gps.ddobj = 1
}
OutputTextNoBr ("You estimately need to go ")
if ( gps.ddobj >  gps.ddobj2) {
  if ( gps.ddobj2 > gps.xcoorsteps * 10) {
    OutputTextNoBr ("more than " + gps.xcoorsteps * 10 + " to the")
  }
  else {
    OutputTextNoBr ("objs than " + gps.xcoorsteps * 10 + " to the")
  }
}
else {
  if ( gps.ddobj > gps.xcoorsteps * 10) {
    OutputTextNoBr ("more than " + gps.xcoorsteps * 10 + " to the")
  }
  else if ( gps.ddobj > gps.xcoorsteps * 5) {
    OutputTextNoBr ("objs than " + gps.xcoorsteps * 10 + " to the")
  }
}
if ( gps.ddobj >  gps.ddobj2) {
  if (gps.xcoorobj2 >= 180) {
    if (gps.xcoor >= 180) {
      if (gps.xcoor > gps.xcoorobj2) {
        OutputTextNoBr (" east,")
      }
      else if (gps.xcoor < gps.xcoorobj2) {
        OutputTextNoBr (" west,")
      }
    }
    else {
      if (gps.xcoorobj2 - gps.xcoor > 180) {
        OutputTextNoBr (" east,")
      }
      else if (gps.xcoorobj2 - gps.xcoor <= 180) {
        OutputTextNoBr (" west,")
      }
    }
  }
  if (gps.xcoorobj2 < 180) {
    if (gps.xcoor < 180) {
      if (gps.xcoor > gps.xcoorobj2) {
        OutputTextNoBr (" east,")
      }
      else if (gps.xcoor < gps.xcoorobj2) {
        OutputTextNoBr (" west,")
      }
    }
    else {
      if (gps.xcoor - gps.xcoorobj2 > 180) {
        OutputTextNoBr (" west,")
      }
      else if (gps.xcoor - gps.xcoorobj2 <= 180) {
        OutputTextNoBr (" east,")
      }
    }
  }
  if (gps.ycoorobj2 > gps.ycoor) {
    OutputTextNoBr (" north ")
  }
  else if (gps.ycoorobj2 < gps.ycoor) {
    OutputTextNoBr (" south ")
  }
}
else {
  if (gps.xcoorobj >= 180) {
    if (gps.xcoor >= 180) {
      if (gps.xcoor > gps.xcoorobj) {
        OutputTextNoBr (" east,")
      }
      else if (gps.xcoor < gps.xcoorobj) {
        OutputTextNoBr (" west,")
      }
    }
    else {
      if (gps.xcoorobj - gps.xcoor > 180) {
        OutputTextNoBr (" east,")
      }
      else if (gps.xcoorobj - gps.xcoor <= 180) {
        OutputTextNoBr (" west,")
      }
    }
  }
  if (gps.xcoorobj < 180) {
    if (gps.xcoor < 180) {
      if (gps.xcoor > gps.xcoorobj) {
        OutputTextNoBr (" east,")
      }
      else if (gps.xcoor < gps.xcoorobj) {
        OutputTextNoBr (" west,")
      }
    }
    else {
      if (gps.xcoor - gps.xcoorobj > 180) {
        OutputTextNoBr (" west,")
      }
      else if (gps.xcoor - gps.xcoorobj <= 180) {
        OutputTextNoBr (" east,")
      }
    }
  }
  if (gps.ycoorobj > gps.ycoor) {
    OutputTextNoBr (" north ")
  }
  else if (gps.ycoorobj < gps.ycoor) {
    OutputTextNoBr (" south ")
  }
}
OutputTextNoBr ("of you.<br>")
if ( gps.ddobj >  gps.ddobj2) {
  if ( gps.ddobj2 < gps.xcoorsteps * 5) {
    msg ("The objective 2 is at " + gps.xcoorobj2 + " x and " + gps.ycoorobj2 + " y.")
  }
}
else {
  if ( gps.ddobj < gps.xcoorsteps * 5) {
    msg ("The objective is at " + gps.xcoorobj + " x and " + gps.ycoorobj + " y.")
  }
}

Gng

Btw the code works, but I'm wondering if there was a simpler solution to this.


Log in to post a reply.

Support

Forums