@@ -25,36 +25,50 @@ const float CMEASURE_DETONATE_DISTANCE = 40.0f;
2525
2626// Used to set a countermeasure velocity after being launched from a ship as a countermeasure
2727// ie not as a primary or secondary.
28- void cmeasure_set_ship_launch_vel (object * objp, object * parent_objp, int arand)
28+ void cmeasure_set_ship_launch_vel (object* objp, object* parent_objp, int arand)
2929{
30- vec3d vel, rand_vec;
30+ vec3d vel;
31+ vec3d rand_vec;
32+ vec3d world_dir;
3133
32- // Get cmeasure rear velocity in world
33- vm_vec_scale_add (&vel, &parent_objp->phys_info .vel , &parent_objp->orient .vec .fvec , -25 .0f );
34+ weapon* wp = &Weapons[objp->instance ];
35+ auto * wip = &Weapon_info[wp->weapon_info_index ];
36+
37+ // Start with parent velocity
38+ vel = parent_objp->phys_info .vel ;
39+
40+ // Normalize direction
41+ vec3d local_dir = wip->cm_launch_vec ;
42+ if (vm_vec_mag_squared (&local_dir) > 0 .0f ) {
43+ vm_vec_normalize (&local_dir);
44+ }
3445
35- // Get random velocity vector
36- static_randvec (arand+ 1 , &rand_vec );
46+ // Convert from ship to world space
47+ vm_vec_unrotate (&world_dir , &local_dir, &parent_objp-> orient );
3748
38- // Add it to the rear velocity
39- vm_vec_scale_add2 (&vel, &rand_vec, 2 .0f );
49+ // Apply launch impulse
50+ vm_vec_scale_add2 (&vel, &world_dir, wip->cm_launch_speed );
51+
52+ // Add random variance
53+ static_randvec (arand + 1 , &rand_vec);
54+ vm_vec_scale_add2 (&vel, &rand_vec, wip->cm_launch_variance );
4055
4156 objp->phys_info .vel = vel;
4257
4358 // Zero out this stuff so it isn't moving
4459 vm_vec_zero (&objp->phys_info .rotvel );
4560 vm_vec_zero (&objp->phys_info .max_vel );
4661 vm_vec_zero (&objp->phys_info .max_rotvel );
47-
62+
4863 // blow out his reverse thrusters. Or drag, same thing.
4964 objp->phys_info .rotdamp = 10000 .0f ;
5065 objp->phys_info .side_slip_time_const = 10000 .0f ;
5166
52- objp-> phys_info . max_vel . xyz . z = - 25 . 0f ;
53- vm_vec_copy_scale (&objp->phys_info .desired_vel , &objp-> orient . vec . fvec , objp-> phys_info . max_vel . xyz . z );
67+ // Desired velocity should align with launch direction, not hardcoded backward
68+ vm_vec_copy_scale (&objp->phys_info .desired_vel , &world_dir, wip-> cm_launch_speed );
5469
5570 // if this cmeasure has a single segment trail, let the trail know since we just changed the velocity
5671 // yeah this is hacky but that's what this function gets for CHANGING the velocity on objects with a ""CONSTANT VELOCITY""
57- weapon* wp = &Weapons[objp->instance ];
5872 if (wp->trail_ptr && wp->trail_ptr ->single_segment ) {
5973 wp->trail_ptr ->vel [0 ] = objp->phys_info .vel ;
6074 wp->trail_ptr ->vel [1 ] = objp->phys_info .vel ;
0 commit comments