Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

FRotator off by 90 or wrong axis?

I am taking an example from a diffrent project and re-creating it in my own game. In the original working project, there is a gun that rotates around the hand correctly and these are some of the correct rotation values:

 P=0.000000 Y=-48.702690 R=-8.323224
 P=0.000000 Y=-49.789928 R=-8.552868
 P=-0.000007 Y=-50.883415 R=-8.711617
 P=-0.000007 Y=-52.022709 R=-8.827152
 P=-0.000007 Y=-53.098286 R=-8.840174
 P=0.000000 Y=-54.122597 R=-8.782237
 P=0.000000 Y=-55.130390 R=-8.710796
 P=0.000000 Y=-56.111393 R=-8.561297
 P=0.000007 Y=-57.071911 R=-8.353694
 P=0.000000 Y=-58.066284 R=-8.080949
 P=0.000007 Y=-58.996971 R=-7.765880
 P=0.000000 Y=-60.021267 R=-7.374957
 P=0.000000 Y=-61.099659 R=-6.955542
 P=0.000000 Y=-62.155025 R=-6.534186
 P=-0.000007 Y=-63.222946 R=-6.109061
 P=-0.000007 Y=-64.318413 R=-5.663557
 P=-0.000007 Y=-65.328362 R=-5.252420
 P=-0.000007 Y=-66.379761 R=-4.831179
 P=-0.000007 Y=-67.402679 R=-4.408598
 P=0.000000 Y=-68.367493 R=-4.024447

In my own project, I print out the same rotation values the same way its done in the example but the results look like this and produce an incorrect rotation:

 P=7.775964 Y=-7.446252 R=-0.000001
 P=7.824084 Y=-7.487617 R=-0.000001
 P=7.818578 Y=-7.518079 R=-0.000001
 P=7.850033 Y=-7.517104 R=-0.000001
 P=7.858570 Y=-7.491702 R=-0.000001
 P=7.864113 Y=-7.506747 R=-0.000001
 P=7.866255 Y=-7.553430 R=-0.000001
 P=7.897817 Y=-7.592583 R=-0.000001
 P=7.916775 Y=-7.657037 R=-0.000001
 P=7.922338 Y=-7.749585 R=-0.000001
 P=7.989968 Y=-7.808784 R=-0.000001
 P=8.000801 Y=-7.914917 R=-0.000001
 P=7.993641 Y=-7.998132 R=-0.000001
 P=7.993693 Y=-8.040081 R=-0.000001
 P=7.861572 Y=-8.544514 R=-0.000001
 P=7.832154 Y=-8.645342 R=-0.000001
 P=7.791457 Y=-8.710664 R=-0.000001
 P=7.802832 Y=-8.731378 R=-0.000001
 P=7.763756 Y=-8.795066 R=-0.000002
 P=7.734653 Y=-8.790546 R=-0.000001
 P=7.715140 Y=-8.758606 R=-0.000001
 P=7.686746 Y=-8.767592 R=-0.000001
 P=7.592921 Y=-8.746538 R=-0.000001
 P=7.560185 Y=-8.724867 R=-0.000001
 P=7.504443 Y=-8.743661 R=-0.000001
 P=7.429923 Y=-8.720572 R=-0.000001
 P=7.396094 Y=-8.722472 R=-0.000001
 P=7.371034 Y=-8.729496 R=-0.000001
 P=7.331400 Y=-8.721493 R=-0.000001
 P=7.353291 Y=-8.695507 R=-0.000001
 P=7.358566 Y=-8.671263 R=-0.000001
 P=7.356328 Y=-8.624143 R=-0.000001
 P=7.369827 Y=-8.545854 R=-0.000001
 P=7.372361 Y=-8.504041 R=-0.000001

As you can see, it looks like something is slightly off with the math. I first tried to simply switch the pitch and the roll but that didn't really help too much. I think the answer might be in using UKismetMathLibrary::ComposeRotators() to combine the rotation by 90 somewhere or rotate the rotator?

Edit: Here is the code used to calculate the rotation for the gun when the second hand grabs it (second hand is not attached but the first one is attached).

 void ATwoHandGunWeapon::Tick(float deltaTime)
     FVector handleLocalPos;
     FVector gripLocalPos;
     FRotator gunLookAtRotation;
     //IF the grip is being touched aswell, account for the rotation.
     if (mySecondaryHand != NULL && myGripComponent != NULL && myFirePointComponent != NULL && myRootComponent != NULL)
         handleLocalPos = UKismetMathLibrary::InverseTransformLocation(this->GetActorTransform(), myPrimaryHand->GetControllerLocation());
         gripLocalPos = UKismetMathLibrary::InverseTransformLocation(this->GetActorTransform(), mySecondaryHand->GetControllerLocation());
         gunLookAtRotation = UKismetMathLibrary::FindLookAtRotation(handleLocalPos, gripLocalPos);
         //Combines two rotators together.
         //gunLookAtRotation = UKismetMathLibrary::ComposeRotators(FRotator(0.0f, 0.0f, -90.0f), gunLookAtRotation);
         UE_LOG(LogTemp, Error, TEXT("after compose gunLookAtRotation:%s"), *(gunLookAtRotation.ToString()));

Product Version: UE 4.18
more ▼

asked Feb 28 '18 at 09:00 PM in C++ Programming

avatar image

151 27 21 30

avatar image Jin_VE Feb 28 '18 at 09:40 PM

Seems like they might be local rotations so it could be hard to tell from just the numbers. Is this gun being attached to a socket?

avatar image Katianie Feb 28 '18 at 09:45 PM

I'm attaching the gun to my right hand, its when I grab with my left hand that I do the rotation. I dont attach the gun to the left hand. These are relative rotations, I set the relative rotation of the item.

avatar image HollingsworthDan Mar 01 '18 at 05:34 AM

Stop using kismet. Those are base functions for blueprint nodes. What you should be writing is the code you see inside of those functions.

avatar image Katianie Mar 01 '18 at 08:40 PM

Calling the function directly vs doing whats in the function; I don't see how that will change anything though?

avatar image HollingsworthDan Mar 02 '18 at 02:46 AM

You would type a lot less. And you would be relying on the base math classes which means your code runs faster. No it doesn't fix the problem your asking about. Which is why I made it a comment. But you really should learn how to use FMath, FVector, FRotator....

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

Ok, I figured out the issue and it will take a little bit of explaining.

The two handed gun I am rotating is a series of static meshes that are parented together like so:

alt text

When I originally posted this, I was grabbing the handle with the primary hand which attached and worked correctly. But when I grabbed with the secondary hand to rotate the gun, it rotated the wrong way (my original problem with it off by 90 somewhere). This is because you are not supposed to rotate around the handle but actually rotate around the GunRoot.

The correct way to do this is to attach your hand to the handle but then Rotate the GunRoot Component and not the handle. If you try to do this out of the box the gun wont rotate and this is because the GunRoot is a USceneComponent and needs to be attached to something grabbable and moveable (UPrimitiveComponent). Below is how the GunRoot is attached to the handle. Once I had this in place, I was able to pick up the gun and rotate it correctly.

 void ATwoHandGunWeapon::OnPlayerReceive(ABaseHand* owningHand)
     UPrimitiveComponent* handlePrimitiveComponent = NULL;
     //Attachment is diffrent for the two handed weapon since rotation is based on the root.
     //Unfortunattly the root is a scene component so it needs to be attached to a moveable part.
     if (owningHand != NULL)
         handlePrimitiveComponent = Cast<UPrimitiveComponent>(myHandleComponent);
         if (handlePrimitiveComponent != NULL)
             handlePrimitiveComponent->AttachToComponent(myGunRootComponent, FAttachmentTransformRules(EAttachmentRule::SnapToTarget, EAttachmentRule::SnapToTarget, EAttachmentRule::KeepWorld, true));
         this->AttachToComponent(owningHand->GetVRHandMesh(), FAttachmentTransformRules(EAttachmentRule::SnapToTarget, EAttachmentRule::SnapToTarget, EAttachmentRule::KeepWorld, true));

The above code was the key to get it working because it attaches the GunRoot to the handle. I thought this was already done automatically because of the hierarchy in the first picture but apparently not, the results don't lie.

capture.png (23.5 kB)
more ▼

answered Mar 06 '18 at 04:53 PM

avatar image

151 27 21 30

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

Check the skeletal mesh of the character in both projects and make sure the right hand sockets have the same rotation.

more ▼

answered Feb 28 '18 at 09:52 PM

avatar image

4.4k 32 79 42

avatar image Katianie Feb 28 '18 at 10:33 PM

In this case, The hands are USkeletalMeshComponents so I'm not using the character mesh. I checked the skeletal mesh component rotation in my project and it is 0. In the example project that is working, two static mesh spheres are being used to represent the hands. I checked the rotation of the static mesh component of the hand and it was 0.

avatar image Jin_VE Feb 28 '18 at 10:55 PM

Well when we use a full character mesh we rotate it 90 degrees on the Z axis so you could try using Delta(Rotator) to subtract 90 degrees or Compose to add in negative 90. Or maybe you need to add it in. It's really hard to tell with what you've provided. You could post some code and screenshots and maybe I'd have a better idea.

avatar image Katianie Mar 01 '18 at 12:04 AM

Interesting, I tried doing almost exactly that. I'll post some code that calculates the the rotation I'm talking about.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question