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"

Getting actor rotation around a specific axis?

I have an actor that is moving in the world while spinning on it's local Z axis(while starting off tilted on it's X and Y axis), which cause it's X and Y values to fluctuate wildly negative and positive. I need to get its X rotation along its direction of travel. I have the actors Physics velocity using:


I need to get the X angle(Preferably Euler or radians) relative to that being the forward vector, but I feel like I'm banging my head against the wall. I have tried using RotateVector and UnrotateVector with the objects rotation, but that doesn't seem to be getting me anywhere. I'm not sure I'm even looking at this the right way. Anyone point me in the right direction?


Product Version: UE 4.19
more ▼

asked Jul 10 '18 at 05:01 AM in C++ Programming

avatar image

3 1 2 4

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

2 answers: sort voted first

To be frank, I don't really understand the question but...

There is a simple way to get the angle between any 2 vectors:

Angle = Acos(DotProduct(V1,V2)/(V1.Size()*V2.Size()))

If both V1 and V2 are normalized ( V1.Size() = V2.Size() = 1.0f ) the angle simplifies to:

Angle = Acos(DotProduct(V1,V2))

Don't be afraid of angles fluctuating. Angle values always flip their sign at the end of the circle the same way you go from 0 to 180 degrees then flip to -180 and back to 0.

Be careful of what Acos() returns (degrees or radians).

more ▼

answered Jul 10 '18 at 09:48 AM

avatar image

1.3k 6 5 10

avatar image LaughinGas Jul 11 '18 at 02:31 AM

Wait that doesnt work upon further testing. When I look straight along the x axis it works but when i look down the y axis it does not, it is finging the world x axis difference.

avatar image LaughinGas Jul 11 '18 at 02:38 AM

Thanks for the reply! It seemed to deleted have my first response, so this has no context. The Acos got me started in the right direction, what I have is:

 FVector v1 = StaticMeshComp->GetPhysicsLinearVelocity().ToOrientationQuat().GetUpVector();
 FVector v2= myActorQuat.GetUpVector();
 v2.X = v1.X;
 float Angle = FMath::Acos(FVector::DotProduct(v1, v2));

I am equalizing the x values to cancel out the pitch, I initially thought I would need to equalize the Y values but apparently not.

So that seems to get me the difference in roll on the world scale, but I need the local difference in roll, with the X axis pointed down the velocity vector. My first thought is using the Velocity Quat to rotate the actors rotation vector, but that doesn't seem to be working.

avatar image dZh0 Jul 11 '18 at 06:28 AM

The angle between 2 vectors does not depend on the reference frame and does not depend on the scale or length of the vectors so you are making a mistake when you modify the X components. The fact that it returns a angle close to world YZ plane angle is pure coincidence. To get the local angle (the angle in myActor) you must get the velocity vector in myActor's coordinate system.

Try this: (I have not tested it but it should work after you fix all punctuation errors)

 FVector v1 = StaticMeshComp->GetPhysicsLinearVelocity(); //Get the velocity
 FTransform LocalTransform = myActor.GetTransform(); //Get the actor transform
 v1 = LocalTransform.TransformVector(v1); //v1 is now in local coordinates
 v1.X = 0;    //Project it in YZ plane
 FQuat VelocityQuat = FQuat::FindBetweenVectors(v1, FVector::RightVector); //Find the quaternion between the velocity(in local coordinates) and the RightVector = (0.0, 1.0, 0.0)
 //From this point on you can get the Angle if you need it with
 float Angle = VelocityQuat.GetAngle;

Be careful what transformations you do to the vectors as this will define what angle you get.

avatar image LaughinGas Jul 11 '18 at 01:45 PM

Thank for the response! That was what I needed, with a little modifying. Here is what I used:

 FVector v1 = StaticMeshComp->GetPhysicsLinearVelocity().ToOrientationQuat().GetUpVector();
 FVector v2 = ActorQuat.GetUpVector();
 v2.X = v1.X;
 float Angle = FMath::Acos(FVector::DotProduct(v1, v2));

I want to ignore one of the angles, the pitch, I want to find the roll angle. So I'm a little confused why this works, ass it looks like i'm setting the roll to be equal to each other. I initially tried

 v2.Y = v1.Y;

But that seemed to be equalizing the roll and what I have above works, verified with debug lines.

So that gives me size of the angle, now I need to find whether it is clockwise/counterclockwise from center, on an object that can travel in any direction. Before I start throwing ideas at visual studio, is there any easy way to get that info?

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

Hi. Maybe you can use functionality of RotationComponent? Look at PivotTranslation. Here is similar question Rotating component around custom point. Does this fit?

more ▼

answered Jul 10 '18 at 08:17 AM

avatar image

459 15 13 23

(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