13.08.2011, 22:27
(
Последний раз редактировалось Kar; 01.12.2014 в 01:09.
)
This tutorial is based upon JernejL's (Redshirt, The turtle lover) Math Plugin.
He basically asked me to explain the functions a little more.
https://sampforum.blast.hk/showthread.php?tid=270508
Other functions
Credits:
SA-MP Developers/Beta testers of course
JernejL
* To the "this is not a tutorial users", I don't care.
He basically asked me to explain the functions a little more.
https://sampforum.blast.hk/showthread.php?tid=270508
- - Basically a fasterpawn Код:native Float:MPDistanceCameraToLocation(Float:CamX, Float:CamY, Float:CamZ, Float:ObjX, Float:ObjY, Float:ObjZ, Float:FrX, Float:FrY, Float:FrZ); // calculates how distant target aim is from camera data pointed towards a certain pointhttp://forum.sa-mp.com/showthread.ph...rgetToLocationpawn Код:Float: DistanceCameraTargetToLocation(Float:CamX, Float:CamY, Float:CamZ, Float:ObjX, Float:ObjY, Float:ObjZ, Float:FrX, Float:FrY, Float:FrZ)
Example code
This is faster than the original one, and might be a little more accurate.pawn Код:stock IsPlayerAimingAt(playerid, Float:x, Float:y, Float:z, Float:radius)
{
new Float:cx,Float:cy,Float:cz,Float:fx,Float:fy,Float:fz;
GetPlayerCameraPos(playerid, cx, cy, cz);
GetPlayerCameraFrontVector(playerid, fx, fy, fz);
return (radius >= MPDistanceCameraToLocation(cx, cy, cz, x, y, z, fx, fy, fz));
}
I don't really know any other uses for this, if you have one please post it.
- This function can be used to flip automatically flip vehicles, it can be used to show where your vehicle is facing (horizontally).pawn Код:native Float:MPGetVehicleUpsideDown(vehicleid); // returns values such as 1.0 as pointing up, -1.0 is totally upside down. returns -5.0 if car id is not 1..2000.
If you can't understand that,Quote:
<&JernejL> itll always make coords 2 units above car roof
<&JernejL> regardless of car rotation
<&JernejL> so if car is upside down
<&JernejL> it'll give you coords 2 units under it
Now this function, is a very helpful, you may use it to detect your vehicle's rotation positionpawn Код://1.0 = car placed on level ground / pointing up
//0.0 = car is 90 degrees on side
//-1.0 would be totally upside down
//-5.0 if car id is not 1..2000.
Example code - automatic flip
pawn Код:public OnPlayerUpdate(playerid)
{
new vehicleid = GetPlayerVehicleID(playerid);
if(vehicleid != 0)
{
new Float:health;
GetVehicleHealth(vehicleid, health);
newVehicleHealth[vehicleid] = health;
if(newVehicleHealth[vehicleid] != oldVehicleHealth[vehicleid])
{
CallLocalFunction("OnPlayerVehicleHealthChange", "iiff", playerid, vehicleid, newVehicleHealth[vehicleid], oldVehicleHealth[vehicleid]);
oldVehicleHealth[vehicleid] = newVehicleHealth[vehicleid];
}
}
}
forward OnPlayerVehicleHealthChange(playerid, vehicleid, Float:newhealth, Float:oldhealth);
public OnPlayerVehicleHealthChange(playerid, vehicleid, Float:newhealth, Float:oldhealth)
{
#pragma unused playerid
#pragma unused newhealth
#pragma unused oldhealth
if(-1.5 <= MPGetVehicleUpsideDown(vehicleid) <= -0.5)
{
new Float:Angle, Float:x, Float:y, Float:z;
GetVehicleVelocity(vehicleid, x, y, z);
GetVehicleZAngle(vehicleid, Angle);
SetVehicleZAngle(vehicleid, Angle);
SetVehicleVelocity(vehicleid, x, y, z);
}
return 1;
} - This function returns the player that this player is aiming at or invalid player id if no player in target area.pawn Код:native MPGetAimTarget(PlayerID, Float:SeekRadius = 50.0); // returns player that this player is aiming at or invalid player id if no player in target area.
SeekRadius is the radius from the playerid, that you want to look from, defaultly 50.0, but you might want to use 200.0 if your gonna use a sniper rifle.
The rest is self explanatory.
Example code
pawn Код:public OnPlayerUpdate(playerid)
{
new aimingtarget = MPGetAimTarget(playerid, 50.0);
if(aimingtarget != INVALID_PLAYER_ID)
{
SendClientMessage(playerid, "You are aiming at a player.");
}
return 1;
} - This function finds the vehicle that this trailer is attached to, returns invalid_vehicle_id if invalid or not attached to any towing vehicle.pawn Код:native MPGetTrailerTowingVehicle(vehicleid); // finds the vehicle that this trailer is attached to, returns invalid_vehicle_id if invalid or not attached to any towing vehicle.
In other words, it gets the vehicle that the trailer is attached to.
pawn Код:new vehicleid = CreateVehicle(402, 0.0, 0.0, 0.0, 180.0, -1, -1, -1);
new vehicletrailer = CreateVehicle(402, 0.0, 0.0, 0.0, 180.0, -1, -1, -1);
AttachTrailerToVehicle(vehicletrailer, vehicleid);
new trailerowner = MPGetTrailerTowingVehicle(vehicletrailer);
printf("trailerowner = %d - vehicleid = %d", trailerowner, vehicleid); - This function get's the driver of a vehiclepawn Код:native MPGetVehicleDriver(vehicleid);
Example code
pawn Код:public OnPlayerStateChange(playerid, newstate, oldstate);
{
switch(newstate)
{
case PLAYER_STATE_PASSENGER:
{
new vehicleid = GetPlayerVehicleID(playerid),
mydriver = MPGetVehicleDriver(vehicleid),
str[64]
;
format(str, 64, "Driver of this vehicle is %d", mydriver);
SendClientMessage(playerid, -1, str);
}
}
return 1;
} - This function gets the amount of driver's in the driver seat of a vehicle, yet there can only be 1. but hacking can put someone else in the vehicle causing more than 1 player's to be in one seat causing a crash to the first person that was a driver when they exit the vehicle.pawn Код:native MPGetVehicleDriverCount(vehicleid); // returns number of drivers a car has (important to solve 2 drivers 1 car issue - if you wrote any decent anticheat you know what i mean)
Example code:
pawn Код:public OnPlayerUpdate(playerid)
{
new vehicleid = GetPlayerVehicleID(playerid);
if(1 <= vehicleid <= 2000 )
{
new drivercount = MPGetVehicleDriverCount(vehicleid);
if(drivercount > 1)
{
for(new i = 0; i < MAX_PLAYERS; i++)
{
if(GetPlayerVehicleID(i) != vehicleid) continue;
RemovePlayerFromVehicle(i);
}
}
}
return 1;
} - This function gets the number of passengers in a vehiclepawn Код:native MPGetVehicleOccupantCnt(vehicleid); // returns number of player a vehicle is carrying
pawn Код:public OnPlayerStateChange(playerid, newstate, oldstate);
{
switch(newstate)
{
case PLAYER_STATE_PASSENGER:
{
new vehicleid = GetPlayerVehicleID(playerid),
passengercount = MPGetVehicleOccupantCnt(vehicleid),
str[64]
;
format(str, 64, "There are %d players in this vehicle", passengercount);
SendClientMessage(playerid, -1, str);
}
}
return 1;
} - This function gets the number of players surfing a vehiclepawn Код:native MPGetVehicleSurfersCnt(vehicleid); // returns number of players surfing a vehicle
pawn Код:public OnPlayerStateChange(playerid, newstate, oldstate);
{
switch(newstate)
{
case PLAYER_STATE_PASSENGER:
{
new vehicleid = GetPlayerVehicleID(playerid),
surfingcount+ = MPGetVehicleSurfersCnt(vehicleid),
str[64]
;
format(str, 64, "There are %d players surfing this vehicle", surfingcount);
SendClientMessage(playerid, -1, str);
}
}
return 1;
} - The function basically clamps any value over 360.pawn Код:native Float:MPClamp360(Float:value);
From JernejL ( clamp360 will also ensure the angle is in 0..360 positive range )
Example you have 400.0, it will return 60.0.
Код:<&JernejL> it's useful if you use stuff like <&JernejL> difference between 2 angles <&JernejL> to make a "x'o clock" system <&JernejL> then you need proper angles clamped to that range
That will return 1.00.pawn Код:public OnGameModeInit()
{
printf("%0.2f", MPClamp360(361.0));
return 1;
} - This function is a very faster GetDistanceBetweenPoints, I did a speed test result with 100000 checks and the results werepawn Код:native Float:MPDistance(Float:v1x, Float:v1y, Float:v1z, Float:v2x, Float:v2y, Float:v2z); // distance between 2 points
GetDistanceBetweenPoints - 73
MPDistance - 21
pawn Код:#define GetDistanceBetweenPoints MPDistance - Yet another getdistancebetweenpoints, but the difference with this one is that it is completely faster, but yet less precise. I would recommend using MPDistance since there's not much of a speed difference.pawn Код:native Float:MPFDistance(Float:v1x, Float:v1y, Float:v1z, Float:v2x, Float:v2y, Float:v2z); // distance between 2 points (faster but less precise)
- A Faster floatsqrt (floatsquareroot)pawn Код:native Float:MPFSQRT(Float:value); // Faster sqrt (****** the 0x5f3759df method)
Other functions
pawn Код:
// pure math
native Float:FMPVecLength(Float:v1x, Float:v1y, Float:v1z); // calculates length of a simple XYZ 3d vector (FAST,less precision)
native Float:MPDistancePointLine(Float:PointX, Float:PointY, Float:PointZ, Float:LineSx, Float:LineSy, Float:LineSz, Float:LineEx, Float:LineEy, Float:LineEz); // http://paulbourke.net/geometry/pointline/ returns super huge number 10000000 if outside of range of specified the lie segment.
native Float:MPDotProduct(Float:v1x, Float:v1y, Float:v1z, Float:v2x, Float:v2y, Float:v2z);
native Float:MPVecLength(Float:v1x, Float:v1y, Float:v1z); // calculates length of a simple XYZ 3d vector
native MPCrossProduct(Float:v1x, Float:v1y, Float:v1z, Float:v2x, Float:v2y, Float:v2z, &Float:resx, &Float:resy, &Float:resz);
native MPFNormalize(&Float:vx, &Float:vy, &Float:vz); // fast float normalization of a vector to unit-length (makes whatever vector 1.0 long, purely to preserve direction and be able to scale it controllably)
native MPInterpolatePoint(Float:v1x, Float:v1y, Float:v1z, Float:v2x, Float:v2y, Float:v2z, &Float:resx, &Float:resy, &Float:resz, Float:distance);
SA-MP Developers/Beta testers of course
JernejL
* To the "this is not a tutorial users", I don't care.


.
