Calculate speed
#1

I was wondering how one can calculate speed of a vehicle? I have searched a bit on the forums, watched and read source of speedometers but I don't really understand. I have gone through a bit with "GetVehicleVelocity" function on SA-MP wiki but I don't really understand, what does it give? Does it give the value that it takes one meter/unit (ingame), as in does it give the time that it takes for the vehicle to move 1 unit? A brief explaination would be nice. :S

I did also search on the forums and this forumla came up:
Код:
s1[playerid] = floatsqroot(((svx[playerid]*svx[playerid])+(svy[playerid]*svy[playerid]))+(svz[playerid]*svz[playerid]))*100;
Same as in another tutorial:
Код:
final_speed = floatsqroot(((speed_x*speed_x)+(speed_y*speed_y))+(speed_z*speed_z))*136.666667;
Where speed_n or svn[playerid] (where n is either x, y or z) are from the function GetVehicleVelocity. I can see that they have basically taken the length of a vector (absolute value) or well basically the absolute value of whatever is given in GetVehicleVelocity and then multiplied with a number which I am guessing is "the magic number" referred to this thread.


Yet my main problem is that I don't understand this, why to use the absolute value (wiki it if you wonder what "absolute value" means) and my other or well first problem is what GetVehicleVelocity -actually- gives for a value, what is it? Is it a vector or something that is given per second or per unit or something?
Reply
#2

https://sampforum.blast.hk/showthread.php?tid=364124&page=3 you want about 181.5 look at my tests on this page that will give you velocity in km/h

That will correlate with this.

http://gtaforums.com/topic/216347-gt.../#entry3209126
Reply
#3

Read it now, but still doesn't answer my actual question. Why do you do the same forumla? squareroot(x^2 + y^2 + v^2) * yourNumber?
Reply
#4

There are a lot of different numbers because we can't tell in what unit the values of GetVehicleVelocity are

Why we use the pythagorean is pretty simple because the x value is the velocity on the x axis, y ... and z ...
Now we want to get whole velocity not only x, y and z, so we get the absolute value of this vector
This vector always points in the direction in which the vehicle moves
Reply
#5

Okay I understand now that you want the length of the vector to get how far you have gone. But if you get the length to be 5units per 1 second, isn't that your speed then? Let's say you take GetVehicleVelocity… And then you divide by the time which would be one second if you set a timer to do it each one second? And a unit can be compered to a meter, then can't you just remake it by multiplying 3,6 to get it into km/h? Or is GetVehicleVelocity really undefined of what it actually gives for values? If so, why haven't the SA-MP team explained it?
Reply
#6

It doesn't matter how often you call it, the values are updated internal

In sa-mp 0.1 we used a timer and calculated the speed like you explained

The main problem isn't the unit or the time it gets measured since the result wouldn't be realistic
Therefor everyone tries to find its own "magic number"

But in the end there is always one vehicle who wont fit, like the bicycle going around 60 km/h
Reply
#7

Quote:
Originally Posted by Nero_3D
Посмотреть сообщение
It doesn't matter how often you call it, the values are updated internal

In sa-mp 0.1 we used a timer and calculated the speed like you explained

The main problem isn't the unit or the time it gets measured since the result wouldn't be realistic
Therefor everyone tries to find its own "magic number"

But in the end there is always one vehicle who wont fit, like the bicycle going around 60 km/h
But then why can't one do a vehicleid check? If it is a bike, the speed will show differently? But that might be very unrealistic as the bike would still show maybe 15km/h while a "cleaning-vehicle" (can't remember their name) will go 30km/h while the bike is still going faster.

If so, isn't there a way to just slow down a vehicle? A function or something? I know there is the freeze/unfreeze function for players but is it possible to set a max value on a vehicle id's speed then?
Reply
#8

Well you could raise or lower the speed with SetVehicleVelocity
Slice released some time ago Speed cap - Limit the vehicle maxspeed!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)