Game Programming Algorithms and Techniques

Slides:



Advertisements
Similar presentations
Vectors, Points, Lines and Planes Jim Van Verth Lars M. Bishop
Advertisements

2.5. B ASIC P RIMITIVE I NTERSECTION Details of common forms of primitive intersection test.
Vector Calculus Mengxia Zhu Fall Objective Review vector arithmetic Distinguish points and vectors Relate geometric concepts to their algebraic.
Intersection Testing Chapter 13 Tomas Akenine-Möller Department of Computer Engineering Chalmers University of Technology.
2.3. B OUNDING V OLUMES Bounding volumes of use for collision detection.
Chapter 4.2 Collision Detection and Resolution. 2 Collision Detection Complicated for two reasons 1. Geometry is typically very complex, potentially requiring.
Collision Detection CSCE /60 What is Collision Detection?  Given two geometric objects, determine if they overlap.  Typically, at least one of.
Collision Detection and Resolution Zhi Yuan Course: Introduction to Game Development 11/28/
Computer graphics & visualization Collisions. computer graphics & visualization Simulation and Animation – SS07 Jens Krüger – Computer Graphics and Visualization.
Geometry Primer Lines and rays Planes Spheres Frustums Triangles Polygon Polyhedron.
10/10/02 (c) 2002 University of Wisconsin, CS 559 Last Time Finished viewing: Now you know how to: –Define a region of space that you wish to view – the.
Here is where my object is Here is where my object is going to be Here is where I want my object to be.
CHAPTER 12 Height Maps, Hidden Surface Removal, Clipping and Level of Detail Algorithms © 2008 Cengage Learning EMEA.
CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons.
CMPE 466 COMPUTER GRAPHICS Chapter 8 2D Viewing Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition by Donald Hearn,
Introduction to Computer Graphics Chapter 6 – 2D Viewing Pt 2 1.
Computational Geometry & Collision detection
Week 14 - Monday.  What did we talk about last time?  Bounding volume/bounding volume intersections.
Advanced Computer Graphics Spring 2014 K. H. Ko School of Mechatronics Gwangju Institute of Science and Technology.
Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.
1cs533d-winter-2005 Notes  More optional reading on web for collision detection.
Chapter 4.2 Collision Detection and Resolution. 2 Collision Detection Complicated for two reasons 1. Geometry is typically very complex, potentially requiring.
1 Geometry A line in 3D space is represented by  S is a point on the line, and V is the direction along which the line runs  Any point P on the line.
OBBTree: A Hierarchical Structure for Rapid Interference Detection Gottschalk, M. C. Lin and D. ManochaM. C. LinD. Manocha Department of Computer Science,
1cs533d-term Notes  list Even if you’re just auditing!
1Notes. 2 Time integration for particles  Back to the ODE problem, either  Accuracy, stability, and ease-of- implementation are main issues  Obviously.
Introduction to 3D Graphics John E. Laird. Basic Issues u Given a internal model of a 3D world, with textures and light sources how do you project it.
Cornell CS465 Fall 2004 Lecture 3© 2004 Steve Marschner 1 Ray Tracing CS 465 Lecture 3.
Week 13 - Wednesday CS361.
CS 450: Computer Graphics REVIEW: OVERVIEW OF POLYGONS
CSE 381 – Advanced Game Programming Quickhull and GJK.
Collision handling: detection and response
Computer Animation Rick Parent Computer Animation Algorithms and Techniques Collisions & Contact.
Week 13 - Monday.  What did we talk about last time?  Exam 2!  Before that…  Polygonal techniques ▪ Tessellation and triangulation  Triangle strips,
CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) Material originally prepared by Gareth Bellaby.
1 KIPA Game Engine Seminars Jonathan Blow Ajou University December 6, 2002 Day 10.
CIS 350 – I Game Programming Instructor: Rolf Lakaemper.
Advanced Computer Graphics Spring 2014 K. H. Ko School of Mechatronics Gwangju Institute of Science and Technology.
1 Dr. Scott Schaefer Intersecting Simple Surfaces.
Introduction to Particle Simulations Daniel Playne.
Collision Detection And Response Jae Chun KyungSoo Im Chau Vo Hoang Vu.
Computer Game Design and Development
1 Sage Demo 4 Collisions SAGE Lecture Notes Ian Parberry University of North Texas.
Lecture 9 From Vertices to Fragments. Objectives Introduce basic implementation strategies Clipping Rasterization hidden-surface removal.
Vectors and the Geometry
University of North Carolina at Greensboro
Computer Graphics Lecture 14 CLIPPING I Taqdees A. Siddiqi
Computer Graphics Lecture 21 Triangles and Planes Taqdees A
Background Shapes & Collision Resolution (Top-down and Side-scrolling)
Collision Detection Spring 2004.
Intersecting Simple Surfaces
Constructing Objects in Computer Graphics By Andries van Dam©
Mrs. Daniel’s Geometry Vocab List
Chapter 4.2 Collision Detection and Resolution
Collision handling: detection and response
Representing Motion Chapter 2.
Geometry.
Geometry – Pre-requisite Skills Mr. Rosilez
2.5. Basic Primitive Intersection
Object Intersection CSE 681.
© University of Wisconsin, CS559 Fall 2004
Motion in Real and Virtual Worlds
Computer Animation Algorithms and Techniques
Collision Detection.
CO Games Concepts Week 12 Collision Detection
Game Programming Algorithms and Techniques
Game Programming Algorithms and Techniques
Geometry.
GPAT – Chapter 7 Physics.
Presentation transcript:

Game Programming Algorithms and Techniques Chapter 7 Physics

Chapter 7 Objectives Planes, Rays, and Line Segments Learn about additional geometric objects that are useful for physics systems Collision Geometry How do we simplify geometry for collision tests? Collision Detection Various instantaneous collision checks as well as continuous checks Physics-Based Movement Linear mechanics Numeric integration Brief rundown of physics middleware

Planes A plane is a flat, two-dimensional surface that extends infinitely. In a game, we might use planes to abstract the ground, walls, and so on. Plane equation: P is a point on the plane. n-hat is the normal of the plane. d is the minimum distance between the plane and the origin.

Planes, Cont'd Given a triangle, it is straightforward to construct a plane: P is any point on the plane, and we know any of the three vertices of the triangle satisfy this. The normal can be computed from a plane, as covered in Chapter 4. Once we have both P and n-hat, we can calculate d. Games will usually store n-hat and d in memory: struct Plane Vector3 normal float d end

Rays A ray starts at a specific point and travels infinitely in a direction. Can be represented by a parametric equation: t >= 0. R0 is the starting point of the plane. v is the direction of the ray.

Line Segments A line segment starts at one point and ends at another. Given a start point P0 and an end point P1, we can solve for v: Then we just restrict t to be between 0 and 1.

Ray Cast Many engines use the term "ray cast" when really it's a line segment: struct RayCast Vector3 startPoint Vector3 endPoint end Test for intersection against objects in the world. Uses include: Basic bullets Aiming reticule AI visibility Fresnel acoustic diffraction

Collision Geometry The goal is to simplify objects for testing whether they intersect with each other. There are a number of simplified representations we might use, some more computationally expensive than others. Often we might use multiple levels of collision geometry, from less accurate to more accurate.

Bounding Sphere The simplest type of collision geometry used Very fast to check the intersection between two bounding spheres Simple to represent: class BoundingSphere Vector3 center float radius end

Bounding spheres for different objects Bounding Sphere, Cont'd Certain types of objects are poorly represented by bounding spheres and have a lot of false positives. Bounding spheres for different objects

Axis-Aligned Bounding Box In 2D, an AABB has every side parallel to a coordinate axis. In 3D, every side of 3D prism is parallel to a coordinate plane. Represented by a min and max point (in 2D this corresponds to bottom left and top right): class AABB2D Vector2 min Vector2 max end Still fairly efficient for calculations.

Axis-Aligned Bounding Box, Cont'd Because the bounding box is axis-aligned, rotations can cause more false positives: Axis-aligned bounding boxes for different orientations of a character

Oriented Bounding Box Like an AABB, but no axis restriction. This means the OBB can rotate with no axis restriction. Can be represented in different ways, but calculations are far more complex than an AABB.

Capsule In 2D, a capsule is a rectangle with two semi-circles on the top and bottom: In 3D, it's a cylinder with two hemispheres. Humanoid surrounded by a capsule

Capsule, Cont'd In code, we represent a capsule as a line segment with a radius: struct Capsule2D Vector2 startPoint Vector2 endPoint float radius end

Convex Polygon For more accurate collisions, use an arbitrary convex polygon to represent the collision geometry: This is the most complex collision geometry, though it's still far more efficient than using the actual geometry. A chair surrounded by a convex polygon

Lists of Collision Geometries We also can represent an object as more than one collision geometry. For example, a human could have: Sphere for head AABB for torso Convex polygons for arms/legs

Collision Detection Using mathematical equations to calculate whether one type of collision geometry intersects with another. Requires the use of linear algebra. We won't cover all the possible collision permutations, but rather some of the most common ones.

Two spheres intersect (a) and do not intersect (b) Sphere vs. Sphere Test whether the distance squared between two spheres is less than the sum of radii squared: Two spheres intersect (a) and do not intersect (b)

The four cases where two 2D AABBs definitely cannot intersect AABB vs. AABB Rather than testing for the cases where they do intersect, test for the four cases where they definitely do not intersect: The four cases where two 2D AABBs definitely cannot intersect

AABB vs. AABB, Cont'd For 2D AABBs, this tests for the four cases: function AABBIntersection(AABB2D a, AABB2D b) bool test = (a.max.x < b.min.x) || (b.max.x < a.min.x) || (a.max.y < b.min.y) || (b.max.y < a.min.y) return !test end

Line Segment vs. Plane We start out with the equation for a ray and a plane: We want to find the t such that R(t) is on the plane. So we substitute R(t) for P:

Line Segment vs. Plane, Cont'd Now solve for t: Notice that there's a possibility for a division by zero that we should check for.

Line Segment vs. Plane, Cont'd The division by zero occurs if the line segment is parallel to the plane: Line segment pointing parallel to the plane

Line Segment vs. Plane, Cont'd The final t value must be in the range of 0 to 1, or the line segment doesn't intersect For instance, less than 0 means the segment faces away: Line segment pointing away from the plane

Line Segment vs. Triangle First, construct the plane that the triangle is on, and test for line segment vs. plane intersection. If a point of intersection between the segment and plane exists, we need to then test whether it's inside or outside the triangle. The way to test this is to see whether it's inside each side of the triangle.

Line Segment vs. Triangle, Cont'd We want to check whether the point is clockwise or counterclockwise from each side of the triangle: Point inside a triangle (a) and outside a triangle (b)

Line Segment vs. Triangle, Cont'd For triangle ABC, to check whether P is inside the triangle: Construct a vector from A to B. Construct a vector from A to P. Take the cross product between these two vectors and normalize it. If the dot product between the vector calculated in step 3 and the normal of the triangle is positive, it means P is inside the triangle. Repeat these steps for every other side. Note: This works for any coplanar convex polygon.

Sphere-plane intersection in a case where they don’t intersect Sphere vs. Plane Make a second plane with the same normal, but with the center of the sphere as a point on the plane. Sphere-plane intersection in a case where they don’t intersect

Continuous Collision Detection What if two objects are travelling so quickly that their intersection is missed? This is known as the bullet-through-paper problem. Bullet-through-paper problem

Continuous Collision Detection, Cont'd We can use continuous collision detection to figure out whether two objects collided in between frames. One such collision is two moving spheres, known as swept spheres. Swept spheres, it turns out, are actually capsules.

Swept Sphere Intersection

Swept Sphere, Cont'd Given the position of the sphere last frame and current frame, we can calculate a velocity much like we did with line segments. Then we can represent the position of both spheres (P and Q) with parametric equations:

Swept Sphere, Cont'd We want to solve for t where the distance between the two spheres equals the sum of the radii: To make this possible to solve, we must square both sides first, and keep in mind that length squared is the same as dot product with self:

Swept Sphere, Cont'd Now substitute for P(t) and Q(t): Next, factor/group to make it more readable: Then apply the following substitution:

Swept Sphere, Cont'd Apply FOIL (first, outside, inside, last): Bring the right term over to the left side, then apply another substitution:

Swept Sphere, Cont'd This can then be solved with the quadratic equation: The discriminant, the value under the radical, is especially important: Negative means the spheres do not intersect. Zero means the spheres tangentially intersect once. Positive means they fully intersect.

Possible values of the discriminant in swept sphere intersection Swept Sphere, Cont'd Possible values of the discriminant in swept sphere intersection

Collision Response How we respond to the collision We need to ensure the response makes sense, and that there aren't side effects (like getting stuck): Two asteroids get stuck

Coefficient of Restitution Elastic if CR > 1, meaning the object moves faster after collision. Inelastic if CR < 1, meaning it moves slower. Most games will use inelastic collision.

Optimizing Collisions How to eliminate collisions as early as possible? One option is to partition the world, such as using a quadtree: A quadtree, where letters represent objects in the world

Linear Mechanics Newton's second law of motion says: Where: F = force m = mass a = acceleration Position is related to velocity and acceleration:

Linear Mechanics for Games We can apply forces to objects with a mass. Given the position and velocity last frame, based on these new forces, determine the position and velocity this frame. To do this, we need to use numeric integration. But first, a word on time steps…

Variable Time Steps If we're using numeric integration, we must use fixed time steps (using frame limiting). Otherwise, different time steps could cause things like different jump arcs: Different jump arcs caused by different sized time steps

Euler Integration Simplest type of numeric integration, but least accurate: class PhysicsObject // List of all the force vectors active on this object List forces Vector3 acceleration, velocity, position float mass function Update(float deltaTime) Vector3 sumOfForces = sum of forces in forces acceleration = sumOfForces / mass // Euler Integration position += velocity * deltaTime velocity += acceleration * deltaTime end

Semi-Implicit Euler Integration Exactly like Euler integration, but the velocity is updated first, before the position: // Semi-Implicit Euler Integration velocity += acceleration * deltaTime position += velocity * deltaTime This ends up being a bit more accurate, and is used in physics systems such as Box2D.

Velocity Verlet Integration Splits the time step into two, and becomes much more accurate: function Update(float deltaTime) Vector3 sumOfForces = sum of forces in forces // Velocity Verlet Integration Vector3 avgVelocity = velocity + acceleration * deltaTime / 2.0f // Position is integrated with the average velocity position += avgVelocity * deltaTime // Calculate new acceleration and velocity acceleration = sumOfForces / mass velocity = avgVelocity + acceleration * deltaTime / 2.0f end

Physics Middleware Third-party libraries that implement all the core aspects of physics, including collision detection/response and movement. 3D: Havok—Industry standard, used by lots of major games PhysX—Also a popular solution, default physics system in Unreal 3 2D: Box2D—Most popular 2D system, ported to many platforms