PDA

View Full Version : Formula to create an object directly under a player, flat


Jack_Leslie
07/05/2016, 10:41 AM
What would the co-ordinates be to create an object underneath a player, flat, no matter where they're standing? So on a hill, angle etc

I'm not good at maths and the formula would be highly appreciated, thanks in advance!

RaPSoR
07/05/2016, 10:46 AM
You can use MapAndreas.
http://forum.sa-mp.com/showpost.php?p=3130004&postcount=153

Ivan_Ino
07/05/2016, 10:53 AM
Try with ColAndreas with this line maybe

CA_RayCastLineAngle (Float:StartX, Float:StartY, Float:StartZ, Float:EndX, Float:EndY, Float:EndZ, &Float:x, &Float:y, &Float:z, &Float:rx, &Float:ry, &Float:rz)

StartX,StartY,StartZ Start position of ray cast.
EndX,EndY,EndZ Endposition of ray cast.
&x,&y,&z Collision position is stored in these references.
&rx,&ry,&rz Rotation of surface of collision.

PrO.GameR
07/05/2016, 11:00 AM
Directly under a player? I believe sa-mp's skin heights are all(or almost all) the same being 1.8 or 2.0, so you could get the player'pos and just create an object 0.9-1.0 below the z you get, and it will be directly under him, however if you want to create the object on the land/object under him, you need to either use MapAndreas or ColAndreas(recommended).

Jack_Leslie
07/05/2016, 11:10 AM
Directly under a player? I believe sa-mp's skin heights are all(or almost all) the same being 1.8 or 2.0, so you could get the player'pos and just create an object 0.9-1.0 below the z you get, and it will be directly under him, however if you want to create the object on the land/object under him, you need to either use MapAndreas or ColAndreas(recommended).

Yeah it's just the angle that I need to formulate, z-1 pretty much creates it directly under him, then the rotation angle.

MapAndreas didn't quiet do it:
http://i65.tinypic.com/2djtgt1.png

Did I use it right?

new Float: x, Float: y, Float: z, Float:a;
GetPlayerFacingAngle(playerid, a);
GetPlayerPos(playerid, x, y, z);
MapAndreas_FindZ_For2DCoord(x, y, z);

PrO.GameR
07/05/2016, 11:47 AM
Yeah but sadly MapAndreas won't provide the rotation, you could create some function to do it for you (checking Z of the area around yours, providing an aproximate angle), but it's like reinventing the wheel so just use ColAndreas for that matter.

Pottus
07/05/2016, 11:48 AM
Yeah you need ColAndreas it will give you the surface angle but you still need to figure out offsets. Once you have your angles you can rotate the object on the z-axis and still maintain the correct angle with this function.

stock ObjectRotateZ(Float:RX, Float:RY, Float:RZ, Float:rot_z, &Float:NewRX, &Float:NewRY, &Float:NewRZ)
{
new
Float:sinx,
Float:siny,
Float:sinz,
Float:cosx,
Float:cosy,
Float:cosz;

FloatConvertValue(RX, RY, RZ, sinx, siny, sinz, cosx, cosy, cosz);
// Convert from one euler angle sequence (ZXY) to another and add the rotation
FloatConvertValue(asin(cosx * cosy), atan2(sinx, cosx * siny) + rot_z, atan2(cosy * cosz * sinx - siny * sinz, cosz * siny - cosy * sinx * -sinz),
sinx, siny, sinz, cosx, cosy, cosz);

// Convert back to the original euler angle sequence and apply the new rotation to the object
NewRX = asin(cosx * siny),
NewRY = atan2(cosx * cosy, sinx),
NewRZ = atan2(cosz * sinx * siny - cosy * sinz, cosy * cosz + sinx * siny * sinz);
return 1;
}

stock FloatConvertValue(Float:rot_x, Float:rot_y, Float:rot_z, &Float:sinx, &Float:siny, &Float:sinz, &Float:cosx, &Float:cosy, &Float:cosz)
{
sinx = floatsin(rot_x, degrees);
siny = floatsin(rot_y, degrees);
sinz = floatsin(rot_z, degrees);
cosx = floatcos(rot_x, degrees);
cosy = floatcos(rot_y, degrees);
cosz = floatcos(rot_z, degrees);
return 1;
}

Jack_Leslie
08/05/2016, 01:18 AM
Yeah you need ColAndreas it will give you the surface angle but you still need to figure out offsets. Once you have your angles you can rotate the object on the z-axis and still maintain the correct angle with this function.

stock ObjectRotateZ(Float:RX, Float:RY, Float:RZ, Float:rot_z, &Float:NewRX, &Float:NewRY, &Float:NewRZ)
{
new
Float:sinx,
Float:siny,
Float:sinz,
Float:cosx,
Float:cosy,
Float:cosz;

FloatConvertValue(RX, RY, RZ, sinx, siny, sinz, cosx, cosy, cosz);
// Convert from one euler angle sequence (ZXY) to another and add the rotation
FloatConvertValue(asin(cosx * cosy), atan2(sinx, cosx * siny) + rot_z, atan2(cosy * cosz * sinx - siny * sinz, cosz * siny - cosy * sinx * -sinz),
sinx, siny, sinz, cosx, cosy, cosz);

// Convert back to the original euler angle sequence and apply the new rotation to the object
NewRX = asin(cosx * siny),
NewRY = atan2(cosx * cosy, sinx),
NewRZ = atan2(cosz * sinx * siny - cosy * sinz, cosy * cosz + sinx * siny * sinz);
return 1;
}

stock FloatConvertValue(Float:rot_x, Float:rot_y, Float:rot_z, &Float:sinx, &Float:siny, &Float:sinz, &Float:cosx, &Float:cosy, &Float:cosz)
{
sinx = floatsin(rot_x, degrees);
siny = floatsin(rot_y, degrees);
sinz = floatsin(rot_z, degrees);
cosx = floatcos(rot_x, degrees);
cosy = floatcos(rot_y, degrees);
cosz = floatcos(rot_z, degrees);
return 1;
}

I still don't understand in what way I would use what functions/calls to set the objects rotational position/offset :confused:

PrO.GameR
08/05/2016, 10:17 AM
This is what I understood from Pottus's post:

Get offsets of each weapon ready so it sits perfectly on a 0,0,0 angle object
Get ground Z angle
feed them both to this function, get new offsets for wherever you want to drop
ObjectRotateZ(Float:RX, Float:RY, Float:RZ, Float:rot_z, &Float:NewRX, &Float:NewRY, &Float:NewRZ)
create object there