1 / 51

Collision Detection

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.

wwong
Download Presentation

Collision Detection

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Collision Detection Chapter 4 Interactions

  2. 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?

  3. 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!

  4. 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

  5. Symbol table • How could one implement a symbol table in Java?

  6. 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?

  7. 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 >();

  8. 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.

  9. Back to unity…

  10. The Object class • Note that the Object class has a name field (a string).

  11. 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.)

  12. The Object class • Find (any) one. good example

  13. The Object class • Find all. good example

  14. The GameObject class • Note that the GameObject class has a tag field (a string).

  15. 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.

  16. GameObject.FindWithTag

  17. GameObject.FindGameObjectsWithTag

  18. GameObject.FindGameObjectsWithTag • Example. • all enemies are tagged with “enemy” • This code iterates over all enemies, finds the nearest one, and prints its name.

  19. GameObject.Find Note support for hierarchy.

  20. GameObject.Find Rotates hand at every update.

  21. Recap • There are 4 ways to refer to objects in Unity: • by name • by tag • by type • by variable (the good, old fashioned way)

  22. collision detection: colliders basic collision detection

  23. 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.

  24. MonoBehaviour

  25. MonoBehaviour.OnTriggerEnter

  26. MonoBehaviour.OnCollisionEnter • Provides great detail.

  27. collision detection: raycasting advanced collision detection

  28. 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.

  29. Let’s do the math… “Mathematics for 3D Game Programming & Computer Graphics” by E. Lengyel is a good reference.

  30. Definition • Define a point or vector (in 3D space) as an ordered triple of numbers. P = <px, py, pz>

  31. Definition • Define a (parametric) line between two points, P1 and P2, as P(t) = (1-t) P1 + t P2, where t  [0..1].

  32. 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.

  33. 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).

  34. 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.

  35. Example raycasting problem • Observation: From any given viewpoint (ray), only three of these planes need to be considered? Why is this true?

  36. 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.

  37. 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.

  38. 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).

  39. 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.

  40. 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.

  41. Raycasting in Unity

  42. Raycasting and vectors predefined constants

  43. Physics.Raycast Cast a ray forward for 10 units and determine hit.

  44. Physics.Raycast Cast a ray downward, check for collision, and obtain info about hit.

  45. RaycastHit – collision info

  46. Physics.Raycast Cast a ray downward, check for collision within 100 units, and obtain info about hit.

  47. Physics.Raycast Cast a ray in direction of mouse click, and check for collision within 100 units.

  48. Physics.Raycast Cast a ray in direction of mouse click, check for collision within 100 units, and obtain info about hit.

  49. Using collision detection in games

  50. Collision detection in games How might one use collision detection in a game?

More Related