Skip to content

Instantly share code, notes, and snippets.

@whoo24
Last active June 4, 2019 16:46
Show Gist options
  • Save whoo24/763064d96f36f7b9e3231324e8ca6f80 to your computer and use it in GitHub Desktop.
Save whoo24/763064d96f36f7b9e3231324e8ca6f80 to your computer and use it in GitHub Desktop.
Circular sector for UE4
FCircularSector::FCircularSector() {}
FCircularSector::FCircularSector(const FVector& Center, float YawAngle, float ConeHalfAngle, float Near, float Far)
: m_Center(Center), m_Yaw(YawAngle), m_HalfAngle(ConeHalfAngle), m_Near(Near), m_Far(Far)
{
}
bool FCircularSector::Contains(const FVector& Location) const
{
return CheckToContain(m_Near, m_Far, Location - m_Center, m_HalfAngle, m_Yaw);
}
bool FCircularSector::CheckToContain(float InnerRadius, float OuterRadius, const FVector& VecToDest, float ComapreAbsDelta, float SourceHeading) const
{
float PolarRadius = VecToDest.SizeSquared2D();
if (InnerRadius > 0 && PolarRadius < FMath::Square(InnerRadius))
{
return false;
}
if (PolarRadius > FMath::Square(OuterRadius))
{
return false;
}
float HeadingToDest = FMath::RadiansToDegrees(VecToDest.HeadingAngle());
float DeltaAngle = FMath::FindDeltaAngleDegrees(SourceHeading, HeadingToDest);
if (FMath::Abs(DeltaAngle) > ComapreAbsDelta)
{
return false;
}
return true;
}
void FCircularSector::DrawDebugWireframe(UWorld* World, FColor LineColor) const
{
TArray<float> Angles;
int32 SegmentPerDegree = 10;
int32 NumOfSegments = FMath::Max(1, static_cast<int32>(m_HalfAngle) * 2 / SegmentPerDegree);
float PortionAngle = m_HalfAngle * 2 / static_cast<float>(NumOfSegments);
for (int I = 0; I < NumOfSegments + 1; ++I)
{
Angles.Add(-m_HalfAngle + PortionAngle * I);
}
TArray<FVector> Vertices;
// add an inner arc
for (int I = 0; I < Angles.Num(); ++I)
{
FVector Point = FRotator(0, Angles[I] + m_Yaw, 0).RotateVector(FVector(1, 0, 0)) * m_Near;
Vertices.Add(Point);
}
// add an outer arc
for (int I = Angles.Num() - 1; I >= 0; --I)
{
FVector Point = FRotator(0, Angles[I] + m_Yaw, 0).RotateVector(FVector(1, 0, 0)) * m_Far;
Vertices.Add(Point);
}
Vertices.Emplace(Vertices[0]); // for a radii
// Draw it
for (int I = 1; I < Vertices.Num(); ++I)
{
DrawDebugLine(World, m_Center + Vertices[I - 1], m_Center + Vertices[I], LineColor, false, 0.0f);
}
}
USTRUCT()
struct FCircularSector
{
GENERATED_BODY()
public:
FCircularSector();
FCircularSector(const FVector& Center, float YawAngle, float ConeHalfAngle, float Near, float Far);
bool Contains(const FVector& Location) const;
void DrawDebugWireframe(UWorld* World, FColor Color) const;
private:
bool CheckToContain(float InnerRadius, float OuterRadius, const FVector& VecToDest, float ComapreAbsDelta, float SourceHeading) const;
private:
FVector m_Center;
float m_Yaw = 0;
float m_HalfAngle = 45.0f;
float m_Near = 0;
float m_Far = 10000.0f;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment