Angle Issues: Setting a attached object on a vehicle to face a vehicle -
Kar - 10.07.2012
Ok, for awhile now I\'ve been trying to set a minigun machine to face another vehicle\'s position. But it\'s not working correctly, I tried all kinds of different methods.. with some methods it works when both vehicles are at certain angles to each other but otherwise it won\'t work.
This video will explain everything.
[ame]http://www.youtube.com/watch?v=LqsTQy03fXs[/ame]
Here\'s the code(s) I\'ve used.
pawn Code:
new Float:x, Float:y, Float:z, Float:x2, Float:y2, Float:Angle, Float:damage,
closestvehicle = GetClosestVehicle(playerid, 25.0);
GetVehiclePos(vehicleid, x, y, z);
GetVehiclePos(closestvehicle, x2, y2, z);
if(closestvehicle == INVALID_VEHICLE_ID || !(1 <= closestvehicle <= 2000))
{
Angle = 95.0; // this is the angle whereas the minigun faces to the front point of the vehicle.
}
else
{
//Angle = (95.0 + 360.0) - atan2( x - x2, y - y2 );
//Angle = 95.0 - atan2(x - x2, y - y2);
//Angle = atan2(y - y2, x - x2) - 90.0 + 95.0;
//Angle = atan2(y - y2, x - x2) - 90.0;
//Angle = atan2(DestY - y, DestX - x) + 270.0;
Angle = PointAngle(vehicleid, x, y, DestX, DestY) - 180.0;
MPClamp360(Angle);
}
//SendClientMessageFormatted(playerid, -1, "[System] - playerid: %d - closestvehicleid: %d - Angle: %f - Angle2D - 95.0: %f", playerid, closestvehicle, Angle, (Angle - 95.0));
AttachObjectToVehicle(PlayerInfo[playerid][pSpecial_Missile_Object], vehicleid, 0.1, -0.5, 1.5, 0.0, 30.0, Angle);
EDIT: PointAngle seems to do the trick.. wondering if there is any other way
pawn Code:
stock Float:PointAngle(vehicleid, Float:xa, Float:ya, Float:xb, Float:yb)
{
new Float:carangle, Float:xc, Float:yc, Float:angle;
xc = floatabs(floatsub(xa, xb));
yc = floatabs(floatsub(ya, yb));
if (yc == 0.0 || xc == 0.0)
{
if(yc == 0 && xc > 0) angle = 0.0;
else if(yc == 0 && xc < 0) angle = 180.0;
else if(yc > 0 && xc == 0) angle = 90.0;
else if(yc < 0 && xc == 0) angle = 270.0;
else if(yc == 0 && xc == 0) angle = 0.0;
}
else
{
angle = atan(xc / yc);
if(xb > xa && yb <= ya) angle += 90.0;
else if(xb <= xa && yb < ya) angle = floatsub(90.0, angle);
else if(xb < xa && yb >= ya) angle -= 90.0;
else if(xb >= xa && yb > ya) angle = floatsub(270.0, angle);
}
GetVehicleZAngle(vehicleid, carangle);
return floatadd(angle, -carangle);
}
Re: Angle Issues: Setting a attached object on a vehicle to face a vehicle -
RyDeR` - 10.07.2012
Sure, try this:
pawn Code:
new
Float: fAZ
;
GetVehicleZAngle(vehicleid, fAZ);
atan2(y - DestY, x - DestX) - fAZ - 180.0;
Re: Angle Issues: Setting a attached object on a vehicle to face a vehicle -
Kar - 11.07.2012
Ah the logic makes sense now, it works. Thanks!