I need to make an AI car. For this, the car needs to have sensors. Those sensors are used to avoid obstacles.
I can’t figure out how to get the top left corner position of the vehicle when it is rotated. The extent, rotation and location of the vehicle is known.
Here is an image of what I mean with sensors:
car with sensors
Here is a video of my current progress (the red line starts at the top left corner, but the start position gets wrong after the car rotates): video
This is my code so far:
FHitResult OutHit; FHitResult OutHit2;
FVector Start = GetVehicleMovementComponent()->GetActorLocation();
Start += FVector(GetMesh()->GetCachedLocalBounds().GetBox().GetExtent() * GetMesh()->GetComponentRotation().Vector());
FVector End = (GetMesh()->GetComponentRotation().Vector() * 1000.f) + Start;
FVector test = GetVehicleMovementComponent()->GetActorLocation();
test.X += GetMesh()->GetCachedLocalBounds().GetBox().GetExtent().X * GetActorForwardVector().X;
test.Y -= GetMesh()->GetCachedLocalBounds().GetBox().GetExtent().Y * GetActorForwardVector().Y;
test.Z -= GetMesh()->GetCachedLocalBounds().GetBox().GetExtent().Z * GetActorForwardVector().Z;
/* float x = test.X - GetMesh()->GetCachedLocalBounds().GetBox().GetExtent().X;
float y = test.Y - GetMesh()->GetCachedLocalBounds().GetBox().GetExtent().Y;
float theta = GetMesh()->GetRelativeRotationCache().GetCachedRotator().Yaw;
float rotatedX = test.X * cos(theta) - test.Y * sin(theta);
float rotatedY = test.X * sin(theta) + test.Y * cos(theta);
test.X = rotatedX + x;
test.Y = rotatedY + y; */
FVector test2 = (GetMesh()->GetComponentRotation().Vector() * 1000.f) + test;
DrawDebugLine(GetWorld(), Start, End, FColor::Green, false, -1.f, 4, 4.f);
DrawDebugLine(GetWorld(), test, test2, FColor::Red, false, -1.f, 4, 4.f);
FCollisionQueryParams CollisionParams;
bool isHit = GetWorld()->LineTraceSingleByChannel(OutHit, Start, End, ECC_Visibility, CollisionParams);
bool isHit2 = GetWorld()->LineTraceSingleByChannel(OutHit2, FVector(test), test2, ECC_Visibility, CollisionParams);
GEngine->AddOnScreenDebugMessage(1, 1, FColor::Green, FString::Printf(TEXT("Rotation Yaw %f"), GetMesh()->GetRelativeRotationCache().GetCachedRotator().Yaw));
GEngine->AddOnScreenDebugMessage(2, 1, FColor::Red, FString::Printf(TEXT("Rotation Pitch %f"), GetMesh()->GetRelativeRotationCache().GetCachedRotator().Pitch));
GEngine->AddOnScreenDebugMessage(3, 1, FColor::Yellow, FString::Printf(TEXT("Rotation Roll %f"), GetMesh()->GetRelativeRotationCache().GetCachedRotator().Roll));
GEngine->AddOnScreenDebugMessage(4, 1, FColor::Green, FString::Printf(TEXT("Forward X %f"), GetActorForwardVector().X));
GEngine->AddOnScreenDebugMessage(5, 1, FColor::Red, FString::Printf(TEXT("Forward Y %f"), GetActorForwardVector().Y));
GEngine->AddOnScreenDebugMessage(6, 1, FColor::Yellow, FString::Printf(TEXT("Forward Z %f"), GetActorForwardVector().Z));
GEngine->AddOnScreenDebugMessage(7, 1, FColor::Green, FString::Printf(TEXT("Pos X %f"), test.X));
GEngine->AddOnScreenDebugMessage(8, 1, FColor::Red, FString::Printf(TEXT("Pos Y %f"), test.Y));
GEngine->AddOnScreenDebugMessage(9, 1, FColor::Yellow, FString::Printf(TEXT("Pos Z %f"), test.Z));
if (isHit)
{
if (OutHit.bBlockingHit && OutHit.Actor != nullptr)
{
GEngine->AddOnScreenDebugMessage(1, 1, FColor::Red, FString::Printf(TEXT("Intersection with %s"), *OutHit.GetActor()->GetName()));
}
}