[Tutorial] Math Plugin - (some) functions explained and the awesome thing's it can do.
#1

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
  • pawn Код:
    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 point
    - Basically a faster
    pawn Код:
    Float: DistanceCameraTargetToLocation(Float:CamX, Float:CamY, Float:CamZ, Float:ObjX, Float:ObjY, Float:ObjZ,  Float:FrX, Float:FrY, Float:FrZ)
    http://forum.sa-mp.com/showthread.ph...rgetToLocation

    Example code

    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));
    }
    This is faster than the original one, and might be a little more accurate.

    I don't really know any other uses for this, if you have one please post it.

  • 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.
    This function can be used to flip automatically flip vehicles, it can be used to show where your vehicle is facing (horizontally).

    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

    If you can't understand that,

    pawn Код:
    //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.
    Now this function, is a very helpful, you may use it to detect your vehicle's rotation position

    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;
    }
  • 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.
    This function returns the 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;
    }
  • 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.
    This function 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);

  • pawn Код:
    native MPGetVehicleDriver(vehicleid);
    This function get's the driver of a vehicle

    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;
    }
  • 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)
    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.

    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;
    }
  • pawn Код:
    native MPGetVehicleOccupantCnt(vehicleid); // returns number of player a vehicle is carrying
    This function gets the number of passengers in a vehicle

    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;
    }
  • pawn Код:
    native MPGetVehicleSurfersCnt(vehicleid); // returns number of players surfing a vehicle
    This function gets the 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;
    }
  • pawn Код:
    native Float:MPClamp360(Float:value);
    The function basically clamps any value over 360.

    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
    pawn Код:
    public OnGameModeInit()
    {
        printf("%0.2f", MPClamp360(361.0));
        return 1;
    }
    That will return 1.00.
  • pawn Код:
    native Float:MPDistance(Float:v1x, Float:v1y, Float:v1z, Float:v2x, Float:v2y, Float:v2z);  // distance between 2 points
    This function is a very faster GetDistanceBetweenPoints, I did a speed test result with 100000 checks and the results were

    GetDistanceBetweenPoints - 73
    MPDistance - 21

    pawn Код:
    #define GetDistanceBetweenPoints MPDistance
  • pawn Код:
    native Float:MPFDistance(Float:v1x, Float:v1y, Float:v1z, Float:v2x, Float:v2y, Float:v2z); // distance between 2 points (faster but less precise)
    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:MPFSQRT(Float:value);  // Faster sqrt (****** the 0x5f3759df method)
    A Faster floatsqrt (floatsquareroot)
More function's explained to come.

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);
Credits:

SA-MP Developers/Beta testers of course
JernejL

* To the "this is not a tutorial users", I don't care.
Reply
#2

Fucking awesome .
Reply
#3

Great TUT.
Reply
#4

Turtles <3 Ok, Nice job on explaining the functions.
Reply
#5

Nice tutorial.
Reply
#6

I am really curious to see an example of MPProjectPointOnVehicle in a comment of the code JernejL says it could be useful to find each tire proximity. Is there an easy way to find that or how about to know a player is at the trunk (boot) of a vehicle? I was looking at this earlier and it still seems like I need a massive array to store the vehicle offsets for this or am I missing something?
Reply
#7

Awesome tutorial!
Reply
#8

Quote:
Originally Posted by cyber_punk
Посмотреть сообщение
I am really curious to see an example of MPProjectPointOnVehicle in a comment of the code JernejL says it could be useful to find each tire proximity. Is there an easy way to find that or how about to know a player is at the trunk (boot) of a vehicle? I was looking at this earlier and it still seems like I need a massive array to store the vehicle offsets for this or am I missing something?
I don't exactly know how to use that function completely, but heres a good way

But as you can see it returns the x y z angles, you should experiment with it

http://forum.sa-mp.com/showthread.ph...00#post1321900

you can change this Pos[2] + 0.5 * Pos[5] to and the 0.4 to get it to the tires. simple you need getvehiclesize by RyDeR`

Might I suggest maybe -0.1 and 0.35. try those, it should work with all vehicles, just adjust it to your needs
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)