View Full Version : [CS:GO] Getting the Player's Speed.. Why is it negative?
EDIT: Okay. So I've managed to figure out how to get an accurate speed displayed on the HUD, but now I'm starting to wonder if this is a very bad way to do it? Like having such a large math calculation in my timer HUD that executes every 0.1? Here is my code, if it is a very bad way to do it what should I do?
public Action:HUD(Handle:timer)
{
for (new i = 1; i <= MaxClients; i++)
{
if (IsClientInGame(i) && IsPlayerAlive(i))
{
decl Float:fVelocity[3];
GetEntPropVector(i, Prop_Data, "m_vecVelocity", fVelocity);
new Float:currentspeed = SquareRoot(Pow(fVelocity[0],2.0)+Pow(fVelocity[1],2.0));
PrintHintText(i, "[Lv. %d] %N <font color='#FF1D8E'>XP: %d/%d</font>\n Time: <font color='#ff0000'>0:00:00</font> \n Speed: %f ", Level[i], i, XP[i], XP_Needed[i], currentspeed);
}
}
}
Zephyrus
11-05-2014, 23:53
thats THE way to calculate the length of velocity (=speed), not sure why you think its bad. also, 3 calculations*32*10=960 calculations a SECOND. thats nothing
friagram
11-06-2014, 11:44
Speed = GetVectorLength(velocity)
If use prop data with some entities it may be stale. Some entities store their velocity in other places as well.
Mitchell
11-06-2014, 11:54
Heres a useful plugin that you can reference to: https://forums.alliedmods.net/showthread.php?p=1106888
Your server should be fine as far as calculations go, since the server does way more calculations than that on a vanilla server.
Also i'd recommend defining the variables in the scope of the timer, not the loop.
friagram
11-06-2014, 12:05
Some of my plugins do some dozens (hundreds?) of float operations per frame per client, and the servers run fine.
And considering modern FPUs are benchmarked in GFLOPs... You could tack on considerably more 0s to your floating point ops per frame and be just fine.
Thank you everyone for the helpful replies, I've solved my issue by making the following changes:
1. I declared my variables outside of my loop as suggested by Mitchell, Thanks!
2. I was also getting a number with 0.0000000 decimal places, so in order to have a cleaner HUD I used the format parameter %.0f to make it show only the whole number!
My final code is below in case anybody finds a use for it! Now it's on to creating a timer, but I'll probably open a new thread regarding that challenge. Thanks again everybody for the help.
public Action:HUD(Handle:timer)
{
decl Float:fVelocity[3];
new Float:currentspeed
for (new i = 1; i <= MaxClients; i++)
{
if (IsClientInGame(i) && IsPlayerAlive(i))
{
GetEntPropVector(i, Prop_Data, "m_vecVelocity", fVelocity);
currentspeed = SquareRoot(Pow(fVelocity[0],2.0)+Pow(fVelocity[1],2.0));
PrintHintText(i, "[Lv. %d] %N <font color='#FF1D8E'>XP: %d/%d</font>\n Time: <font color='#ff0000'>0:00:00</font> \n Speed: %.0f ", Level[i], i, XP[i], XP_Needed[i], currentspeed);
}
}
}
vBulletin® v3.8.7, Copyright ©2000-2024, vBulletin Solutions, Inc.