Announcement

Collapse
No announcement yet.

How to "spin" a rotator?

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

    How to "spin" a rotator?

    Hey all,

    I'm trying to add bullet spread (inaccuracy) to my weapon; it's hard to describe in words, so here's a diagram:



    Now, that's what should happen.

    But when I try to add the "Spin" in step two, it doesn't work, nothing changes. My dots end up in a straight line, only randomized because of the pitch angle. I think Unreal is trying to add the roll relative to the already rotated angle, not the original frame of reference, so nothing happens. So, how can I add to a rotator, relative to the original rotation? (I tried adding the roll first, it didn't help.)

    I hope I've been clear enough; if not, please say so!

    EDIT: haha, forgot to post my code.

    FRotator RandomRot;
    FRandomStream RandStream;
    RandStream.GenerateNewSeed();

    // Step 1, add random pitch. (5 is just an arbitrary number.)
    RandomRot.Pitch = RandStream.FRand() * 5;

    // Step 2, add random roll (spin)
    RandomRot.Roll = RandStream.FRand() * 360;

    // It doesn't matter what order I do steps 1 and 2 in, the result is the same.

    // Now when I spawn a bullet with rotation RandomRot, it's adding the roll relative to the pitch, meaning that the actual bullet is being spun.
    Last edited by gregdumb; 04-29-2014, 12:06 AM.
    Fully dynamic Time of Day System blueprint: https://forums.unrealengine.com/show...e-of-Day-Setup Download it for free now!

    #2
    I can't actually help you because I'm at work, but I can tell you why you're having that problem. Hopefully someone else can fill in the blanks.

    You need to remember that when you add Pitch, Roll or Yaw to a Rotator, you're adding it in LOCAL space - or - you're applying those transforms relative to the rotators current position. What you really want to be doing is taking a copy of your original rotator, let's call it GunRotator and making a BulletRotator. You then rotate BulletRotator on BulletRotator.pitch by your random arc. You then want to rotate BulletRotator, as a whole, by a random degree of roll from GunRotator... so we're saying, rotate our offset BulletRotator by GunRotator's roll.

    I can't help you with the actual code, but;
    BulletRotator = GunRotator
    BulletRotator.pitch += random()
    // TODO: Magic where BulletRotator is now moved relative to GunRotators roll, by a random degree.

    Comment


      #3
      Yes, this is EXACTLY what I want to do! But how do I do it? Is there a special "MoveRelativeToOtherRotator" function that I'm missing or something?
      Fully dynamic Time of Day System blueprint: https://forums.unrealengine.com/show...e-of-Day-Setup Download it for free now!

      Comment


        #4
        Still can't help you as I haven't been near the code today and the docs are still down.

        You could do a quick-and-dirty version of this by just adding a random value to the rotators Pitch and Yaw respectively. If you fire enough bullets your pattern would come out square, so this totally isn't the right way to do it, but it'll get you out of this snag and can be fixed later when someone posts the solution.

        Comment


          #5
          Originally posted by V1nce View Post
          You could do a quick-and-dirty version of this by just adding a random value to the rotators Pitch and Yaw respectively. If you fire enough bullets your pattern would come out square, so this totally isn't the right way to do it, but it'll get you out of this snag and can be fixed later when someone posts the solution.
          Haha, that's what I was doing in UDK I wanted to actually do it properly this time though. Still, it'll tide me over until I, you, or somebody else can figure this out.
          Fully dynamic Time of Day System blueprint: https://forums.unrealengine.com/show...e-of-Day-Setup Download it for free now!

          Comment


            #6
            If you're doing this in code, check out the VRandCone() functions in UnrealMathUtility.h. They'll return a vector within a specified cone, uniformly distributed. Perfect for weapon spread.

            Those seem like good functions to expose to Blueprint, actually -- I'll look into that.
            Senior Engine Programmer, UE4, Epic Games
            Twitter:
            @Byooler

            Comment


              #7
              Thanks Jeff! Didn't know that function existed, will definitely come in handy

              And, by examining the definition for it, I figured out that the function I was looking for earlier is FVector::RotateAroundAxis, which, as it says, rotates a vector around a custom axis. So my original question is also answered Thanks!
              Fully dynamic Time of Day System blueprint: https://forums.unrealengine.com/show...e-of-Day-Setup Download it for free now!

              Comment

              Working...
              X