PDA

View Full Version : ACCURATE and CORRECT GetVehicleSpeed with MPH and KM/H


MP2
25/01/2013, 05:44 PM
I need an ACCURATE and CORRECT GetVehicleSpeed function that can return both MILES PER HOUR and KILOMETRES PER HOUR speeds. Obviously it needs to use GetVehicleVelocity.

I have searched for hours and only found bad ones that aren't accurate.

Thank you.

FUNExtreme
25/01/2013, 06:00 PM
The only way to make a correct one is to make it for each vehicle. There is no magic number to make it accurate for all vehicles, GTA SA vehicles weren't made with the speed limits they have in real life.

MP2
25/01/2013, 06:01 PM
You can get the M/S (meters/units per second) in-game. You can covert that to miles per hour or KM/H. It's do-able.

FUNExtreme
25/01/2013, 06:05 PM
You can get the M/S (meters/units per second) in-game. You can covert that to miles per hour or KM/H. It's do-able.

I'll say it in different words.

GTA SA vehicles do NOT have the same maximum speeds as in real life. There is NO way you can calculate it accurate for each vehicle without making calculations specific for that vehicle.

While one car drives faster ingame than in real life, another may drive slower ingame than in real life.

MP2
25/01/2013, 06:07 PM
Who said anything about top speeds or real life speeds? I certainly didn't.

FUNExtreme
25/01/2013, 06:09 PM
Who said anything about top speeds or real life speeds? I certainly didn't.

The only thing you certainly didn't do is use your brain.

If the top speeds aren't accurate, then neither is any other speed. seriously.

DaRk_RaiN
25/01/2013, 06:11 PM
Here's this one, works like a charm.

stock GetPlayerSpeed(playerid)
{
new Float:ST[4];
if(IsPlayerInAnyVehicle(playerid))
GetVehicleVelocity(GetPlayerVehicleID(playerid),ST[0],ST[1],ST[2]);
else GetPlayerVelocity(playerid,ST[0],ST[1],ST[2]);
ST[3] = floatsqroot(floatpower(floatabs(ST[0]), 2.0) + floatpower(floatabs(ST[1]), 2.0) + floatpower(floatabs(ST[2]), 2.0)) * 179.28625;
return floatround(ST[3]);
}

MP2
25/01/2013, 06:36 PM
The only thing you certainly didn't do is use your brain.

If the top speeds aren't accurate, then neither is any other speed. seriously.
The max speeds aren't accurate because Rockstar didn't set them to be. I need a speed function based on an OBJECT's (a vehicle) velocity (METERS (units) PER SECOND). The calculation would be EXACTLY the same for working out the MPH of an object being moved with MoveObject(). It has NOTHING to do with max speeds or vehicles.

The only way in which you could be right is if the GetVehicleVelocity function is inaccurate, but from what I know it is not. Even so, the old GetDistanceBetweenPoints method could be used (with the equation 'Speed = Distance/Time').



Here's this one, works like a charm.

stock GetPlayerSpeed(playerid)
{
new Float:ST[4];
if(IsPlayerInAnyVehicle(playerid))
GetVehicleVelocity(GetPlayerVehicleID(playerid),ST[0],ST[1],ST[2]);
else GetPlayerVelocity(playerid,ST[0],ST[1],ST[2]);
ST[3] = floatsqroot(floatpower(floatabs(ST[0]), 2.0) + floatpower(floatabs(ST[1]), 2.0) + floatpower(floatabs(ST[2]), 2.0)) * 179.28625;
return floatround(ST[3]);
}


I need an ACCURATE and CORRECT GetVehicleSpeed function that can return both MILES PER HOUR and KILOMETRES PER HOUR speeds. Obviously it needs to use GetVehicleVelocity.

I have searched for hours and only found bad ones that aren't accurate.

Thank you.
What is the '179.28625' for?

FUNExtreme
25/01/2013, 06:49 PM
I need a speed function based on an OBJECT's (a vehicle) velocity (METERS (units) PER SECOND). The calculation would be EXACTLY the same for working out the MPH of an object being moved with MoveObject(). It has NOTHING to do with max speeds or vehicles.


What is the '179.28625' for?

Then why do you keep implying that most GetVehicleSpeed functions are inaccurate? There are enough of them around which use the velocity, yet you say they are inaccurate. Even though they use the exact method described by you. Weird, don't you think?
If you want the accurate speed, calculate it without 'magic number', you'll get the exact speed the vehicle is driving. But are they accurate in relation to the actual world? No. Even though you'll have an accurate function, your cars will be driving unrealistic speeds.

And for the '179.28625', that's the magic number you need to turn the 'accurate velocity speeds' into 'accurate real life speeds'. The problem is, and I've explained it before, is that there is no magic number that works for ALL vehicles.

You can keep on searching all you want. Until you do what I told you in the first reply, you won't have an accurate speed function.

MP2
25/01/2013, 06:59 PM
So what you're basically saying is, the units in the game are NOT equal to a real life meter? Well that's pretty darn easy to solve. Or are you saying GetVehicleVelocity is wrong?

Obviously I made this topic as I haven't got the skills to make this myself. That being said, I did Physics GCSE and therefore know a little bit about velocity and speed.

FUNExtreme
25/01/2013, 07:02 PM
So what you're basically saying is, the units in the game are NOT equal to a real life meter? Well that's pretty darn easy to solve. Or are you saying GetVehicleVelocity is wrong?

No, what I'm saying is that it is impossible to get an accurate vehicle speed with just one magic number. You need to adjust them for each vehicle to get an accurate speed for all of them. This is because the top speeds of the vehicles don't match the real life speeds.
If all the vehicles had the same top speeds (or a multiplication) as in real life, you'd need only one magic number. Yet instaid of being the same they are pretty much random, because of that. If you use one magic number, it'll be accurate for a couple of vehicles but inaccurate for others. This is what I've been trying to get to all this time.

Virtual1ty
25/01/2013, 07:05 PM
Pretty much FUNExtreme has been right from the start, just put this in your head: Rockstar made it so each GTA-SA vehicle doesn't have the same acceleration, nor is it accelerating constantly (I think - gears).

This thread has some useful information and has been made just for this type of discussion:
http://forum.sa-mp.com/showthread.php?t=364124
Please go advise there too.

Good luck putting it all together, I surely don't have the patience to do so, and also OFFTOPIC: you have some nice releases man!

Edit: And oh, no such function to get accurate vehicle speeds has yet been released.

MP2
25/01/2013, 07:10 PM
Hurray for spending 10 minutes writing a reply, accidentally clicking out of the typing box then pressing backspace, making browser go back and losing everything. Fucking vBulletin.


If GetVehicleVelocity returns the velocities in M/S, can't this just be converted to MPH?

http://math.stackexchange.com/questions/9192/converting-a-velocity-vector-into-mph

I don't see why it's different for each vehicle. Surely an Infernus travelling at velocity of 22 meters per second is going the same speed as a Romero going at the same velocity..? Do vehicles have different velocity -> speeds or something? I've always hated maths and physics.

FUNExtreme
25/01/2013, 07:20 PM
Hurray for spending 10 minutes writing a reply, accidentally clicking out of the typing box then pressing backspace, making browser go back and losing everything. Fucking vBulletin.


If GetVehicleVelocity returns the velocities in M/S, can't this just be converted to MPH?

http://math.stackexchange.com/questions/9192/converting-a-velocity-vector-into-mph

I don't see why it's different for each vehicle. Surely an Infernus travelling at velocity of 22 meters per second is going the same speed as a Romero going at the same velocity..? Do vehicles have different velocity -> speeds or something? I've always hated maths and physics.

Yes, You can convert them. But you'll get unrealistic values. Because in the game the vehicles move at unrealistic speeds. (Yes, also the velocity)
Vehicle speeds in samp have to be based on the top speed because if you didn't do that, the vehicles would go unrealisticly high in speed. This is where the magic number comes in. And the problem is, I repeat again, that the magic number doesn't work for all vehicles.

Virtual1ty
25/01/2013, 07:22 PM
[...]
I don't see why it's different for each vehicle. Surely an Infernus travelling at velocity of 22 meters per second is going the same speed as a Romero going at the same velocity..? Do vehicles have different velocity -> speeds or something? I've always hated maths and physics.
Well it should be the same, but not in the GTA world, cause they made something called Inertia (still don't know what it means, I'm not so great at Phsysics), and it's connected to the vehicle model Mass and everything...
So that is the correct reason why vehicles don't accelerate with the same speed, and also on steep, curvy roads, because of Inertia, and that's why it's so hard to get that "Magic number" to multiplay the vehicle velocities with... There's no easy way to solve it as it's very GTA'ish, unrealistic...

Found this interesting reads by Googling (first one is for IV, just ignore it, basic principle applies for GTA-SA too.)
http://www.grandtheftwiki.com/Handling.cfg/GTAIV
http://www.gtaforums.com/index.php?showtopic=208373 I GUESS THIS IS THE MOTHER OF THEM ALL!

I posted these pages in lack of better ones..

Yeah so basically what FUNExtremo said and my knowledge posts too....

MP2
25/01/2013, 07:23 PM
Okay, what about my previous idea of distance checks, like people used to do before 0.3? That WOULD be accurate - right? You'd have the time (GetTickCount) and the distance (GetVehicleDistanceFromPoint(.. old coordinates ..).

Speed = Distance / Time.

Am I right?

Virtual1ty
25/01/2013, 07:25 PM
That would I guess be far the best way as velocity one is just waay too difficult to quickly address, however the distance one is just CPU-intensive, that's why (again, I guess..) people have coded their Speedometres with newly-introduced GetVehicleVelocity fn.

Edit: you're right, from Physics: Velocity is a change of distance* over time, v = s/t

MP2
25/01/2013, 07:28 PM
GetVehicleDistanceFromPoint is a native function - not one of the old old functions, so I assume it's a bit faster.

To be honest, what I need this for isn't for a speedo or anything, it's other stuff, and I need it to be based on velocity, so that isn't really an option for me. I guess I'll just have to stick with what I've got.

Scenario
25/01/2013, 07:30 PM
MP2, this function returns an accurate speed as far as I'm concerned.

forward Float:GetVehicleSpeed(vehicleid, UseMPH);
public Float:GetVehicleSpeed(vehicleid, UseMPH)
{
new Float:speed_x,Float:speed_y,Float:speed_z,Float:te mp_speed;
GetVehicleVelocity(vehicleid,speed_x,speed_y,speed _z);
if(UseMPH == 0) temp_speed = floatsqroot(((speed_x*speed_x)+(speed_y*speed_y))+ (speed_z*speed_z))*136.666667;
else temp_speed = floatsqroot(((speed_x*speed_x)+(speed_y*speed_y))+ (speed_z*speed_z))*85.4166672;
floatround(temp_speed,floatround_round);
return temp_speed;
}

I have a speedometer which presents a speed in both MPH and KM/H and I'll just take random SS's throughout the time I'm playing and will check the speeds with ******'s conversion calculator. So far they have been dead-on accurate.

FUNExtreme
25/01/2013, 07:30 PM
The distance one isn't accurate because of the way it works. Say you are at a specific point. You start driving, the distance is saved, you take a turn, when finishing the turn the distance is checked against the saved one. It'll think you did a shorter distance than you actually did, making it inaccurate. That is why velocity is better. (Note: the speed at which this is checked makes it so the inaccuracy is very small)

But I'm pretty sure that with the distance method you'll get the same unrealistic results because it is kind of the same as using velocity.

MP2
25/01/2013, 07:30 PM
Thank you everyone for your help, and putting up with my stubbornness and stupidity :<

Thank you to RealCop228 for the function.