1
$\begingroup$

I'm attempting to add gun turret style tracking to objects that have been placed onto the polygons of another object using Animation Nodes 2.1. I'm currently attempting this with blender's built in constraints system, but that has not worked, which I'll detail later.

My setup consists of 5 objects, 1 object which has its polygons & polygon normals used to place the 3 next objects; the 2nd object is the static "base" of the turret; the 3rd object is the yaw aspect of the turret, which should only rotate around its local z axis, with the other 2 axes being locked; the the 4th object is the pitch aspect of the turret (the barrel, in this case a scaled cube), which should only rotate along its y axis (between -180° and 0°) and have the same z rotation as the yaw aspect, with only the x axis locked; the 5th object is an empty, to be tracked.

The pitch and yaw objects both have constraints set so that they'll track the empty with their appropriate axes locked, as described above. On their own, before animation nodes is involved, they track the empty as expected.

When I instantiate the appropriate number of objects, and place them on the polygons with Animation Nodes however, each instantiated object seems to lose the rotation of the polygon they're on. They do track the empty, but will rotate rotate around these incorrect local axes. I suspect that this may be because the constraints' transformations may apply before animation nodes' transformations apply.

Is there any way to work around this issue, or would it be possible to altogether avoid using blender's built in constraints system and instead simulate the effect entirely in animation nodes? The latter option seems that it'd be more flexible, however I do not know how to replicate the effect of blender's constraints in animation nodes while also maintaining the position on the objects on their polygons.

Here's a sample blend file:

$\endgroup$
5
$\begingroup$

I am going to discuss a pure Animation Nodes approach instead of of using constraints, because, as you said, it is simpler and more flexible.

Base

The base is simple, we instance the base object, position it based on the polygons centers, and orient it based on the polygons normals as follows:

Base

Turret

For the turret, the same will apply, but we are going to use the vector directed from the turret to the target empty as the guide for the Direction To Rotation node. We can compute that vector by subtracting the centers from the target location as follows:

Turret

The output rotation matrix is something we will use later, so make sure to expose it as an output.

Barrel

The barrel have the same orientation as the turret but it can rotate around its local Y axis, to rotate the barrel around the local Y axis, we multiply the rotation matrix we computed before by a Y rotation matrix. The location is also the same except it is shifted along the normal slightly, to do this shifting, we simply add a scalar multiple of the normal to the location as follows:

Barrel

Now the only thing left to do is to compute the angle at which we are going to rotate each barrel along the local Y axis. To compute that angle, we are going to compute the angle between the the vector along the length of the barrel and the vector directed from the barrel's location to the orthogonal projection of the target empty on the plane of barrel, this angle:

Angle

Does that make sense? We will implement that as follows:

Barrel Rotation

There is something slightly wrong with this implementation, the angle is not directed, it is, in fact, the smallest angle between both vectors. To get the signed angle (Directed angle) we can either use the Quaternion Output or investigate the cross product, we will be using the cross product method here as it is simpler. We compute the cross product between the two vectors, normalize the output, subtract it from the normal of the plane, return the sign -1 if its length is close to zero and 1 if not, then multiply the signs to the angle as follows:

Signed Angle

One last thing to do is clamp the rotation so that the barrel doesn't go through the base, and maybe expose the limits as parameters. So the final result is:

Result

Blend file:

$\endgroup$
  • $\begingroup$ Wow, this is perfect! Thank you! One question about the Barrel subprogram, is there a specific reason that you're using radians as opposed to degrees or is it just up to personal preference? $\endgroup$ – Amelia Oct 15 '18 at 16:30
  • 1
    $\begingroup$ @Amelia Glad I could help. Animation Nodes uses radians by default in everything, so using degrees will only bring trouble. Blender also converts everything you input into radians. So generally, I never use degrees unless it is a value that will be exposed to the user, since most people are used to degrees. $\endgroup$ – Omar Emara Oct 15 '18 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.