Rotating a vector

Hi, I need to rotate a vector to do a shotgun instant hits system. I did this to rotate each hit horizontally:


GetWorld()->LineTraceSingle(GunHitResult, CameraWorldPosition, ((CameraForwardVector * 50000) + CameraWorldPosition).RotateAngleAxis(5.f, FVector(0.f, 0.f, 1.f)), ECC_Visibility, TraceParams);

but the problem is that if I change the X or Y axis instead of Z, to rotate a vector of a hit vertically, it goes up if I’m watching a side of the map, and it goes down if I’m watching the other side.

What can I do to rotate the vector of a hit vertically?


So basically we attempted to set up our shotgun bullet spread using vectors with coordinates this way (see attached image).
Our issue is that we can’t seem to get our vectors to rotate along the x axis. Only the Z axis seems to work, therefore our bullet spread system is only half done.

Does someone know why this is happening and/or what could possibly be causing this?

Better start working with angles and rotators. Shotgun spread is more easily represented in terms of angles, so you just create a rotator with the yaw and pitch of whatever spread you want. FRotator::RotateVector will transform a vector, so you can just take your camera forward vector and transform it using each shotgun spread rotator.

Also, you’d probably want to randomize shotgun spread, rather than use predefined coordinates. You can use FMath::VRandCone for that. Let’s say you want your maximum shotgun spread to be 2 degrees:


FVector PelletDir = FMath::VRandCone( CameraForwardVector, FMath::DegreesToRadians( 2.0f ) );
GetWorld()->LineTraceSingle( GunHitResult, CameraWorldPosition, CameraWorldPosition + PelletDir * MaxPelletDistance, ECC_Visibility, TraceParams);

Thank you very much for the heads up.:slight_smile: Since our game is an FPS arena we’re going for the fixed spread method, Quake style.

We’ll try out your solution and report back.

Same problem. If I watch this side of the map, I get this:


else if I watch the other side of the map, I get this:

http://puu.sh/gUiGm/f8ba26c559.jpg

If I turn 90 degrees left or right, the point is in the center of the camera.

I did this:


GetWorld()->LineTraceSingle(GunHitResult, CameraWorldPosition, FRotator(5.f, 0.f, 0.f).RotateVector(((CameraForwardVector * 50000) + CameraWorldPosition)), ECC_Visibility, TraceParams);

Why it isn’t working?


FRotator(5.f, 0.f, 0.f).RotateVector( **((CameraForwardVector * 50000) + CameraWorldPosition)** )

You’re rotating both your origin and your direction, I doubt that helps.


CameraWorldPosition + FRotator(5.f, 0.f, 0.f).RotateVector( **CameraForwardVector** ) * 50000

See if that helps. By the way, this is just stylistic advice but you have much to gain and nothing to lose by separating this kind of stuff over multiple lines of code. Rather than cram everything on a single line, it’s a lot more readable to calculate each vector separately before passing it to your trace. Readability helps avoiding precisely this sort of issue.

And you mention Quake as a reference, I’m pretty certain Quake 3 randomizes its pellet spread. :slight_smile:

I did this:


GetWorld()->LineTraceSingle(GunHitResult, CameraWorldPosition, CameraWorldPosition + FRotator(5.f, 0.f, 0.f).RotateVector(CameraForwardVector * 50000), ECC_Visibility, TraceParams);

but the problem is still there.

The issue seems to be that the vectors are erroneously influenced by the direction you are aiming at. We need to find a way to fix this issue which could be ovverriding the rotation set for the vectors.

Can someone please help us out? This issue is killing our workflow. :frowning:

If I use the local position, it works. Thanks anyway.

This is actually unrelated to your code and your problem, which I can’t help to fix, but this is relevant to the topic, which is to rotate a vector. Future readers who are looking to rotate a vector, this is how to do it:



//Try to get a rotator from anywhere that you want your vector to face towards.
//In this case, I used my UStaticMeshComponent rotation of my AActor.
FRotator Rotation = this->MyMesh->GetComponentRotation();

//This is an arbitrary target vector you want to rotate. It can be any vector you designate.
FVector Vector = FVector(1.0f, 0.0f, 0.0f);

//To rotate, I call on FRotationMatrix class, pass in the rotator, and call on RotateVector()
//to rotate a vector for me. Pass it in like this:
Vector = Rotation.RotateVector(Vector);


2 Likes