Useful code snippets

From Tripwire Interactive Wiki
Jump to: navigation, search

Herein follows a collection of code snippets and functions for Killing Floor, although they should be relevant to other games using the same engine.

Functions

Create a vector from X, Y, Z components

:

static final function vector MakeVector(float x, float y, float z) { local vector V; V.X = x; V.Y = y; V.Z = Z; return V; }

DrawStayingDebugCircle

TWI code.

:

final function DrawStayingDebugCircle(vector Base,vector X,vector Y, byte R, byte G, byte B,float Radius,int NumSides) { local float AngleDelta; local int SideIndex; local vector LastVertex, Vertex;

AngleDelta = 2.0f * PI / NumSides; LastVertex = Base + X * Radius;

for(SideIndex = 0;SideIndex < NumSides;SideIndex++) { Vertex = Base + (X * Cos(AngleDelta * (SideIndex + 1)) + Y * Sin(AngleDelta * (SideIndex + 1))) * Radius; DrawStayingDebugLine(LastVertex,Vertex,R,G,B); LastVertex = Vertex; } }

DrawStayingDebugSphere

TWI code. Requires DrawStayingDebugCircle.

:

final function DrawStayingDebugSphere(vector Base, float Radius, int NumDivisions, byte R, byte G, byte B) { local float AngleDelta; local int SideIndex; local float SegmentDist; local float TempZ; local vector UsedBase;

AngleDelta = 2.0f * PI / NumDivisions; SegmentDist = 2.0 * Radius / NumDivisions;

for( SideIndex = -NumDivisions/2; SideIndex < NumDivisions/2; SideIndex++) { TempZ = SideIndex*SegmentDist; UsedBase = Base; UsedBase.Z -= TempZ; DrawStayingDebugCircle(UsedBase, vect(1,0,0), vect(0,1,0), R,G,B, Sqrt(Radius*Radius - SideIndex*SegmentDist*SideIndex*SegmentDist), NumDivisions); }

for(SideIndex = 0; SideIndex < NumDivisions; SideIndex++) DrawStayingDebugCircle(Base, vect(0,0,1), vect(1,0,0) * Cos(AngleDelta * SideIndex) + vect(0,1,0) * Sin(AngleDelta * SideIndex), R,G,B, Radius, NumDivisions); }

DrawStayingDebugCylinder

TWI code.

:

function DrawStayingDebugCylinder(vector Base,vector X, vector Y,vector Z, FLOAT Radius,float HalfHeight,int NumSides, byte R, byte G, byte B) { local float AngleDelta; local vector LastVertex, Vertex; local int SideIndex;

AngleDelta = 2.0f * PI / NumSides; LastVertex = Base + X * Radius;

for(SideIndex = 0;SideIndex < NumSides;SideIndex++) { Vertex = Base + (X * Cos(AngleDelta * (SideIndex + 1)) + Y * Sin(AngleDelta * (SideIndex + 1))) * Radius;

DrawStayingDebugLine(LastVertex - Z * HalfHeight,Vertex - Z * HalfHeight,R,G,B); DrawStayingDebugLine(LastVertex + Z * HalfHeight,Vertex + Z * HalfHeight,R,G,B); DrawStayingDebugLine(LastVertex - Z * HalfHeight,LastVertex + Z * HalfHeight,R,G,B);

LastVertex = Vertex; } }

Convert HSL to RBG

Taken from here.

Converts HSL to RBG. It's an easy way to get a wide array of colours without complex calculations.

:

static final function HSL2RGB(float h, float sl, float l, out int ROut, out int GOut, out int BOut) { local float v; local float r,g,b; local float m; local float sv; local int sextant; local float fract, vsf, mid1, mid2;

r = l; g = l; b = l;

if (l <= 0.5) v = (l * (1.0 + sl)); else v = (l + sl - l * sl);

if (v > 0) { m = l + l - v; sv = (v - m ) / v; h *= 6.0; sextant = h; fract = h - sextant; vsf = v * sv * fract; mid1 = m + vsf; mid2 = v - vsf;

switch (sextant) { case 0: r = v; g = mid1; b = m; break; case 1: r = mid2; g = v; b = m; break; case 2: r = m; g = v; b = mid1; break; case 3: r = m; g = mid2; b = v; break; case 4: r = mid1; g = m; b = v; break; case 5: r = v; g = m; b = mid2; break; } }

ROut = r * 255.0; GOut = g * 255.0; BOut = b * 255.0; }