Announcement

Collapse
No announcement yet.

Aiming a turret (Math question)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Aiming a turret (Math question)

    So I have a turret and I can't figure out the angles needed to aim the thing correctly. See the skeleton of the turret below and you will know what I mean:
    Click image for larger version

Name:	turret02_maked.png
Views:	1
Size:	49.4 KB
ID:	1225893

    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. Here's a picture of how the turrent looks when properly aimed:
    Click image for larger version

Name:	turret03_marked.png
Views:	1
Size:	15.4 KB
ID:	1225892

    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 following steps:
    1) Use law of sines to get the following:
    Click image for larger version

Name:	law_of_sines.png
Views:	1
Size:	8.3 KB
ID:	1225886
    2) Then the following holds:
    Click image for larger version

Name:	tan.png
Views:	1
Size:	8.4 KB
ID:	1225887
    3) So pluging in and simplifying:
    Click image for larger version

Name:	math.png
Views:	1
Size:	49.6 KB
ID:	1225885

    so you get down to a linear combination of sine and cosine which according to here (or https://en.wikipedia.org/wiki/List_o...r_combinations) is equal to the following:
    Click image for larger version

Name:	sum.png
Views:	1
Size:	18.4 KB
ID:	1225888

    which reduces to:
    Click image for larger version

Name:	result.png
Views:	1
Size:	16.3 KB
ID:	1225889

    But plugging this last equation into a blueprint the turret isn't aiming correctly:
    Click image for larger version

Name:	example_2.png
Views:	1
Size:	206.6 KB
ID:	1225890

    Click image for larger version

Name:	example.png
Views:	1
Size:	746.7 KB
ID:	1225891

    the "project onto plane polar coordiantes" node is doing just that (ie the brown line is the target's projection onto the white plane). the white plane with the XYZ coordinate axis drawn is the (x,y) target position space relative to the joint used to aim (red arrow) and is rendering because the "draw debug helpers" is checked on that node.

    I'm trying to find an analytical solution and not just iteratively reducing the aim error. I tried other methods but this is the simplest non-solution I could come up with. Any smarties out there that could scan the math and just tell me what stupid mistake I'm making? I would really appreciate it as this is bugging the hell out of me now. Thanks.
    Last edited by instantmelanie; 07-28-2017, 05:37 AM.

    #2
    Have you tried just doing a simple "Look At" matrix for the aiming?

    https://docs.unrealengine.com/latest...ion/index.html
    Able Ability System - A high performance, robust ability system for UE4. Now Available!

    Comment


      #3
      Originally posted by ExtraLifeMatt View Post
      Have you tried just doing a simple "Look At" matrix for the aiming?

      https://docs.unrealengine.com/latest...ion/index.html
      Thanks for the response, but unfortunately that doesn't work. The condition for the turret to be aimed properly isn't that one thing is looking at another thing. Because of the joints' offset, there is an angle calculation somewhere. It's more like an IK problem with an unknown end effector. Maybe there isn't an analytical solution. Anyways, still appreciate your response

      Comment


        #4
        You need to find the lookat rotation for the end of the turret and then offset it by the rotation delta between the base and the end of the turret.

        Comment


          #5
          Originally posted by RotemS View Post
          You need to find the lookat rotation for the end of the turret and then offset it by the rotation delta between the base and the end of the turret.
          It's not so simple, to find a lookat rotation requires the source location, but in this case the source location is itself a function of the rotation.

          [MENTION=1434]instantmelanie[/MENTION] I've been working on a generic targeting framework that uses the iterative approach you mentioned. It's totally fine in practice for most cases because your target distances generally dwarf your offsets and you also probably don't want to snap to the perfect rotation on a single frame anyway. Still, an exact solution for a known fixed offset might be something I'll want, so I had a bit of a scribble. A lot of a scribble actually, it's not completely trivial but I'm also pretty sure it shouldn't be so hard as I found it. If you picture it visually, the line of aim will obviously move round smoothly as you rotate, at some point passing through the target, so there should be a fairly straightforward analytical solution.

          Anyhow, I ended up doing a different derivation because when I started off trying to solve for theta I got in a mess pretty quick and my trig isn't what it used to be. I think what I got works, and plugging it back into the formula you derived, it looks like they are indeed just different forms of the same solution. I think you pretty much had it correct right up until the end. There will actually be two roots to the analytical solution (generally you can keep rotating around and the line of aim will hit the target backwards of the turret too). In my solution it came down to choosing the sign of a square root, I think in yours you have the same choice with your inverse sin (since the sine curve is symmetrical about 0). In the numbers I plugged in, I got what I think is the right answer by just taking the negative of the inverse sin term rather than the positive, but you may find it's not always that way and you may need to do some check to determine which one is correct in a given instance. To begin with though, I'd just stick a Multiply by -1 node after your ASINd and see how that works.

          Comment


            #6
            Is this the behavior you're looking for?

            Comment


              #7
              Yukova, I'm not OP, but I am trying to achieve a similiar behavior in your youtube video. Can you offer some insights?

              Comment


                #8
                namrog84, give me some time to clean up the example, and then I'll post a full explanation (with source).

                Comment


                  #9
                  I apologize for the really long wait, but I'm back with a full analytical explanation:


                  You can download the project yourself here: https://github.com/Konokai/UE4-OffsetTurretRotation

                  Let me know if you have any questions.

                  Comment


                    #10
                    The best way to do this, surely, would be to use the standard functionality for calculating the aim required for a turret, then offsetting the target position based on the muzzle offset from the rotation point?

                    Comment


                      #11
                      You can download the project yourself here: https://github.com/Konokai/UE4-OffsetTurretRotation
                      Thanks for the well documented code!

                      teak
                      "A little bit of nonsense now and then is cherished by the wisest men..."
                      -- Willy Wonka

                      Smooth Zoom Camera Plugin for 4.24 here.

                      Comment

                      Working...
                      X