Here is an update, the whole thing workes good when the gun is placed at the origin (0,0,0), but when it is moved it often becomes buggy.

The gun is made like this:

.....|.....

..\..|../

...\.|./

.....G

Sorry for all the dots, but it was the only way,

as the whith spaces was removed when I press edit

The senter arrow is where I want it to point, the two others are just to help the gun determin the shortest way to turn

Here is the code i am using:

{

//find information

vector $posGun = `xform -q -ws -t Gun`;

vector $posAim = `xform -q -ws -t Aim`;

vector $posAimLeft = `xform -q -ws -t AimLeft`;

vector $posAimRight = `xform -q -ws -t AimRight`;

vector $posTarget = `xform -q -ws -t Target`;

//Distance between Gun and Target

float $GunTargetDistX = ($posGun.x + $posTarget.x);

float $GunTargetDistZ = ($posGun.z + $posTarget.z);

float $GunTargetDist = sqrt(($GunTargetDistX*$GunTargetDistX)+($GunTargetDistZ*$GunTargetDistZ));

//print $GunTargetDist;

//Determin which way to turn, trail 1:

//-------------------------------------

//Using the angle for the between the target and the two aim helpers.

//turn in the direction where the angle is the smallest

float $angleLeft =rad_to_deg(angle($posTarget,$posAimLeft));

float $angleRight =rad_to_deg(angle($posTarget,$posAimRight));

float $angleMain =rad_to_deg(angle($posTarget,$posAim));

//Gun.LeftAngle = $angleLeft;

//Gun.RightAngle = $angleRight;

Gun.Cos = $angleMain;

if ($angleLeft <= $angleRight)

{

//print "true";

Gun.turner = 1;

}

else

{

//print "false";

Gun.turner = -1;

}

//A safity so the gun does not get stuck between two equal angles.

//sometimes if the two aim angles are almost the same the aimer get stuck

//between 1 and -1 for the direction. This fuction will give the Gun

//a push to one side of this happes.

//NOT working!!

if ((($angleRight - $angleLeft) < 2) || (($angleRight - $angleLeft) < -2))

print "Everything is fine";

else

print "Need help!";

//Determin which way to turn, trail 2:

//-------------------------------------

//using the distance between the target and the aim helpers

//turn the way the distance is the shortest

/*

float $posAimLeftX = ($posAimLeft.x + $posTarget.x);

float $posAimLeftZ = ($posAimLeft.z + $posTarget.z);

float $posAimLeftDist = sqrt(($posAimLeftX*$posAimLeftX)+($posAimLeftZ*$posAimLeftZ));

float $posAimRightX = ($posAimRight.x + $posTarget.x);

float $posAimRightZ = ($posAimRight.z + $posTarget.z);

float $posAimRightDist = sqrt(($posAimRightX*$posAimRightX)+($posAimRightZ*$posAimRightZ));

Gun.LeftAngle = $posAimLeftDist;

Gun.RightAngle = $posAimRightDist;

float $angleMain =rad_to_deg(angle($posTarget,$posAim));

Gun.SenterAngle = $angleMain;

if ($posAimLeftDist > $posAimRightDist)

{

//print "true";

Gun.turner = 1;

}

else

{

//print "false";

Gun.turner = -1;

}

*/

//turn the tower using rigid bodies

if ($angleMain < 1)

{

//print ("perfect match");

rigidBody1.spinImpulseY = 0;

}

else

{

//print ("NOT perfect match");

rigidBody1.spinImpulseY = 0.2 * Gun.turner;

}

}

Any one know why it acs weird when moved? I can also upload the scene if someone is interested.

Thanks in advance

EDIT:

It looks like the angle() command causes some truble in some cases, as it uses 2 points and asumes the third point is at (0,0). I think this will cause truble when the Gun is moved? Am i right?

I guess than I have to use the cosine rule:

angle= Cos-1 ((AB^2 +AC^2 + BC^2)/2 * AB * AC))

How do it get the Cos-1 in maya? Tryed with this, but it acts funny.

float $rotationAngle = cosd( ( ($GunAimDist*$GunAimDist)+($GunTargetDist*$GunTargetDist)+($AimTargetDist*$AimTargetDist) )

/ (2*$GunAimDist*$GunTargetDist));

CGTalk Moderation

10-11-2006, 08:27 AM

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.