Author Topic: SOLVED: Arctangent function returns bad values  (Read 435 times)

Don't ask me to "check my code because I don't know what the hell this math does" because that doesn't contribute anything to this thread, because I don't know what the heck is going on.

I am trying to find the angle of ascent on a vehicle.
Code: [Select]
//Slope calculation
function gameConnection::GPWS_Tick3(%this)
{
if(isObject(%this.player))
%vehicle = %this.player.getObjectMount();
else
return;

if(isObject(%vehicle))
{
%isDriving = (%this.player.getObjectMount().getControllingClient() == %this);
%isFlyableVehicle = (%vehicle.getClassName() $= "FlyingVehicle" ? 1 : %vehicle.getDatablock().lift > 0); //a way to check if WheeledVehicles can fly
}
else
return;

if(%isDriving && %isFlyableVehicle)
{
%vehicle.pos100MSago = %vehicle.position;
%this.schedule(500,"gpws_slopecalc", %vehicle);
}
}

function gameConnection::GPWS_SlopeCalc(%this, %vehicle)
{
if(isObject(%vehicle))
{
%isDriving = (%this.player.getObjectMount().getControllingClient() == %this);
%isFlyableVehicle = (%vehicle.getClassName() $= "FlyingVehicle" ? 1 : %vehicle.getDatablock().lift > 0); //a way to check if WheeledVehicles can fly
}
else
return;
if(%isDriving && %isFlyableVehicle)
{
%p1_x = getword(%vehicle.pos100msago, 0);
%p1_y = getword(%vehicle.pos100msago, 1);

%p2_x = getword(%vehicle.position, 0);
%p2_y = getword(%vehicle.position, 1);
echo(%p1_x SPC %p1_y SPC %p2_x SPC %p2_y);

%xdist = %p2_x - %p1_x;
%ydist = %p2_y - %p1_y;

echo(%xdist SPC %ydist);

%runvector = msqrt( (%xdist * %xdist) + (%ydist * %ydist) ); //pythagorean theorem

echo(%runvector);

%angle = mRadToDeg(matan(%ydist, %runvector));

if(%ydist == 0)
%angle = 0;
else if(%ydist < 0)
%angle = 0 - %angle;

%vehicle.angleOfAscent = %angle;
}
}
« Last Edit: January 03, 2013, 04:03:18 PM by Axolotl2 »

In what case does it return 44 when it's not supposed to?

In what case does it return 44 when it's not supposed to?
While the vehicle clearly has an angle of ascent of 45 degrees, it simply just says 10, and it increases as I move away from the origin.

Oh look

I'm a complete dumbass and forgot to use the Z distance.

EDIT: Fixed this stuff.
Code: [Select]
//Slope calculation
function gameConnection::GPWS_Tick3(%this)
{
if(isObject(%this.player))
%vehicle = %this.player.getObjectMount();
else
return;

if(isObject(%vehicle))
{
%isDriving = (%this.player.getObjectMount().getControllingClient() == %this);
%isFlyableVehicle = (%vehicle.getClassName() $= "FlyingVehicle" ? 1 : %vehicle.getDatablock().lift > 0); //a way to check if WheeledVehicles can fly
}
else
return;

if(%isDriving && %isFlyableVehicle)
{
%vehicle.pos100MSago = %vehicle.position;
%this.schedule(500,"gpws_slopecalc", %vehicle);
}
}

function gameConnection::GPWS_SlopeCalc(%this, %vehicle)
{
if(isObject(%vehicle))
{
%isDriving = (%this.player.getObjectMount().getControllingClient() == %this);
%isFlyableVehicle = (%vehicle.getClassName() $= "FlyingVehicle" ? 1 : %vehicle.getDatablock().lift > 0); //a way to check if WheeledVehicles can fly
}
else
return;
if(%isDriving && %isFlyableVehicle)
{
%p1_x = getword(%vehicle.pos100msago, 0);
%p1_y = getword(%vehicle.pos100msago, 1);
%p1_z = getword(%vehicle.pos100msago, 2);

%p2_x = getword(%vehicle.position, 0);
%p2_y = getword(%vehicle.position, 1);
%p2_z = getword(%vehicle.position, 2);

%xdist = %p2_x - %p1_x;
%ydist = %p2_y - %p1_y;
%zdist = %p2_z - %p1_z;

%runvector = msqrt( (%xdist * %xdist) + (%ydist * %ydist) ); //pythagorean theorem

%vehicle.angleOfAscent = mRadToDeg(matan(%zdist, %runvector));
}
}
« Last Edit: January 03, 2013, 04:03:55 PM by Axolotl2 »

Computers don't make mistakes!

Computers don't make mistakes!
They do if you tell them to.

PARADOX GET