220 likes | 366 Views
Movement and physics. Mark Nelson mjas@itu.dk. Movement. You will probably want some things moving Lots of ways to do that General tradeoff between physics and animation And hybrid approaches. Basic movement model. Recall Pong Velocity per object Update position per frame
E N D
Movement and physics • Mark Nelson • mjas@itu.dk Fall 2012 www.itu.dk
Movement You will probably want some things moving Lots of ways to do that General tradeoff between physics and animation And hybrid approaches
Basic movement model Recall Pong Velocity per object Update position per frame Control by changing an object’s velocity: Rotate Speed up / slow down
Smoothed movement Instantaneous velocity changes aren’t always great ”Natural” motion is often non-linear Mario moving platforms speed up and slow down at path ends Even blinking lights aren’t always linear
Simple smoothing Send velocity changes via an API Now, each game-loop iteration: Update position based on velocity Update velocity based on curve Can be pre-programmed animation (platform movement) Or, reactive (e.g. with some lag)
Physics-based smoothing The reactive version is basically velocity+acceleration: Objects have velocity Positions are updated based on velocity (1st derivative of position) Velocities are updated based on acceleration (2nd derivative of position) Acceleration can be the basic unit, or computed from force F = ma, so equivalent for a constant-mass object
Physics versus animation Why not always physics? Physics can be an awkward inverse problem Want a specific smooth motion: Have to calculate what combination of forces would produce it Then run a physics simulation just to get that back again
Curve-following Say we want to follow a specific curved path Robotics-style reactive controller: If we’re off the path, apply corrective velocity Produces smooth, physically realistic, but ”laggy” path following Can follow more closely: Predictive controller, e.g. start turning a bit before a turn Analytical solution, like calculating a satellite’s thrust schedule
Inverse kinematics Kinematics: How objects move in reaction to forces Inverse kinematics: How to apply forces to get objects to move in specific ways Hard in general
Some more pros/cons When not physics: Weird emergent effects to debug Want effects you don’t want to fully simulate, e.g. tractor-beam motion When physics: Want unscripted interaction between animations and other objects E.g. Platform on a pre-animated path, but can be knocked off-course by player colliding with it
Some animation approaches How do we follow a path? Waypointing Animator gives specific points on a path Animation system moves objects between them Simplest is just drawing straight lines, but often want smoothing
Parametric curves General representation of paths Function X(t) gives object position from t_start to t_end
Bezier curves Probably the most common parameterized curve Quadratic Bezier: X(t) = (1-t)2P0 + 2(1-t)P1+t2P2
Some facts about parametric curves speed(t) = |X’(t)| distance(t) = integrate speed(t) from t_start to t Plus, lots more useful relationships, e.g. curvature
Constant-speed motion Often want to move at constant speed along a curve Or at least, want to control our speed separately, by varying t Not always easy to define a curve so that it comes with constant speed(t) Solve by noticing: Constant speed means constant distance traveled
Constant-speed motion Reparameterize by arc length Choose desired arc position p we want to be at Solve distance(t)= p Done by finding root of distance(t) – p = 0 (Requires numeric integration, usually Newton’s method)
Bezier curves Notice that almost all Bezier curves don’t start out as constant-speed curves! Also, can sometimes have weird artifacts Loops, backwards travel More common in higher-degree curves May be inappropriate, or need more constraints, if you have obstacles
Physics engine [see other slides]
2d platformer physics Similar general principles But some things can be simplified Grid-based methods Pixel-based methods Collision response Bouncing off, stopping Special-case being ”on” a surface