Distance to Miles -
jakejohnsonusa - 22.08.2014
How do I convert a distance, from say GetVehicleDistanceFromPoint, to miles?
I know the math formula must lie somewhere in my vehicle speed formula by misco. Just not sure how to pull it out...
pawn Код:
stock GetPlayerSpeed(playerid,bool:kmh) // by misco
{
  new Float:Vx,Float:Vy,Float:Vz,Float:rtn;
  if(IsPlayerInAnyVehicle(playerid)) GetVehicleVelocity(GetPlayerVehicleID(playerid),Vx,Vy,Vz); else GetPlayerVelocity(playerid,Vx,Vy,Vz);
  rtn = floatsqroot(floatabs(floatpower(Vx + Vy + Vz,2)));
  return kmh?floatround(rtn * 100 * 1.61):floatround(rtn * 100);
}
Anyone know the formula?
Re: Distance to Miles -
Threshold - 22.08.2014
1 kilometer = 0.62137119 miles.
So to convert from km/hour to miles/hour, you just times (rtn * 100 * 1.61) by 0.62137119, which gives you roughly 100.04076159. Divide that by 100 and you get 1.0004076159, which so close to '1' that you really can just use the value of '1'... so the formula you already have is fine.
Re: Distance to Miles -
jakejohnsonusa - 22.08.2014
Yes, but that gives me the speed.
I'm looking to convert the game's default distance units (as a single float) to miles (as a single float).
Re: Distance to Miles -
Threshold - 22.08.2014
Kilometers:
distance(metres) / 1000
Miles:
distance(metres) / 1000 * 0.62137119
Example:
[pawn]
Re: Distance to Miles -
jakejohnsonusa - 22.08.2014
So I assume the game's default measurement units are considered meters?
Re: Distance to Miles -
Threshold - 22.08.2014
Yes, give me a second. I will edit this post when I am done with an example.
pawn Код:
#define CONVERSION_METRES 0
#define CONVERSION_KILOMETRES 1
#define CONVERSION_MILES 2
Float:ConvertDistance(Float:value, convertfrom, convertto)
{
  if(convertfrom == convertto) return value;
  switch(convertfrom)
  {
    case CONVERSION_METRES:
    {
      switch(convertto)
      {
        case CONVERSION_KILOMETRES: return (value / 1000);
        case CONVERSION_MILES: return ((value / 1000) * 0.62137119);
      }
    }
    case CONVERSION_KILOMETRES:
    {
      switch(convertto)
      {
        case CONVERSION_METRES: return (value * 1000);
        case CONVERSION_MILES: return (value * 0.62137119);
      }
    }
    case CONVERSION_MILES:
    {
      switch(convertto)
      {
        case CONVERSION_METRES: return ((value * 1000) / 0.62137119);
        case CONVERSION_KILOMETRES: return (value / 0.62137119);
      }
    }
  }
  return value;
}
Example:
pawn Код:
ConvertDistance(500.0, CONVERSION_KILOMETRES, CONVERSION_MILES);
Will return the distance of 500 kilometres in miles.
Re: Distance to Miles -
Pottus - 22.08.2014
Unfortunately your speed calculation is incorrect.
https://sampforum.blast.hk/showthread.php?tid=486060
Re: Distance to Miles -
jakejohnsonusa - 22.08.2014
Quote:
Originally Posted by Threshold
Yes, give me a second. I will edit this post when I am done with an example.
pawn Код:
#define CONVERSION_METRES 0 #define CONVERSION_KILOMETRES 1 #define CONVERSION_MILES 2
Float:ConvertDistance(Float:value, convertfrom, convertto) { Â Â if(convertfrom == convertto) return value; Â Â switch(convertfrom) Â Â { Â Â Â Â case CONVERSION_METRES: Â Â Â Â { Â Â Â Â Â Â switch(convertto) Â Â Â Â Â Â { Â Â Â Â Â Â Â Â case CONVERSION_KILOMETRES: return (value / 1000); Â Â Â Â Â Â Â Â case CONVERSION_MILES: return ((value / 1000) * 0.62137119); Â Â Â Â Â Â } Â Â Â Â } Â Â Â Â case CONVERSION_KILOMETRES: Â Â Â Â { Â Â Â Â Â Â switch(convertto) Â Â Â Â Â Â { Â Â Â Â Â Â Â Â case CONVERSION_METRES: return (value * 1000); Â Â Â Â Â Â Â Â case CONVERSION_MILES: return (value * 0.62137119); Â Â Â Â Â Â } Â Â Â Â } Â Â Â Â case CONVERSION_MILES: Â Â Â Â { Â Â Â Â Â Â switch(convertto) Â Â Â Â Â Â { Â Â Â Â Â Â Â Â case CONVERSION_METRES: return ((value * 1000) / 0.62137119); Â Â Â Â Â Â Â Â case CONVERSION_KILOMETRES: return (value / 0.62137119); Â Â Â Â Â Â } Â Â Â Â } Â Â } Â Â return value; }
Example:
pawn Код:
ConvertDistance(500.0, CONVERSION_KILOMETRES, CONVERSION_MILES);
Will return the distance of 500 kilometres in miles.
|
Just what I was looking for, thanks!
Quote:
Originally Posted by Pottus
|
That URL lead to an include that only included returning a vehicles maximum speed...?
Re: Distance to Miles -
Pottus - 22.08.2014
Sorry about that here you go.
pawn Код:
stock GetVehicleSpeed(vehicleid, bool:mph = false)
{
  static Float:VELOCITY[4];
  GetVehicleVelocity(vehicleid, VELOCITY[0], VELOCITY[1], VELOCITY[2]);
  VELOCITY[3] = floatsqroot(floatpower(floatabs(VELOCITY[0]), 2.0) + floatpower(floatabs(VELOCITY[1]), 2.0) + floatpower(floatabs(VELOCITY[2]), 2.0));
  if(mph)
    return floatround(VELOCITY[3] * 111.847);
  else
    return floatround(VELOCITY[3] * 180);
}
It is on the last page.
Re: Distance to Miles -
Kar - 22.08.2014
pawn Код:
#define UNIT_METRIC 0
#define UNIT_IMPERIAL 1
const Float:fVelocityMagicNumber = 181.5;
getVehicleSpeed(vehicleid, unit = UNIT_METRIC)
{
  new Float:fX, Float:fY, Float:fZ, Float:vehicleSpeed;
  GetVehicleVelocity(vehicleid, fX, fY, fZ);
  if(fZ < -0.01) // -0.023
    vehicleSpeed = floatsqroot((fX * fX)+(fY * fY)) * fVelocityMagicNumber;
  else
    vehicleSpeed = floatsqroot((fX * fX)+(fY * fY)+(fZ * fZ)) * fVelocityMagicNumber;
  return (unit == UNIT_METRIC) ? floatround(vehicleSpeed) : floatround(floatdiv(vehicleSpeed, 1.609344));
}