• News

• Industries

• Learning & Support

• Community

• Marketplace

# How do I use RotateVector in C++?

Can somebody tell me, how to use the RotateVector function properly?

I want to return a vector, which is a vector rotated by a rotator. Is this not the correct syntax below?

``````

// Create the Launch Impulse Vector.
FVector LaunchImpulse = FVector(ScreenEdgeImpulse, ScreenEdgeImpulse, 0.0f);

// Get the Rotator that we want to rotate the Impulse by for each mesh.
FRotator RotateLightWisp = UKismetMathLibrary::FindLookAtRotation(DarkWispMesh->GetComponentLocation, LightWispMesh->GetComponentLocation);
FRotator RotateDarkWisp = UKismetMathLibrary::FindLookAtRotation(LightWispMesh->GetComponentLocation, DarkWispMesh->GetComponentLocation);

FVector LightWispLaunchImpulse = UKismetMathLibrary::RotateVector(LaunchImpulse, RotateLightWisp);

``````

Need help pretty urgently, as I’m in the middle of a game jam

Dear Jamish,

Do you always know what axis you want to rotate around?

If you always want to rotate around z axis for example you can use this

``````
/**
* Rotates around Axis (assumes Axis.Size() == 1).
*
* @param Angle Angle to rotate (in degrees).
* @param Axis Axis to rotate around.
*
* @return Rotated Vector.
*/
FVector RotateAngleAxis( const float AngleDeg, const FVector& Axis ) const;

``````

example

``````

FVector MyVector = something

//rotate 30 degrees around z axis
FVector ZAxisRotatedVector = MyVector.RotateAngleAxis(30,FVector(0,0,1));

``````

If you dont know that much information, and just have a Rotator,
you can turn that rotator into a Rotation Matrix and the rotate the vector.

``````
**Rotation Matrix**

If you are trying to rotate a Direction Vector (ie, normalized difference between two locations)

```

/**
*	Transform a direction vector - will not take into account translation part of the FMatrix.
*	If you want to transform a surface normal (or plane) and correctly account for non-uniform scaling you should use TransformByUsingAdjointT.
*/
FORCEINLINE FVector4 TransformVector(const FVector& V) const;

```

```

FRotator MyRotator = //the rotator to rotate the Vector by

//Rotation Matrix
**FRotationMatrix MyRotationMatrix(MyRotator);**

FVector RotatedDirVector = MyRotationMatrix.**TransformVector**(YourVector);

```

``````

Normalized Impulse

If the RotationMatrix doesnt seem to work then give it the normalized version of your impulse, and get the length of the impulse so you can reapply its length/magnitude after getting the rotated impulse direction.

``````
FVector RotatedImpulse = MyRotationMatrix.TransformVector((Impulse.SafeNormal());

RotatedImpulse *= Impulse.Size();

``````

Thanks Rama I ended up doing this:

``````

// Create the Launch Impulse Vector.
FVector LaunchImpulse = FVector(ScreenEdgeImpulse, ScreenEdgeImpulse, 0.0f);

// Get the Rotator that we want to rotate the Impulse by for each mesh.
FRotator RotateLightWisp = FRotationMatrix::MakeFromX(DarkWispLoc - LightWispLoc).Rotator();
FRotator RotateDarkWisp = FRotationMatrix::MakeFromX(-DarkWispLoc + LightWispLoc).Rotator();

//FRotator RotateLightWisp = UKismetMathLibrary::FindLookAtRotation(DarkWispLoc, LightWispLoc);
//FRotator RotateDarkWisp = UKismetMathLibrary::FindLookAtRotation(LightWispLoc, DarkWispLoc);

// Rotate the launch vector by the rotation
FVector LightWispLaunchImpulse = FRotator(RotateLightWisp).RotateVector(LaunchImpulse);
FVector DarkWispLaunchImpulse = FRotator(RotateDarkWisp).RotateVector(LaunchImpulse);

``````

My next question is… why can I not get input from the keyboard for whatever reason?

``````

void AWispPawn::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);

//FVector RepulsionImpulse = (WispRepulsionImpulse, WispRepulsionImpulse, 0.0f);

FVector DarkWispLoc = (DarkWispMesh->GetComponentLocation());
FVector LightWispLoc = (LightWispMesh->GetComponentLocation());

// Check to see if we are too far apart already.
if (DistanceBetweenWisps() > 3200.0f)
{
// Create the Launch Impulse Vector.
FVector LaunchImpulse = FVector(ScreenEdgeImpulse, ScreenEdgeImpulse, 0.0f);

// Get the Rotator that we want to rotate the Impulse by for each mesh.
FRotator RotateLightWisp = FRotationMatrix::MakeFromX(DarkWispLoc - LightWispLoc).Rotator();
FRotator RotateDarkWisp = FRotationMatrix::MakeFromX(-DarkWispLoc + LightWispLoc).Rotator();

//FRotator RotateLightWisp = UKismetMathLibrary::FindLookAtRotation(DarkWispLoc, LightWispLoc);
//FRotator RotateDarkWisp = UKismetMathLibrary::FindLookAtRotation(LightWispLoc, DarkWispLoc);

// Rotate the launch vector by the rotation
FVector LightWispLaunchImpulse = FRotator(RotateLightWisp).RotateVector(LaunchImpulse);
FVector DarkWispLaunchImpulse = FRotator(RotateDarkWisp).RotateVector(LaunchImpulse);

}
else
{
// Set World Location Of Light Wisp Depending on Inputs.
FVector LightLocalMove = FVector();
LightLocalMove.X = (CurrentLightForwardSpeed * MovementSpeed) * DeltaSeconds;
LightLocalMove.Y = (CurrentLightRightSpeed * MovementSpeed) * DeltaSeconds;
LightWispMesh->SetWorldLocation(LightLocalMove, true);

FVector DarkLocalMove = FVector();
DarkLocalMove.X = (CurrentDarkForwardSpeed * MovementSpeed) * DeltaSeconds;
DarkLocalMove.Y = (CurrentDarkRightSpeed * MovementSpeed) * DeltaSeconds;
DarkWispMesh->SetWorldLocation(DarkLocalMove, true);
}

// Move Camera To Correlate to new World Positions.
float NewCameraHeight = FMath::Clamp(DistanceBetweenWisps(), CameraMinHeight, CameraMaxHeight);
FVector AddCamLoc = FVector(0.0f, 0.0f, NewCameraHeight);

FVector NewCamLocation = MidpointBetweenWisps() + AddCamLoc;

TopDownCamera->SetWorldLocation(FVector(NewCamLocation), true);
}

void AWispPawn::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
check(InputComponent);

// Fine Up and Right Axes for Light Wisp.
InputComponent->BindAxis("LightMoveForward", this, &AWispPawn::LightWispForward);
InputComponent->BindAxis("LightMoveRight", this, &AWispPawn::LightWispRight);

// Find input for dark wisp.
InputComponent->BindAxis("DarkMoveForward", this, &AWispPawn::DarkWispForward);
InputComponent->BindAxis("DarkMoveRight", this, &AWispPawn::DarkWispRight);
}

void AWispPawn::LightWispForward(float val)
{

CurrentLightForwardSpeed = val;
}

void AWispPawn::LightWispRight(float val)
{
CurrentLightRightSpeed = val;
}

void AWispPawn::DarkWispForward(float val)
{
CurrentDarkForwardSpeed = val;
}

void AWispPawn::DarkWispRight(float val)
{
CurrentDarkRightSpeed = val;
}

``````

I’ve setup my inputs in the config file properly, but for whatever reason this won’t work

oh oops hee hee, yea using FRotator directly! Great idea!

hee hee

Are the functions you are calling marked with UFUNCTION() ?

AWispPawn::LightWispForward

is this above UFUNCTION()

I believe Marc Audy said this is not necessary any more but its worth checking to mark it off the list

Rama

Hi Rama

How do you use the RotateAngleAxis function if the axis is at a distance (radius) from the location vector that you’re moving?

Thanks!

Normalize the Axis

``````

FVector MyAxis = MyAxis.GetSafeNormal();

``````

Also, you really should create a new thread instead of bumping a 2-year old one

Thanks TheJamsh! Didnt realize that was bad etiquette, wont do it again!

To rotate Vector by Rotator use: FRotator::RotateVector()

This thread is four years old dammit…

Why not just auto lock them after 3 months of inactivity?

Maybe you can try:
Rorator YourRot;
FVector YourVec;
FVector Result = yourRot.RotateVector(YourVect);

Regardless of the age of thread, it tends to show up when you search Google.