Greetings.
Im trying to right a function that will return which face of an object is closest to facing up.
In the example the shape has 6 sides (a cube). But there is cases where i will need to do different amounts of uniform sides.
I have the following code.
const int SidesOfDie = 6;
const FVector BaseUp = FVector(1.0f, 0.0f, 0.0f);
float Angle;
int32 LowestInt;
float LowestAngle;
FRotator ActorRotation = this->GetActorRotation();
FVector vectors[SidesOfDie] = {
ActorRotation.RotateVector(FVector(1.0f, 0.0f, 0.0f)),
ActorRotation.RotateVector(FVector(-1.0f, 0.0f, 0.0f)),
ActorRotation.RotateVector(FVector(0.0f, 1.0f, 0.0f)),
ActorRotation.RotateVector(FVector(0.0f, -1.0f, 0.0f)),
ActorRotation.RotateVector(FVector(0.0f, 0.0f, 1.0f)),
ActorRotation.RotateVector(FVector(0.0f, 0.0f, -1.0f))
};
for (size_t i = 0; i < SidesOfDie; i++)
{
Angle = FVector::DotProduct(BaseUp, vectors[i]);
if (i == 0) {
LowestInt = i;
LowestAngle = Angle;
}
else if (Angle < LowestAngle) {
LowestInt = i;
LowestAngle = Angle;
}
}
return LowestInt;
So we start of by defining the base up vector which is 1,0,0
Then I make a vector from the center of the object to the middle of each face.
Then I rotate each vector by the objects current rotation.
Then I calculate the dot product.
Now if i remember my maths right that should return the angle between the vectors. Meaning the smallest one is the vector correlating to the side facing.
But its not working i rotate my cube and get the same value.
Can anyone see what im doing wrong :)?