.qc (dot qc) - the group for quake c coders of all denominations. If you make quake one mods and write code in quake c, join our group! I'll try to answer any quake c questions - please post your inquiry in the forums...

Forum Thread
  Posts  
Rail trail in qc (Groups : qc : Forum : hard_code() : Rail trail in qc) Locked
Thread Options
numbersix
numbersix quake-c coder++
Aug 23 2011 Anchor

Promised code to generate a rail gun style trail with the darkplaces engine. This will probably work in any engine, but darkplaces will have the best looking effect.

// variables for this segment

local vector org, destination, v1, v2, vp;
local float i, j, k, m, vl, damage, aj,ak, af, al, cn;
local entity startent, impactent;
local entity e;

// set muzzle light, make punchangle for gun fire, also might want to use ammo up and play sounds here as well

self.effects = self.effects | EF_MUZZLEFLASH;
self.punchangle_x = -4;

// origin - business end of gun pre-calculated for visible weapon effect

org = self.origin + v_up * VWZ_RAILGUN + v_right * VWY_RAILGUN + v_forward * VWX_RAILGUN;

// trace rail beam out

traceline (org, org + v_forward*2048, TRUE, self);

// initialize loop variables

makevectors(self.v_angle);
v1 = org;
i = pointcontents(v1);
vl = vlen(trace_endpos - org);
af = 1;
j =1;
cn = k = 0;
aj = -0.1;
ak = 0.1;

// loop to display particles - stops when entering a solid brush like a wall or blocking bsp model (doors, plats, etc.)

while (i != CONTENT_SOLID)
{
// this is where we cheat a sin() math value - this approximates a positon on a circle by using vectors up & right
// via quadrant defined by j & k and using af as the directional
// vp is vector for particle position around beam center vector v1

if (af < 10) af = af + 0.2;
v2 = (v_up * j + v_right * k);
vp = normalize(v2) * af;

// particle trail inside spiral - standard quake particles, this could be refined to more approximate the q2 railgun

if (random() < 0.2) particle (v1, v_forward * 30, 250, 2);

// beambit makes the spiral, one every so many quake units
// under the darkplaces engine beambit.spr has a tga override tex that makes it a true circle
// the bits also have a velocity so the trail expands slightly before they fade
// player motion also translates (jump, run, strafe, etc.) to the trail, in a cool effect
// chaos_spawn is a parameter controlled spawn function imparting flags, position, vectors, models, sounds, thinks & touch funcs.
// NOTE: you can NOT use the particle function for this - it has to be a sprite to look right!

e = chaos_spawn(MOVETYPE_FLY, SOLID_NOT, 0, 0, v1 + vp, v0, v0, v0, vp + self.velocity, "", "progs/beambit.spr", SUB_Remove, 0.5, SUB_Remove, SUB_Null, self, world);

// count bits and set transparency in darkplaces

if (e)
{
cn = cn + 1;
e.alpha = 0.4;
}

// test code to track beam positioning - commented out for release code
/*
bprint("pos v_u = ");
bprint(vtos(v_up * j));
bprint(" pos v_r = ");
bprint(vtos(v_right * k));
bprint(".\n");
*/

// code to advance spiral along - this sets a new quadrant of the spiral based on position

j = j + aj;
k = k + ak;

if (fabs(j) < 0.02 || fabs(k) < 0.02)
{
if (aj == -0.1 && ak == 0.1) // took k to 1, j to 0
{
ak = -0.1;
}
else if (aj == -0.1 && ak == -0.1) // took k to 0, j to -1
{
aj = 0.1;
}
else if (aj == 0.1 && ak == -0.1) // took k to -1, j to 0
{
ak = 0.1;
}
else if (aj == 0.1 && ak == 0.1) // took k to 0, j to 1
{
aj = -0.1;
}
}

// calculate distance forward (i is metric, v1 stores origin vector) to next particle - based on distance and particle count

i = 2 + (1 / af) * 0.5;
if (vl > 600) i = i + (vl / 600);
if (cn > 200) i = i + (cn / 100);
if (cn > 300) i = i + (cn / 150);
if (cn > 400) i = i + (cn / 100);
// if (af > 8 ) i = 2;
v1 = v1 + v_forward * i;

// find out if we are in a solid yet - this could be re-written to penetrate thin brushes like doors with an extra check!

i = pointcontents(v1);
}

// loop done - damage routine should follow this code

// Arg...forum style is stripping the tab indents. They are there, you just cant see them. dur.

Edited by: numbersix

Reply to thread
click to sign in and post

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.