SA-MP Forums Archive
Calculate Speed of Vehicle - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: Calculate Speed of Vehicle (/showthread.php?tid=99992)



Calculate Speed of Vehicle - clavador - 02.10.2009

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).



Re: Calculate Speed of Vehicle - Joe Staff - 02.10.2009

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.


Re: Calculate Speed of Vehicle - yezizhu - 02.10.2009

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


Re: Calculate Speed of Vehicle - Joe Staff - 02.10.2009

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