510 likes | 549 Views
Collision Detection. Chapter 4 Interactions. But before we begin collision detection. How can we find/refer to objects at runtime? This must be dynamic because objects may come and go. How can we solve this problem?. But before we begin collision detection.
E N D
Collision Detection Chapter 4 Interactions
But before we begin collision detection . . . • How can we find/refer to objects at runtime? • This must be dynamic because objects may come and go. • How can we solve this problem?
But before we begin collision detection . . . • How can we find/refer to objects at runtime? • This must be dynamic because objects may come and go. • How can we solve this problem? • Use a symbol table!
Symbol table • “In computer science, a symbol table is a data structure used by a language translator such as a compiler or interpreter, where each identifier in a program's source code is associated with information relating to its declaration or appearance in the source, such as its type, scope level and sometimes its location.” • from http://en.wikipedia.org/wiki/Symbol_table
Symbol table • How could one implement a symbol table in Java?
Symbol table • How could one implement a symbol table in Java? 1. Implement the following: class SymbolTableEntry { String name; //key //additional information here: Object o; //value } SymbolTableEntry symbolTable[] = new SymbolTableEntry[N]; • But how do we find an entry? • What happens if we need more (or less) than N entries?
Symbol table • How could one implement a symbol table in Java? 2. Use Java’s built in HashMap. HashMap< String, Object > symbolTable = new HashMap< String, Object >();
Symbol table • Java’s HashMap does not allow duplicate keys. • Use MultiValueMap for duplicate keys (see http://commons.apache.org/collections/api-release/org/apache/commons/collections/map/MultiValueMap.html). • Or use an array (or ArrayList) of values per key in a regular HashMap. • C++ also has hash_map and hash_multimap.
The Object class • Note that the Object class has a name field (a string).
The Object class • It also has two useful (static/class) methods: • FindObjectOfType - just one • FindObjectsOfType - more than one • (But it lacks a method to find things by name! But if one knows the type, one can then check the names. Or we will see GameObject.Find() shortly.)
The Object class • Find (any) one. good example
The Object class • Find all. good example
The GameObject class • Note that the GameObject class has a tag field (a string).
The GameObject class • GameObject inherits from Object. • It is the base class for all entities in Unity scenes. • Provides useful static methods: • FindWithTag • Returns one active GameObject tagged tag. Returns null if no GameObject was found. • FindGameObjectsWithTag • Returns a list of active GameObjects tagged tag. Returns null if no GameObject was found. • Find • Finds a game object by name (from Object), and returns it.
GameObject.FindGameObjectsWithTag • Example. • all enemies are tagged with “enemy” • This code iterates over all enemies, finds the nearest one, and prints its name.
GameObject.Find Note support for hierarchy.
GameObject.Find Rotates hand at every update.
Recap • There are 4 ways to refer to objects in Unity: • by name • by tag • by type • by variable (the good, old fashioned way)
collision detection: colliders basic collision detection
MonoBehaviour • Recall that scripts that extend MonoBehaviour (MB) can be associated with game objects. • MB provides a number of methods that may be used to implement basic collision detection.
MonoBehaviour.OnCollisionEnter • Provides great detail.
collision detection: raycasting advanced collision detection
Raycasting • Drawback of collision detection via colliders is that the two objects must come in contact with each other first. • There may be a discernable “bump.” • Imagine an automatic door. No contact is necessary. • Raycasting is more predictive and avoids contact.
Let’s do the math… “Mathematics for 3D Game Programming & Computer Graphics” by E. Lengyel is a good reference.
Definition • Define a point or vector (in 3D space) as an ordered triple of numbers. P = <px, py, pz>
Definition • Define a (parametric) line between two points, P1 and P2, as P(t) = (1-t) P1 + t P2, where t [0..1].
Definition • Define a ray starting at point Q (i.e., at t=0), and extending to ∞ as P(t) = Q + t V, where t ≥ 0, and V is a direction vector.
Raycasting problem • Where (if any) does a ray intersect a surface? • For simplicity, intersections are computed in object space (where the world and object origins coincide, and object coordinate system axes are aligned with world axes).
Example raycasting problem • Example: Intersection of a ray and a box. Let a box, B, be defined by six planes: • x = 0 (left) • x = rx (right) • y = 0 (bottom) • y = ry (top) • z = 0 (far/back) • z = rz (near/front) where rx, ry, and rz are the dimensions of B.
Example raycasting problem • Observation: From any given viewpoint (ray), only three of these planes need to be considered? Why is this true?
Example raycasting problem • Observation: From any given viewpoint (ray), only three of these planes need to be considered? Why is this true? At most, only three planes may face V (and at least three face away from V) for any V.
Example raycasting problem • Examine components of V (individually) to determine which planes need to be considered. • Using Vx as an example: • If Vx = 0, then the ray cannot intersect either of the planes x = 0 or x = rx because V is parallel to them. • If Vx > 0, then we do not need to check the plane x = rx since it faces away from V. • If Vx < 0, then we don’t need to check the plane x = 0 (since it faces away from V). • And similarly for Vy and Vz.
Example raycasting problem • Now we have the point where a ray intersects a plane (but it may be a point that is outside of the face).
Example raycasting problem • So next, we need to examine the two coordinates corresponding to the directions parallel to the plane. • For example, the value of t corresponding to the point where the ray P(t) = Q + t V intersects the plane x = rx is given by t = (rx – Qx) / Vx.
Example raycasting problem • Continuing this example, to be within the face, the y and z values of P(t) must satisfy both of the following: • 0 ≤ P(t)y ≤ ry • 0 ≤ P(t)z ≤ rz • If both are satisfied, then this face is intersected. (No additional checks are necessary.) • If both are not satisfied, then check remaining faces for intersection.
Raycasting and vectors predefined constants
Physics.Raycast Cast a ray forward for 10 units and determine hit.
Physics.Raycast Cast a ray downward, check for collision, and obtain info about hit.
Physics.Raycast Cast a ray downward, check for collision within 100 units, and obtain info about hit.
Physics.Raycast Cast a ray in direction of mouse click, and check for collision within 100 units.
Physics.Raycast Cast a ray in direction of mouse click, check for collision within 100 units, and obtain info about hit.
Collision detection in games How might one use collision detection in a game?