Skip to content

Commit 049fa72

Browse files
authored
change bank to target vel from target position (#4417)
1 parent 1b56336 commit 049fa72

File tree

2 files changed

+34
-30
lines changed

2 files changed

+34
-30
lines changed

code/ai/aicode.cpp

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,7 +1195,7 @@ void ai_turn_towards_vector(vec3d* dest, object* objp, vec3d* slide_vec, vec3d*
11951195
vec3d vel_in, vel_out, desired_fvec, src;
11961196
float delta_time;
11971197
physics_info *pip;
1198-
float delta_bank;
1198+
float bank;
11991199

12001200
Assertion(objp->type == OBJ_SHIP || objp->type == OBJ_WEAPON, "ai_turn_towards_vector called on a non-ship non-weapon object!");
12011201

@@ -1289,17 +1289,18 @@ void ai_turn_towards_vector(vec3d* dest, object* objp, vec3d* slide_vec, vec3d*
12891289
// Should be more general case here. Currently, anything that is not a weapon will bank when it turns.
12901290
// Goober5000 - don't bank if sexp or ship says not to
12911291
if ( (objp->type == OBJ_WEAPON) || (flags & AITTV_IGNORE_BANK ) )
1292-
delta_bank = 0.0f;
1292+
bank = 0.0f;
12931293
else if (objp->type == OBJ_SHIP && Ship_info[Ships[objp->instance].ship_info_index].flags[Ship::Info_Flags::Dont_bank_when_turning])
1294-
delta_bank = 0.0f;
1294+
bank = 0.0f;
12951295
else if ((bank_override) && (iff_x_attacks_y(Ships[objp->instance].team, Player_ship->team))) { // Theoretically, this will only happen for Shivans.
1296-
delta_bank = bank_override;
1296+
bank = bank_override;
12971297
} else {
1298-
delta_bank = vm_vec_dot(&curr_orient.vec.rvec, &objp->last_orient.vec.rvec);
1299-
delta_bank = 200.0f * (1.0f - delta_bank) * pip->delta_bank_const;
1298+
bank = vm_vec_dot(&curr_orient.vec.rvec, &objp->last_orient.vec.rvec);
1299+
bank = 200.0f * (1.0f - bank) * pip->delta_bank_const;
13001300
if (vm_vec_dot(&objp->last_orient.vec.fvec, &objp->orient.vec.rvec) < 0.0f)
1301-
delta_bank = -delta_bank;
1301+
bank = -bank;
13021302
}
1303+
bank *= vel_limit.xyz.z;
13031304

13041305

13051306
matrix out_orient;
@@ -1311,7 +1312,7 @@ void ai_turn_towards_vector(vec3d* dest, object* objp, vec3d* slide_vec, vec3d*
13111312
vm_angular_move_matrix(&goal_orient, &curr_orient, &vel_in, delta_time,
13121313
&out_orient, &vel_out, &vel_limit, &acc_limit, The_mission.ai_profile->flags[AI::Profile_Flags::No_turning_directional_bias]);
13131314
} else {
1314-
vm_angular_move_forward_vec(&desired_fvec, &curr_orient, &vel_in, delta_time, delta_bank,
1315+
vm_angular_move_forward_vec(&desired_fvec, &curr_orient, &vel_in, delta_time, bank,
13151316
&out_orient, &vel_out, &vel_limit, &acc_limit, The_mission.ai_profile->flags[AI::Profile_Flags::No_turning_directional_bias]);
13161317
}
13171318

@@ -4871,12 +4872,12 @@ void avoid_ship()
48714872
// If in front of enemy, turn away from it.
48724873
// If behind enemy, try to get fully behind it.
48734874
if (away_dot < 0.0f) {
4874-
turn_away_from_point(Pl_objp, &enemy_pos, Pl_objp->phys_info.speed);
4875+
turn_away_from_point(Pl_objp, &enemy_pos, 1.0f);
48754876
} else {
48764877
vec3d goal_pos;
48774878

48784879
vm_vec_scale_add(&goal_pos, &En_objp->pos, &En_objp->orient.vec.fvec, -100.0f);
4879-
turn_towards_point(Pl_objp, &goal_pos, NULL, Pl_objp->phys_info.speed);
4880+
turn_towards_point(Pl_objp, &goal_pos, NULL, 1.0f);
48804881
}
48814882

48824883
// Set speed.
@@ -5313,9 +5314,9 @@ void evade_ship()
53135314
// caused flying in an odd spiral.
53145315
vm_vec_scale_add(&goal_point, &enemy_pos, &Pl_objp->orient.vec.rvec, 1000.0f);
53155316
if (dist < 100.0f)
5316-
bank_override = Pl_objp->phys_info.speed;
5317+
bank_override = 1.0f;
53175318
} else {
5318-
bank_override = Pl_objp->phys_info.speed; // In enemy's sights, not pointing at him, twirl away.
5319+
bank_override = 1.0f; // In enemy's sights, not pointing at him, twirl away.
53195320
goto evade_ship_l1;
53205321
}
53215322
} else {
@@ -7662,7 +7663,7 @@ void ai_chase_attack(ai_info *aip, ship_info *sip, vec3d *predicted_enemy_pos, f
76627663

76637664
//SUSHI: Don't change bank while circle strafing or glide attacking
76647665
if (dist_to_enemy < 250.0f && dot_from_enemy > 0.7f && aip->submode != AIS_CHASE_CIRCLESTRAFE && aip->submode != AIS_CHASE_GLIDEATTACK) {
7665-
bank_override = Pl_objp->phys_info.speed;
7666+
bank_override = 1.0f;
76667667
}
76677668

76687669
// If enemy more than 500 meters away, all ships flying there will tend to match bank.
@@ -7709,9 +7710,7 @@ void ai_chase_es(ai_info *aip)
77097710
tvec.xyz.y += frand();
77107711
}
77117712

7712-
float bank_override = Pl_objp->phys_info.speed;
7713-
7714-
ai_turn_towards_vector(&tvec, Pl_objp, nullptr, nullptr, bank_override, 0);
7713+
ai_turn_towards_vector(&tvec, Pl_objp, nullptr, nullptr, 1.0f, 0);
77157714
accelerate_ship(aip, 1.0f);
77167715
}
77177716

@@ -7722,7 +7721,6 @@ void ai_chase_ga(ai_info *aip, ship_info *sip)
77227721
{
77237722
// If not near end of this submode, evade squiggly. If near end, just fly straight for a bit
77247723
vec3d tvec;
7725-
float bank_override;
77267724
vec3d vec_from_enemy;
77277725

77287726
if (En_objp != NULL) {
@@ -7735,9 +7733,7 @@ void ai_chase_ga(ai_info *aip, ship_info *sip)
77357733
vm_vec_scale_add2(&tvec, &vec_from_enemy, 300.0f);
77367734
vm_vec_add2(&tvec, &Pl_objp->pos);
77377735

7738-
bank_override = Pl_objp->phys_info.speed;
7739-
7740-
ai_turn_towards_vector(&tvec, Pl_objp, nullptr, nullptr, bank_override, 0);
7736+
ai_turn_towards_vector(&tvec, Pl_objp, nullptr, nullptr, 1.0f, 0);
77417737

77427738
accelerate_ship(aip, 2.0f);
77437739

code/math/vecmat.cpp

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1901,7 +1901,7 @@ float time_to_arrival(float goal, float vel, float vel_limit, float acc_limit) {
19011901
// and also scales their speed to make a nice straight line
19021902
// note that this is now treated as a movement in linear space, despite the name
19031903
vec3d vm_angular_move(const vec3d* goal, float delta_t,
1904-
vec3d* vel, const vec3d* vel_limit, const vec3d* acc_limit, bool aggressive_bank, bool force_no_overshoot, bool no_directional_bias)
1904+
vec3d* vel, const vec3d* vel_limit, const vec3d* acc_limit, bool no_bank, bool force_no_overshoot, bool no_directional_bias)
19051905
{
19061906
vec3d ret, slow;
19071907
vm_vec_make(&slow, 1.f, 1.f, 1.f);
@@ -1915,12 +1915,12 @@ vec3d vm_angular_move(const vec3d* goal, float delta_t,
19151915
// so they arrive at approximately the same time as the slowest component, so the path there is nice and straight
19161916
float max = fmax(slow.xyz.x, fmax(slow.xyz.y, slow.xyz.z));
19171917
if (max != 0) vm_vec_scale(&slow, 1 / max);
1918-
if (aggressive_bank) slow.xyz.z = 1.f;
19191918
}
19201919

19211920
ret.xyz.x = vm_angular_move_1dimension(goal->xyz.x, delta_t, &vel->xyz.x, vel_limit->xyz.x, acc_limit->xyz.x, slow.xyz.x, force_no_overshoot);
19221921
ret.xyz.y = vm_angular_move_1dimension(goal->xyz.y, delta_t, &vel->xyz.y, vel_limit->xyz.y, acc_limit->xyz.y, slow.xyz.y, force_no_overshoot);
1923-
ret.xyz.z = vm_angular_move_1dimension(goal->xyz.z, delta_t, &vel->xyz.z, vel_limit->xyz.z, acc_limit->xyz.z, slow.xyz.z, force_no_overshoot);
1922+
if (!no_bank)
1923+
ret.xyz.z = vm_angular_move_1dimension(goal->xyz.z, delta_t, &vel->xyz.z, vel_limit->xyz.z, acc_limit->xyz.z, slow.xyz.z, force_no_overshoot);
19241924
return ret;
19251925
}
19261926

@@ -2012,7 +2012,7 @@ void vm_angular_move_matrix(const matrix* goal_orient, const matrix* curr_orient
20122012
// orient => current orientation matrix (with current forward vector)
20132013
// w_in => current input angular velocity
20142014
// delta_t => this frametime
2015-
// delta_bank => desired change in bank in degrees
2015+
// bank_vel => desired bank velocity
20162016
// next_orient => the orientation matrix at time delta_t (with current forward vector)
20172017
// w_out => the angular velocity of the ship at delta_t
20182018
// vel_limit => maximum rotational speed
@@ -2027,15 +2027,14 @@ void vm_angular_move_matrix(const matrix* goal_orient, const matrix* curr_orient
20272027
// function attempts to rotate the forward vector toward the goal forward vector taking account of anglular
20282028
// momentum (velocity) Attempt to try to move bank by goal delta_bank.
20292029
// called "vm_forward_interpolate" in retail
2030-
void vm_angular_move_forward_vec(const vec3d* goal_f, const matrix* orient, const vec3d* w_in, float delta_t, float delta_bank,
2030+
void vm_angular_move_forward_vec(const vec3d* goal_f, const matrix* orient, const vec3d* w_in, float delta_t, float bank_vel,
20312031
matrix* next_orient, vec3d* w_out, const vec3d* vel_limit, const vec3d* acc_limit, bool no_directional_bias)
20322032
{
20332033
vec3d rot_axis;
20342034
vm_vec_cross(&rot_axis, &orient->vec.fvec, goal_f); // Get the direction to rotate to the goal
20352035
float cos_theta = vm_vec_dot(&orient->vec.fvec, goal_f); // Get cos(theta) where theta is the amount to rotate
20362036
float sin_theta = fmin(vm_vec_mag(&rot_axis), 1.0f); // Get sin(theta) (cap at 1 for floating point errors)
2037-
vec3d theta_goal;
2038-
vm_vec_make(&theta_goal, 0, 0, delta_bank); // theta_goal will contain the radians to rotate (in the same direction as rot_axis but in local coords)
2037+
vec3d theta_goal = vmd_zero_vector; // theta_goal will contain the radians to rotate (in the same direction as rot_axis but in local coords)
20392038

20402039
if (sin_theta <= SMALL_NUM) { // sin(theta) is small so we are either very close or very far
20412040
if (cos_theta < 0) { // cos(theta) < 0, sin(theta) ~ 0 means we are pointed exactly the opposite way
@@ -2063,15 +2062,24 @@ void vm_angular_move_forward_vec(const vec3d* goal_f, const matrix* orient, cons
20632062

20642063
// derive theta from sin(theta) for better accuracy
20652064
vm_vec_copy_scale(&theta_goal, &local_rot_axis, (cos_theta > 0 ? asinf_safe(sin_theta) : PI - asinf_safe(sin_theta)) / sin_theta);
2066-
2067-
// reset z to delta_bank, because it just got cleared
2068-
theta_goal.xyz.z = delta_bank;
20692065
}
20702066

20712067
// calculate best approach in linear space (returns velocity in w_out and position difference in rot_axis)
20722068
*w_out = *w_in;
20732069
rot_axis = vm_angular_move(&theta_goal, delta_t, w_out, vel_limit, acc_limit, true, false, no_directional_bias);
20742070

2071+
// handle bank separately, simpler, since its just a target velocity
2072+
{
2073+
float delta_bank_vel = bank_vel - w_in->xyz.z;
2074+
float delta_bank_accel = fl_abs(delta_bank_vel) / delta_t; // the accel required to reach the target vel this frame
2075+
float accel = (delta_bank_accel > acc_limit->xyz.z) ? acc_limit->xyz.z : delta_bank_accel;
2076+
if (delta_bank_vel < 0)
2077+
accel = -accel;
2078+
2079+
rot_axis.xyz.z = w_in->xyz.z * delta_t + accel * delta_t * delta_t * 0.5f; // vt + 1/2at^2
2080+
w_out->xyz.z = w_in->xyz.z + accel * delta_t;
2081+
}
2082+
20752083
// normalize rotation axis and determine total rotation angle
20762084
float theta = vm_vec_mag(&rot_axis);
20772085
if (theta > SMALL_NUM)

0 commit comments

Comments
 (0)