Jump to content
  • Advertisement

Aiming turret (math question)

This topic is 798 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I really can't figure out the angles needed to aim a certain turret rig I have. The skeleton looks like first image.

The red arrow is pointing to the joint that is used to aim (via skeletal controller) and the blue arrow is pointing to the joint that needs to be aimed ie this joints forward x vector needs to point to the target. When aimed the turret likes like the second image.

The red dot is the red joint being controlled and the blue dot is the blue joint being aimed. I just can't seem to calculate theta as a function of (x,y) the targets position.

So the first thing I tried is the math in third image.

The first line we get from the law of sines, the second line is just the definition of tangent, the third line is plugging the first equation into the second, and the fourth line is obtained from the general formula for a linear combination of sine and cosine (https://en.wikipedia.org/wiki/List_of_trigonometric_identities#Linear_combinations). The last line is theta as a function of (x,y) simplified which is my goal. But trying all this out in UE4 I get something that's obviously not correct (see last image). I have a feeling the math is wrong.

Math isn't really my wheelhouse and I'm surprised I even came up with anything even it appears wrong. Anyone able to tell me what stupid overcomplicated mistake I'm making in the math? I would really like an analytical solution and not just iteratively error correct it into aiming correctly. I would really appreciate a pair of eyes on this. Thanks!







Edited by instantmelanie

Share this post

Link to post
Share on other sites

do you not have the value of h? You could just go arctan(y/(x+h)) = theta. Have you tried that already? I see what you did, nvm let me look further


do you know they bounds of x and y? You are using an arcsin to get to your last step and the domain of it may be causing you some issues. your sin^-1() in the last part of your equation will not work for values outside of the bounds of x and y with -1<x<1 and -pi/2<y<pi/2

Edited by abominableCodeman

Share this post

Link to post
Share on other sites

You shouldn't need the angles, if you can set the joint's matrix.

I'll label your matricies from the base to the barrel as: A (base), B(joint), C(barrel base), D(barrel end)

Firstly, you have a target to find, relative to your joint:

target_dir = normalize(target_pos - B.pos())

Secondly, your barrel is offset from the joint:

offset = D * inv(B)

Thirdly, you need to apply that offset to get the correct position, which means rotating your turret into the same plane as the target and applying the inverse of the offset to the target position.

offset_pos = target_pos * look_at(target_dir) * inv(offset)

Lastly, you have your corrected target position, so you can look at it relative to world space, and multiply by your turret's bind pose to get B in the correct spot.

B = look_at(normalize(offset_pos - joint_pos)) * joint_bind_matrix

Where "look_at" if your library doesn't define it is basically building a matrix from a guess of the "up" direction combined with your desired "at" direction:

look_at(dir) {
  left = cross(up_guess, dir);
  up = cross(dir, left);
  return [at, up, left, zero_vec];


However, if you do actually need the angle, you can extract them from that final matrix's `at` vector.


Edited by KulSeran

Share this post

Link to post
Share on other sites

Having gone through what you are doing now, around turret aiming, you have identified much of the problem.  But you probably need to simplify.  There are a number of spaces you need to consider.  When I did this, you need to keep local and world space in mind.  You calculate your target vector in the world space, but then apply your rotations in local space (hope I can articulate this well enough, im not that good at explaining it!)  

A) you obviously have a target vector, I like yourself want to calculate that from barrel of the gun.  This as you know is executing the matrix mutliplications from base to barrel, that is calculating barrels position.  

Base/Origin matrix -> First attachment.

First attachment to second attachment

Second attachment to barrel.

You have the location and you then can calculate from that the direction vector to the target point.  That's the first bit done.

Now, im going to assume that your first attach point rotates in say the X/Z plane.  Therefore you need to map our target vector on to that plane.   That allows you to set the direction of the connection point on that plane by getting the angle between the reference direction of the model and direction on the X/Z plane.  This leaves only the Y component for your 2nd connection point, but you need to base the elevation off the distance in the XZ plane.


The key points, because you will probably be a little confused here.  I base all my angles to rotate based off the reference direction of the model in relation to the vector in a specific plane, ala YAW for XZ and Pitch in XY.

I have working example this in my game (https://www.facebook.com/InsaneSoftware.com.au/).  The Gun turrets follow you on both static and moving models.  My code model is a little more complicated as I assign rotation axis on each connection point and it takes account of say moving ships with moving rotating turrets with accurate barrel ejection points.  But you get the idea, if you need some help just PM me.  I will try and elaborate offline.   My models also consider optimal rotation direction, blind spot (gun arc limitations) etc.


Edit: I assume your barrel ejection point as a direction Vector, This is key to how I solved my rotation problems.

Edited by ErnieDingo

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!