CIS 488/588 Bruce R. Maxim UM-Dearborn Physics CIS 488/588 Bruce R. Maxim UM-Dearborn 12/5/2018
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 12/5/2018
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) 12/5/2018
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 12/5/2018
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 12/5/2018
Perfect Intersection - 1 It is possible to use physics to predict the collision of two moving points In 3D 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)| = s2t2 12/5/2018
Perfect Intersection - 2 Computationally our aiming point becomes |p + vt| = s2t2 (px + vx)2 + (py + vy)2 + (pz + vz)2 = s2t2 With an appropriate variable substitution we get at2 + bt + c = 0 We can use the quadratic equation to get t = -b – sqrt(b2 – 4ac) / 2a 12/5/2018
Perfect Intersection - 3 The coefficient values are (0 <= i <= 2) a = -s2 + vi2 b = 2 vipi2 c = pi2 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 12/5/2018
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 12/5/2018
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 12/5/2018
Mathematical Model const Vec3f e_pos = enemy.position - position; float a = -k_ProjectileSpeed * k_ProjectileSpeed, b = 0.0f, c = 0.0f; 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.0f; // 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; 12/5/2018
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 12/5/2018
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 12/5/2018
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 12/5/2018
12/5/2018
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)); 12/5/2018
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 12/5/2018
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 = - delta / 2.0; // remember when enemy is farther than projectile sign = time < flight_time; 12/5/2018
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 12/5/2018
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 12/5/2018
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) 12/5/2018
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 12/5/2018
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%) 12/5/2018
Evaluation – 2 With two animats movement prediction is essentially one dimensional When animats are on different levels requires a 2D 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 12/5/2018