SA-MP Forums Archive
Position of attached object - 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: Position of attached object (/showthread.php?tid=396117)



Position of attached object - CONTROLA - 29.11.2012

Hello! I've got a question: How can I get the position of an attached object?
Please help!


Re: Position of attached object - GiamPy. - 29.11.2012

Try this:
pawn Код:
/*
GetAttachedObjectPos

objectid - the objectid to which the attached object is attached to. (not the attached objectid)
Float:offset_x - the distance between the main object and the attached object in the X direction.
Float:offset_y - the distance between the main object and the attached object in the Y direction.
Float:offset_z - the distance between the main object and the attached object in the Z direction.
Float:x - the variable to store the X coordinate, passed by reference.
Float:y - the variable to store the Y coordinate, passed by reference.
Float:z - the variable to store the Z coordinate, passed by reference.
*/


stock GetAttachedObjectPos(objectid, Float:offset_x, Float:offset_y, Float:offset_z, &Float:x, &Float:y, &Float:z)
{
    new Float:object_px,
        Float:object_py,
        Float:object_pz,
        Float:object_rx,
        Float:object_ry,
        Float:object_rz;
    GetObjectPos(objectid, object_px, object_py, object_pz);
    GetObjectRot(objectid, object_rx, object_ry, object_rz);
    new Float:cos_x = floatcos(object_rx, degrees),
        Float:cos_y = floatcos(object_ry, degrees),
        Float:cos_z = floatcos(object_rz, degrees),
        Float:sin_x = floatsin(object_rx, degrees),
        Float:sin_y = floatsin(object_ry, degrees),
        Float:sin_z = floatsin(object_rz, degrees);
    x = object_px + offset_x * cos_y * cos_z - offset_x * sin_x * sin_y * sin_z - offset_y * cos_x * sin_z + offset_z * sin_y * cos_z + offset_z * sin_x * cos_y * sin_z;
    y = object_py + offset_x * cos_y * sin_z + offset_x * sin_x * sin_y * cos_z + offset_y * cos_x * cos_z + offset_z * sin_y * sin_z - offset_z * sin_x * cos_y * cos_z;
    z = object_pz - offset_x * cos_x * sin_y + offset_y * sin_x + offset_z * cos_x * cos_y;
}
Thanks to YJIET.


Re: Position of attached object - CONTROLA - 29.11.2012

Already tried it and it doesn't work, but maybe I did it wrong. Can you give me an example of usage of that stock? Thanks!


Re: Position of attached object - GiamPy. - 29.11.2012

Quote:

the objectid to which the attached object is attached to. (not the attached objectid)

You probably misread this.
You don't have to choose the objectid you attached to the object, but the objectid to WHICH you have attached the object.


Re: Position of attached object - CONTROLA - 29.11.2012

Well I attached the object to a vehicle.. How can I get the position of the attached object?


Re: Position of attached object - GiamPy. - 29.11.2012

Try using this then, you didn't say the vehicle before:
pawn Код:
stock GetAttachedObjectPos(vehicleid, Float:offset_x, Float:offset_y, Float:offset_z, &Float:x, &Float:y, &Float:z)
{
    new Float:deg;
    new Float:s;
    new Float:objectangle;
    new Float:pos[3];
    GetVehicleZAngle(vehicleid, deg);
    GetVehiclePos(vehicleid, pos[0], pos[1], pos[2]);
    s = floatsqroot(offset_x * offset_x + offset_y * offset_y);
    objectangle = atan(offset_y / offset_x);
    deg += 90.0;
    deg -= objectangle;
    x = pos[0] + s * floatcos(deg, degrees);
    y = pos[1] + s * floatsin(deg, degrees);
    z = pos[2] + offset_z;
}



Re: Position of attached object - CONTROLA - 29.11.2012

Still doesn't work. This is how I use it:

pawn Код:
new Float:X, Float:Y, Float:Z, Float:oX, Float:oY, Float:oZ;
GetAttachedObjectPos(vehicleid, X, Y, Z, oX, oY, oZ);
SetPlayerPos(playerid, oX, oY, oZ);
^ It teleports me to an invalid position so my client crashes.


Re: Position of attached object - GiamPy. - 29.11.2012

Maybe explaining me the reason of this would be helpful.