Calculate Speed of Vehicle
#1

Hi!

I can't find a working calculus to get my speed-meter working.

I've tried a code that came with a speed-o-meter & one of my own too, but i can't make it work.

Here's the code with what was my old function mixed with someone else's:

Код:
public Float:GetVelocity(playerid,Float:X, Float:Y,Float:Z)
{
	if(IsPlayerConnected(playerid) && IsPlayerInAnyVehicle(playerid))
	{
		    new Float:Distance;
	      GetPlayerPos(playerid, X, Y, Z);
	      Distance = floatsqroot(floatadd(floatadd(floatpower(floatsub(X, OldX[playerid]), 2), floatpower(floatsub(Y, OldY[playerid]), 2)), floatpower(floatsub(Z, OldZ[playerid]), 0)));

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	      OldX[playerid] = X; OldY[playerid] = Y; OldZ[playerid] = Z;
				new Float:Kilometers = floatround(floatmul(Distance, 2.0), floatround_floor);
				
				return Kilometers;
	}
	else return 0.0;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

}
And here the part where I draw it on the screen:

Код:
	new Float:result = GetVelocity(playerid, x,y,z);
	
  new string[128];
  format(string, sizeof(string),"%f", result);
  SendClientMessage(playerid, 0xff0000ff, string);
  
  return 1;
}
I just wanna know why it raises when i accelerate but sometimes i'm going through a straight line and it displays, 70,80, 90, 40, 80, 90, 40 (it goes well below and comes up again to a reasonable number).
Reply
#2

You need a consistent timer, like a 1 second timer, to figure the exact speed.
pawn Код:
forward UpdateSpeed()
new Float:pOldPos[MAX_PLAYERS][3];
new Float:pSpeed[MAX_PLAYERS];
Float:GetPlayerMPH(playerid)
{
  return ((pSpeed[playerid]*3600)/1000)*1.609344;
}
Float:GetPlayerKMPH(playerid)
{
  return (pSpeed[playerid]*3600)/1000;
}
public OnGameModeInit()
{
  SetTimer("UpdateSpeed",1000,1);
}
public UpdateSpeed()
{
  new Float:x,Float:y,Float:z;
  for(new i; i < MAX_PLAYERS; i++)
  {
    if(IsPlayerConnected(i))
    {
      GetPlayerPos(i,x,y,z);
      pSpeed[i]=floatsqroot( ((x-pOldPos[i][0])*(x-pOldPos[i][0]))+((y-pOldPos[i][1])*(y-pOldPos[i][1]))+((z-pOldPos[i][2])*(z-pOldPos[i][2])) );
      pOldPos[i][0]=x;
      pOldPos[i][1]=y;
      pOldPos[i][2]=z;
    }
  }
  return;
}
I believe then you could use GetPlayerMPH(playerid) and GetPlayerKMPH(playerid) in your script, or just use those formulas in your own script.

Quite frankly, this is off the top of my head and I've been out of the game for some time, so I could be wrong.
Reply
#3

Don't if is offtopic, in 0.3 a func called GetVehicleVelocity
It can get vec speed efficiency.
Reply
#4

This isn't the 0.3 discussion board, but yes that's a better method.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)