Distance to Miles
#1

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?
Reply
#2

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.
Reply
#3

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).
Reply
#4

Kilometers:
distance(metres) / 1000

Miles:
distance(metres) / 1000 * 0.62137119

Example:
[pawn]
Reply
#5

So I assume the game's default measurement units are considered meters?
Reply
#6

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.
Reply
#7

Unfortunately your speed calculation is incorrect.

https://sampforum.blast.hk/showthread.php?tid=486060
Reply
#8

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
Посмотреть сообщение
Unfortunately your speed calculation is incorrect.

https://sampforum.blast.hk/showthread.php?tid=486060
That URL lead to an include that only included returning a vehicles maximum speed...?
Reply
#9

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.
Reply
#10

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));
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)