SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 10/07/2012, 05:50 AM   #1
Kar
High-roller
 
Kar's Avatar
 
Join Date: May 2010
Location: Black Asylum
Posts: 3,182
Reputation: 504
Default Angle Issues: Setting a attached object on a vehicle to face a vehicle

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

Last edited by Kar; 10/07/2012 at 01:40 PM.
Kar is offline   Reply With Quote
Old 10/07/2012, 08:39 PM   #2
RyDeR`
High-roller
 
RyDeR`'s Avatar
 
Join Date: Feb 2009
Location: Belgium
Posts: 3,062
Reputation: 694
Default Re: Angle Issues: Setting a attached object on a vehicle to face a vehicle

Sure, try this:
pawn Code:
new
    Float: fAZ
;
GetVehicleZAngle(vehicleid, fAZ);
atan2(y - DestY, x - DestX) - fAZ - 180.0;
__________________

Sup?
RyDeR` is offline   Reply With Quote
Old 11/07/2012, 12:37 AM   #3
Kar
High-roller
 
Kar's Avatar
 
Join Date: May 2010
Location: Black Asylum
Posts: 3,182
Reputation: 504
Default Re: Angle Issues: Setting a attached object on a vehicle to face a vehicle

Ah the logic makes sense now, it works. Thanks!
Kar is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Object being attached to vehicle but don't deattach. DJGama101 Scripting Help 1 05/04/2012 08:22 PM
Vehicle attached to vehicle? Help Kasis Scripting Help 3 06/11/2011 02:02 PM
Setting vehicle facing angle Pooh7 Help Archive 10 20/07/2011 07:46 PM
Trailer attached to vehicle LegendNissanGTR Help Archive 0 11/12/2010 02:36 PM
Attached to Vehicle corne Help Archive 18 29/11/2010 11:09 AM


All times are GMT. The time now is 09:32 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.