Skip to content

Commit bfacc16

Browse files
authored
Fix for single segment trails (#6864)
* fix single segmen trails when interrupted on both ends * cleaner case handling * remove wip stuff
1 parent f96c73f commit bfacc16

1 file changed

Lines changed: 21 additions & 25 deletions

File tree

code/weapon/trails.cpp

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -160,20 +160,15 @@ void trail_render( trail * trailp )
160160
float speed = vm_vec_mag(&trailp->vel[front]);
161161

162162
float total_len = speed * ti->max_life;
163-
float t = vm_vec_dist(&trailp->pos[front], &trailp->pos[back]) / total_len;
164-
CLAMP(t, 0.0f, 1.0f);
165163
float f_alpha, b_alpha, f_width, b_width;
166-
if (trailp->object_died) {
167-
f_alpha = t * (ti->a_start - ti->a_end) + ti->a_end;
168-
b_alpha = ti->a_end;
169-
f_width = t * (ti->w_start - ti->w_end) + ti->w_end;
170-
b_width = ti->w_end;
171-
} else {
172-
f_alpha = ti->a_start;
173-
b_alpha = t * (ti->a_end - ti->a_start) + ti->a_start;
174-
f_width = ti->w_start;
175-
b_width = t * (ti->w_end - ti->w_start) + ti->w_start;
176-
}
164+
165+
float t_front = trailp->val[front] - trailp->val[back];
166+
float t_back = MAX(0.0f, -trailp->val[back]);
167+
168+
f_alpha = t_front * (ti->a_start - ti->a_end) + ti->a_end;
169+
b_alpha = t_back * (ti->a_start - ti->a_end) + ti->a_end;
170+
f_width = t_front * (ti->w_start - ti->w_end) + ti->w_end;
171+
b_width = t_back * (ti->w_start - ti->w_end) + ti->w_end;
177172

178173
vec3d trail_direction, ftop, fbot, btop, bbot;
179174
vm_vec_normalized_dir(&trail_direction, &trailp->pos[back], &trailp->pos[front]);
@@ -195,8 +190,8 @@ void trail_render( trail * trailp )
195190

196191
verts[0].texture_position.u = trailp->val[front] * uv_scale;
197192
verts[1].texture_position.u = trailp->val[front] * uv_scale;
198-
verts[2].texture_position.u = trailp->val[back] * uv_scale;
199-
verts[3].texture_position.u = trailp->val[back] * uv_scale;
193+
verts[2].texture_position.u = MAX(trailp->val[back], 0.0f) * uv_scale;
194+
verts[3].texture_position.u = MAX(trailp->val[back], 0.0f) * uv_scale;
200195

201196
verts[0].texture_position.v = verts[3].texture_position.v = 0.0f;
202197
verts[1].texture_position.v = verts[2].texture_position.v = 1.0f;
@@ -372,6 +367,9 @@ void trail_add_segment( trail *trailp, vec3d *pos , const matrix* orient, vec3d*
372367

373368
if (trailp->single_segment && velocity) {
374369
trailp->vel[next] = *velocity;
370+
if (next == 0) {
371+
trailp->val[next] = -1.0;
372+
}
375373
} else if (orient != nullptr && trailp->info.spread > 0.0f) {
376374
vm_vec_random_in_circle(&trailp->vel[next], &vmd_zero_vector, orient, trailp->info.spread, false, true);
377375
} else
@@ -404,10 +402,15 @@ void trail_move_all(float frametime)
404402
time_delta = frametime / trailp->info.max_life;
405403

406404
if (trailp->single_segment) {
407-
if (trailp->object_died) {
408-
trailp->pos[0] += trailp->vel[0] * frametime; // only back keeps going...
409-
trailp->val[0] += time_delta;
405+
trailp->val[0] += time_delta;
410406

407+
if (trailp->val[0] > 0.0f) {
408+
// finished 'unfurling'
409+
// back end moves too now
410+
trailp->pos[0] += trailp->vel[0] * frametime;
411+
}
412+
413+
if (trailp->object_died) {
411414
if (trailp->val[0] >= trailp->val[1])
412415
num_alive_segments = 0; // back has caught up to front and were dead
413416
else
@@ -416,13 +419,6 @@ void trail_move_all(float frametime)
416419
trailp->pos[1] += trailp->vel[1] * frametime;
417420
trailp->val[1] += time_delta;
418421

419-
if (trailp->val[1] > 1.0f) {
420-
// finished 'unfurling'
421-
// back end moves too now
422-
trailp->pos[0] += trailp->vel[0] * frametime;
423-
trailp->val[0] += time_delta;
424-
}
425-
426422
num_alive_segments = 2;
427423
}
428424
} else if ( trailp->tail != trailp->head ) {

0 commit comments

Comments
 (0)