SA-MP Forums Archive
Modulus/maths help; race spawns fails, something wrong ? - 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)
+--- Thread: Modulus/maths help; race spawns fails, something wrong ? (/showthread.php?tid=545615)



Modulus/maths help; race spawns fails, something wrong ? - denNorske - 09.11.2014

Hello guys!

Me and a friend made this little snippet in order to get players spawning in rows and columns.
However, let me explain:

pawn Код:
//startPosition: ("*" is the checkpoint they are headed against)
//  *
//1 2 3
//4 5 6
//7 8 9
//.......

stock GetXYForRacer(&Float:x, &Float:y, Float:a, startPosition)
{
    //x and y should contain the start checkpoint location, a should be the angle of the cars
    new Float:raceSpawnOffsets[2];
    if(((startPosition+1) % 3) == 0)  //I think there is something wrong here, because it will be the same as startposition = 2 on the first and and so on, but how to do the "%" calculations correctly?
    { //Middle
        GetXYAtAngleFromPos(raceSpawnOffsets[0], raceSpawnOffsets[1], a-180.00, 5);
        x += raceSpawnOffsets[0] * (((startPosition-1)/3)+1);
        y += raceSpawnOffsets[1] * (((startPosition-1)/3)+1);
    }
    else
    { //Sides
        GetXYAtAngleFromPos(raceSpawnOffsets[0], raceSpawnOffsets[1], a+90.00, 3);
        if((startPosition % 3) == 0)  
        { //Right side
            x += raceSpawnOffsets[0];
            y += raceSpawnOffsets[1];
        }
        else
        { //Left Side
            x -= raceSpawnOffsets[0];
            y -= raceSpawnOffsets[1];
        }
    }
}
stock GetXYAtAngleFromPos(&Float:x, &Float:y, Float:a, distance)
{
    x += (distance * floatsin(-a, degrees));
    y += (distance * floatcos(-a, degrees));
}


And this is what happens when testing the code above:



Please note that the cars are increasing the offsets further back.
And that there is only 2 rows, where the third vehicle spawn on top of the other. (just placed them side by side)

What should be done here?
And it wouldn't matter if i had two rows only, but how could i do that? It kinda sucks when you work with things you never thought you had use for, so i am open to hear anything to learn.


Re: Modulus/maths help; race spawns fails, something wrong ? - Toxik - 09.11.2014

maybe u got some codes witch create dubble vehicle . ??!?


Re: Modulus/maths help; race spawns fails, something wrong ? - denNorske - 09.11.2014

Quote:
Originally Posted by Toxik
Посмотреть сообщение
maybe u got some codes witch create dubble vehicle . ??!?
Nope i don't. The third vehicle is supposed to spawn on a third row, which never happens.


Re: Modulus/maths help; race spawns fails, something wrong ? - DavidBilla - 09.11.2014

I do not have experience with dealing with mathematical calculations In pawn, but since you asked about the usage of '%' in pawn,let me explain it.
% is similar to / but instead of giving the quotient as a result, it gives the remainder as result.

Eg: c=15/10
Here c will give you the value 1 (Quotient)
Eg: c=15%10
Here c will give you the value 5 (since the remainder is 5)


AW: Modulus/maths help; race spawns fails, something wrong ? - Nero_3D - 09.11.2014

rowSize is the vehicle count pro row, also 3 in your case
pawn Код:
// this function only gets the next position! can't be used for random positions
// x, y and startPosition must be variables, they change which each call
stock GetXYForRacer(& Float: x, & Float: y, Float: a, rowSize, & startPosition) {
    if(rowSize < 1) {
        rowSize = 1;
    }
    const
        Float: rowSpace = 5.0,
        Float: columnSpace = 3.0
    ;
    if(++startPosition <= 1) {
        startPosition = 1;
        // moving 0.5 row to the left because the startPos is in the middle
        GetXYAtAngleFromPos(x, y, a + 90.0, (columnSpace * (rowSize - 1)) / 2.0);
    } else {
        if((startPosition % rowSize) == 1) {
             // moving the pos to the left again
            GetXYAtAngleFromPos(x, y, a + 90.0, columnSpace * (rowSize - 1));
            // and one row down
            GetXYAtAngleFromPos(x, y, a + 180.0, rowSpace);
        } else {
            // moving one to the right
            GetXYAtAngleFromPos(x, y, a - 90.0, columnSpace);
        }
    }
}
If you want to get the angle more accurate you should get a startPosition and a endPosition along the road (the distance doesn't matter) but higher distance, higher accuracy
pawn Код:
stock Float: GetAngleFromLine(Float: sX, Float: sY, Float: eX, Float: eY) {
    return -atan2(eX - sX, eY - sY);
}



Re: Modulus/maths help; race spawns fails, something wrong ? - denNorske - 09.11.2014

Here is a video explaining /showing in detail what which is happening.

http://*********/YPA1tFCP-XI


Re: AW: Modulus/maths help; race spawns fails, something wrong ? - denNorske - 09.11.2014

Quote:
Originally Posted by Nero_3D
Посмотреть сообщение
If you want to get the angle more accurate you should get a startPosition and a endPosition along the road (the distance doesn't matter) but higher distance, higher accuracy
Ah thanks alot Nero3D! I were just setting specific coordinates (calculated) instead of just adding the floats like you did.

And the angle makes sense yes, i'll test that. However, i assume the the value returned is dependable on which direction you use it. So, nice of you defining "e" and "s" in it (or else i would have to search it up heh)


Let me try this, thanks again


AW: Modulus/maths help; race spawns fails, something wrong ? - Nero_3D - 09.11.2014

Well I dislike this kind of function which goes from one to another position because

- You need to store x, y and startPostion global
- You only get the next position

Would be easier to use a function which start always from the starting point than you only need to save startPosition global

/ Don't quote code, it can change


Re: Modulus/maths help; race spawns fails, something wrong ? - denNorske - 09.11.2014

The above function does not work Nero_3D.

Im ending up randomly left left left then right then middle and so on .

What if we say i need 2 columns ? Will it make it any easier; by using that method above?
Ofcourse it is much better to do it this way instead of doing a function 500 times btw ?


AW: Modulus/maths help; race spawns fails, something wrong ? - Nero_3D - 09.11.2014

The code does work
pawn Код:
// global
enum eRaceData {
    Float: rX,
    Float: rY,
    Float: rA,
    rRowSize,
    rStartPos
}
new gRaceData[eRaceData];
// command to init a race, lets say we init the default race
    switch(strval(params)) {
        case 0: {}
        default: {
            gRaceData[rStartPos] = 0; // needs to be 0
            gRaceData[rRowSize] = 3; // Vehicles / row
            gRaceData[rX] = 0.0; // Start X
            gRaceData[rY] = 0.0; // Start Y
            gRaceData[rA] = 0.0; // Angle
        }
    }
// command to join the race
    GetXYForRacer(gRaceData[rX], gRaceData[rY], gRaceData[rA], gRaceData[rRowSize], gRaceData[rStartPos]);

    printf("%d. %f %f", gRaceData[rStartPos], gRaceData[rX], gRaceData[rY]);

/* should output

1. -3.000000 0.000000
2. 0.000000 0.000000
3. 3.000000 0.000000
4. -3.000000 -5.000000
5. 0.000000 -5.000000
6. 3.000000 -5.000000
7. -3.000000 -10.000000
8. 0.000000 -10.000000
9. 3.000000 -10.000000
*/