Physics CIS 488588 Bruce R Maxim UMDearborn 1232022
Physics CIS 488/588 Bruce R. Maxim UM-Dearborn 1/23/2022 1
Newtonian Physics • Successful prediction of movement requires the level understanding used by physics engines • All entities have certain physical properties (velocity, acceleration, center of mass) • Velocity is derivative of the position v = dx/dt • Acceleration is derivative of velocity A = dv/dt 1/23/2022 2
Numeric Integration • One big problem in physics implementations is keeping track of position changes over time • Position can be defined as the integral (or accumulation) of the velocity • Numeric integrators are algorithms that compute values that change over time • Integrators for position and velocity as time functions might be denoted as x(t) and v(t) 1/23/2022 3
Euler Integration • Takes approach of estimating next values for v(t) and x(t) based on the derivative and the current value v(t + t) = v(t) + a(t) t x(t + t) = x(t) + v(t) t • Computationally this becomes for clock ticks x_velocity = x_velocity + x_acceleration y_velocity = y_velocity + y_acceleration player_x = player_x + x_velocity player_y = player_y + y_velocity 1/23/2022 4
Problems with Euler Integration • Acceleration often changes between time steps t • Collisions need to be dealt with during time steps t • More sophisticated approaches can help the physics model, but are not usually needed from a AI perspective 1/23/2022 5
Perfect Intersection - 1 • It is possible to use physics to predict the collision of two moving points • In 3 D space position and velocity are p = [px, py , pz] and v = [vx, vy , vz] • The position of a player at time t will be p(t) = p + vt • If s is the speed of the projectile fired from the origin (0, 0, 0) then position will be |p(t)| = s 2 t 2 1/23/2022 6
Perfect Intersection - 2 • Computationally our aiming point becomes |p + vt| = s 2 t 2 (px + vx)2 + (py + vy)2 + (pz + vz)2 = s 2 t 2 • With an appropriate variable substitution we get at 2 + bt + c = 0 • We can use the quadratic equation to get t = -b – sqrt(b 2 – 4 ac) / 2 a 1/23/2022 7
Perfect Intersection - 3 • The coefficient values are (0 <= i <= 2) a = -s 2 + vi 2 b = 2 v ip i 2 c = p i 2 • Note the result is defined as long as the radical expression is positive (meaning the projectile is moving fast enough to hit target) • The positive quadratic root predicts negative values of t and really just predicts the past 1/23/2022 8
Predictor – 1 • Predict function implements the math model • Checks for visible players first • If nearby enemy is found, its position and velocity are monitored • The position and velocity values are plugged into the equation 1/23/2022 9
Predictor – 2 • Predict function implements the math model • Checks for visible players first • If nearby enemy is found, its position and velocity are monitored • The position and velocity values are plugged into the equation 1/23/2022 10
Mathematical Model const Vec 3 f e_pos = enemy. position - position; float a = -k_Projectile. Speed * k_Projectile. Speed, b = 0. 0 f, c = 0. 0 f; for (int i=0; i<3; ++i) { a += enemy. velocity[i] * enemy. velocity[i]; b += e_pos[i] * enemy. velocity[i]; c += e_pos[i] * e_pos[i]; } b *= 2. 0 f; // use constants to determine time of intersection const float t = (-b - sqrtf(b*b - 4*a*c)) / (2*a); // direction of travel is based on velocity return enemy. position + enemy. velocity * t; 1/23/2022 11
Predicting Behavior – 1 • When predicting movement of living things, it does not matter how well the equations and integrator performs • Real world creature behavior is not governed by rules • There is lots of uncertainty in forces creatures can apply and their acceleration can vary 1/23/2022 12
Predicting Behavior – 2 • Neither AI or human players have any knowledge of internal object states • Object movement is observed and velocity is estimated by 3 observations at different times – Noting initial position – Understand velocity by processing position change – Extract acceleration by noting velocity changes • Estimating velocity and acceleration based on stopped/running basis is better than lag caused by trying to average observations 1/23/2022 13
Simulation Algorithm • Simulation can be used to predict target collisions as opposed to creating a perfect physics equations • The simulation algorithm can use successive iterations to find an acceptable estimate for the point in time for the intersection of the paths of the projectile and its target 1/23/2022 14
1/23/2022 15
Finding Collision Point Using Forward Integration repeat // update enemy position during time step enemy. position += enemy. position + delta; // increment time step time += delta; // compute projectile flight time to enemy flight_time = length(enemy. position – origin) / projectile. speed; // compute distance between enemy and projectile difference = abs(time – flight_time); until((difference < threshold) or (time > max_time)); 1/23/2022 16
Evaluation • Linear search for collision point is OK, as long as it can be found after a small number of iterations • If we used a much larger time step the point of collision could be found in O(log(n)) time • If the simulation goes to far, we need to back and redo the last step with a smaller delta 1/23/2022 17
Code Needed to Adjust Delta // if sign changes simulation has gone too far if sign XOR time < flight_time then // reverse direction and decrease delta = - delta / 2. 0; // remember when enemy is farther than projectile sign = time < flight_time; 1/23/2022 18
Evaluation • There are few cases where the updated version of the algorithm is preferred • The author claims that in practice this approach often takes longer to compute the same result as the first algorithm • A purely mathematical algorithm might be better if greater precision is needed • In the first algorithm it is easier for AI to anticipate changes in enemy velocity 1/23/2022 19
Colin • Anticipates enemy position based on what it can see • Checks to see is enemy path is blocked by a wall • If wall can be side-stepped enemy velocity can be adjusted by guessing • If wall is major blockage simulation is halted and that point is used as a target 1/23/2022 20
Experimentation - 1 • To emphasize benefits of prediction the animats use a blaster that fire slow projectiles • Fight finish, but last long enough to evaluate animat abilities • Forcing animats to shoot from great distance also showcases their prediction ability (in fact these animats will not attack “close” enemies) • Simple movement code can be used to test prediction (e. g. Pinbot) 1/23/2022 21
Experimentation - 2 • Animats need to be modified to look in direction or enemy during combat rather than in the direction of travel • To allow movement without seeing the obstacles, requires the animats to use physical sensors to bounce off walls • Steering behaviors are acceptable using this strategy since humans often cannot look backward while firing weapons 1/23/2022 22
Evaluation – 1 • In many cases shooting without prediction can be acceptable (e. g. enemy close by or running in line of fire) • At distance, predictions skills are surprisingly good for Colin (esp. in large areas with few turns required for movement) • Moving average gives good performance, but needs to be biased toward recently observed velocity (e. g. 80%/20%) 1/23/2022 23
Evaluation – 2 • With two animats movement prediction is essentially one dimensional • When animats are on different levels requires a 2 D prediction strategy • The mathematical model will be fooled by a animat running up a stairway • Colin will be fooled by enemies running into walls and will not shoot at them • Bots using dodging tactics will also fool Colin into firing where the bot was last 1/23/2022 24
- Slides: 24