Skip to content

Instantly share code, notes, and snippets.

@navaneeth-dev
Created August 3, 2019 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save navaneeth-dev/52fe10db579b927813b667c66c890772 to your computer and use it in GitHub Desktop.
Save navaneeth-dev/52fe10db579b927813b667c66c890772 to your computer and use it in GitHub Desktop.
[UE3] Multi purpose World to screen
#define UCONST_Pi 3.1415926
#define URotation180 32768
#define URotationToRadians UCONST_Pi / URotation180
#define CONST_DegToUnrRot 182.0444
#define CONST_RadToUnrRot 10430.3783504704527
FVector RotationToVector(FRotator R)
{
FVector Vec;
float fYaw = R.yaw * URotationToRadians;
float fPitch = R.pitch * URotationToRadians;
float CosPitch = cos(fPitch);
Vec.x = cos(fYaw) * CosPitch;
Vec.y = sin(fYaw) * CosPitch;
Vec.z = sin(fPitch);
return Vec;
}
float Size(FVector &v)
{
return sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
}
void Normalize(FVector &v)
{
float size = Size(v);
if (!size)
v.x = v.y = v.z = 1;
else
{
v.x /= size;
v.y /= size;
v.z /= size;
}
}
FVector VectorSubtract(FVector va, FVector vb);
FVector VectorSubtract(FVector va, FVector vb)
{
FVector out;
out.x = va.x - vb.x;
out.y = va.y - vb.y;
out.z = va.z - vb.z;
return out;
}
float inline Dot(const FVector& V1, const FVector& V2)
{
return (V1.x*V2.x + V1.y*V2.y + V1.z*V2.z);
}
FVector2D W2S(FVector Location, FRotator myRot, FVector cameraLoc, float GFOV)
{
FVector2D Return;
FVector AxisX, AxisY, AxisZ, Delta, Transformed;
GetAxes(myRot, AxisX, AxisY, AxisZ);
Delta = VectorSubtract(Location, cameraLoc);
Transformed.x = Dot(Delta, AxisY);
Transformed.y = Dot(Delta, AxisZ);
Transformed.z = Dot(Delta, AxisX);
if (Transformed.z < 1.00f)
Transformed.z = 1.00f;
float CentX = (screen_width / 2.f);
float CentY = (screen_height / 2.f);
Return.x = CentX + Transformed.x * (CentX / tan(GFOV * UCONST_Pi / 360.0f)) / Transformed.z;
Return.y = CentY + -Transformed.y * (CentX / tan(GFOV * UCONST_Pi / 360.0f)) / Transformed.z;
return Return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment