Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
67535: Computer Games Programming Week 5: Physics II: • rigid body collision • fluids Tutorial: Geometry shader, transform feedback Rigid body description Position p and orientation o (together known as pose) Velocity v and spin (angular velocity) ω Acceleration a and angular acceleration dω Force f and torque τ ( F = ma; τ = I x dω ) Mass m and inertia tensor I Momentum p and angular momentum L Inertia Tensor http://techhouse.brown.edu/~dmorris/projects/tutorials/inertia.tensor.summary.pdf Inertia tensor defines distribution of mass in the volume Volume integral of mass Precomputed numerically 3x3 symmetric matrix (ok, tensor) Diagonal elements: Ixx = m ∫v (y2 + z2) dV Non-diagonal elements: Ixy = -m ∫v xy dV – zero for symmetrical objects Non-constrained motion For every force acting on body – calculate the force and the torque Calculate acceleration and angular acceleration – – a = Σ F/m dω = Σ τ x I-1 Integrate motion to get new position and orientation Use an ODE solver Fixed objects Setting 1/m = 0 and I-1 = zero 3x3 matrix ensures that the body moves nowhere and supports any weight Collision resolution During collision of two bodies, strong forces act in a short time Can be calculated by dynamics, with noninterpenetration approximated by forcefields Good for soft, deformable bodies (e.g. cloth of jelly) Rigid body collision No gradual deformation Shortcut: infinite force applied in zero time v(t) is not continuous, dynamics do not work Rigid body collisions Rigid body Dynamics with Collisions http://www.pixar.com/companyinfo/research/pbm2001/pdf/notesg.pdf For each time step tn For each object Compute sum of all forces Calculate acceleration an Integrate motion to get vn and pn If: collisions detected at tc Stop ODE solver Resolve collisions Update velocities (keep position & orientation) Restart ODE solver with new system state Collision detection Fact of collision Time of collision Points of contact NB: use the fact that things do not change a lot between t and t+h – e.g. Find a separating plane once and run collision detection only for elements of a separating plane and their neighbours Time of collision No intersection at tn; intersection at tn+h Find time of collision tc – e.g. by bisecting h – Rerun collision detection only for elements that are intersecting at tn+h Types of points of contact Face-to-vertex (F2V) Edge-to-edge (E2E) Vertex-to-vertex and vertex-to-edge – Degenerate cases – ignore and wait Everything else is a combination of F2V and E2E – e.g. Face-to-face is a 3x F2V or F2V + 2x E2E or 2x F2V + 2x E2E Managing points of contact Maintain a list of active points of contact – Pointers to bodies in contact • p: Point of contact in world coordinates – for vertex/face • n: face normal – for edge/edge • ea and eb: edge directions (n = ea x eb) Calculating relative velocities Calculate Vpa and Vpb: velocity of point of contact for both bodies: Vpa = Va + ωa x (pa-xa) Calculate velocity of pa relative to pb: Vrel = n . (Vpa -Vpb) Relative velocity Vrel>ε: bodies moving away from each other; remove contact point Vrel=0±ε: resting contact Vrel<-ε: collision contact Collision contact resolution Impulse: like force, but acts immediately, changing v and ω No friction: impulse acts along n only Calculated so that Vrel_after = -b*Vrel_before where b is a bounciness factor, [0 1] (derivation and code at http://www.pixar.com/companyinfo/research/pbm2001/pdf/notesg.pdf) Resting contact resolution Calculate and update a contact force at point of contact that: * prevents interpenetration * does not prevent separation * becomes zero upon separation A set of linear constraints on quadratic polynomials Requires a Quadratic Programming (QP) solver Collision resolution summary Update points of contact Update velocities at colliding contacts Calculate forces at resting contacts Update ODE solver with new velocities and resting forces NB: still no friction Rigid body Dynamics with Collisions http://www.pixar.com/companyinfo/research/pbm2001/pdf/notesg.pdf For each time step tn For each object Compute sum of all forces Calculate acceleration an Integrate motion to get vn and pn If: collisions detected at tc Stop ODE solver Resolve collisions Update velocities (keep position & orientation) Update forces Restart ODE solver with new system state Friction http://www.cs.cmu.edu/~baraff/papers/sig94.pdf Friction is a force ft at a contact point that is: • • • • Tangential to the contact surface Opposite to tangential acceleration Depends on normal force and contact face area At most μfn Accounting for friction adds constraints to QS; not solvable in extreme cases but usually O(n) Static and dynamic friction Static Friction •Tangential relative velocity is 0 •Added constraints: • ft must keep angular velocity at 0 • But not be higher than μfn Dynamic friction •ft = μfn •Direction is opposite to velocity at contact point Fluids Fluids http://software.intel.com/en-us/articles/fluid-simulation-for-video-games-part-1 Liquid, gas or plasma. Smoke is almost fluid: an aerosol, a combination of gas and particles Precise fluid models • Navier-Stokes (for viscous fluid) • Euler (for non-viscous fluid) https://www.youtube.com/watch?v=vOFcHqImXJ8 https://www.youtube.com/watch?v=MlNxgmPVF6U Simpler fluid models As particles: • • Properties averaged over nearby particles Or weighted by distance https://www.youtube.com/watch?v=Qve54Z71VYU https://www.youtube.com/watch?v=7ZEpYxqcqbc • As a volume: model flow at grid points (aka lattice) • lattice Bolzmann models • Or as both (Hybrid) https://www.youtube.com/watch?v=p67-Qiad5zc https://www.youtube.com/watch?v=pxDeVrJO5yY Volumetrics The unspoken assumption The world is mostly transparent with some non-transparent objects – Human vision is fine-tuned to perceive (most) solid objects as nontransparent, and (most) non-solid objects as transparent Can be modelled using outer surfaces only – That is why we get polygon meshes Complications The world is actually a 3D space full of matter with various visual and, generally, physical properties Some simple exceptions – Fog – Particle systems: fire, smoke, water mist – Transparent and semi-transparent objects Sometimes it is important to know what’s inside an object – Breaking, bending, exploding, cutting through… Modelling volumes Voxel – a volume element – A cube of space or a point of space in a grid Each voxel holds information about its content: – – – – Simplest case: boolean empty/full Transparency: float 0..1 or short uint 0...255 Material And other options, depending on task 3D object can be modelled as a 3D array of voxels MyVoxel volume[100][100][100]; Simple, but computationally heavy: 1million voxels for a not-too-good resolution 3D textures on GPU are useful here Use case: Realistic terrain – Caves, overhangs, arches... – Height map is too simple Use case: modeling physics Realistic destruction – Worms 4: Mayhem Solid objects, e.g. vehicles – Masters of Orion III – Red Alert Soft body dynamics – Deformation, tearing, bounce – http://www.alecrivers.com/fastlsm/ Use case: medicine Imaging: Volumetric data is Xray/Ultrasound transparency values Illustration: http://grahamj.com/ Images: Wikimedia commons Use case: fluid dynamics http://http.developer.nvidia.com/GPUGems3/gpugems3_ch30.html gas/liquid/plasma flow Tutorial Geometry shaders Particle system on GPU using transform feedback Geometry Shader Primitives Input Primitives: Points (1) Lines (2) Lines_adjacency (4) Triangles (3) Triangles_adjacency (6) Output Privitives: points line_strip triangle_strip Format layout (triangles) in; layout (line_strip, max_vertices = 4) out; * Input primitive is discarded * 0 or more primitives can be output * no more than max_vertices! #version 330 layout (points) in; Pos.y += 1.0; layout (triangle_strip) out; gl_Position = gVP * vec4(Pos, 1.0); layout (max_vertices = 4) out; TexCoord = vec2(0.0, 1.0); EmitVertex(); uniform mat4 gVP; uniform vec3 gCameraPos; Pos.y -= 1.0; Pos += right; out vec2 TexCoord; gl_Position = gVP * vec4(Pos, 1.0); TexCoord = vec2(1.0, 0.0); void main() { EmitVertex(); vec3 Pos = gl_in[0].gl_Position.xyz; vec3 toCamera = Pos.y += 1.0; normalize(gCameraPos - Pos); vec3 up = vec3(0.0, 1.0, 0.0); gl_Position = gVP * vec4(Pos, 1.0); vec3 right = cross(toCamera, up); TexCoord = vec2(1.0, 1.0); EmitVertex(); Pos -= (right * 0.5); gl_Position = gVP * vec4(Pos, 1.0); TexCoord = vec2(0.0, 0.0); EmitVertex(); EndPrimitive(); } Instancing using GS layout(invocations = 8) in; ... gl_Position = gl_InvocationID * gl_in[0].gl_Position.xyz; Setting up GS GLuint shader = glCreateShader(GL_GEOMETRY_SHADER); glShaderSource(shader, 1, “test.gs.glsl”, NULL); glCompileShader(shader); NOTE: the first one to extend ShaderIO class to GS and publish code on moodle gets +1 to next exercise