Presentation is loading. Please wait.

Presentation is loading. Please wait.

 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?

Similar presentations


Presentation on theme: " 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?"— Presentation transcript:

1

2  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  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  “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  How could one implement a symbol table in Java?

6 1. Implement the following: class SymbolTableEntry { String name;//key //additional information here: Object o;//value } SymbolTableEntry symbolTable[] = new SymbolTableEntry[N]; 1.But how do we find an entry? 2.What happens if we need more (or less) than N entries?

7  How could one implement a symbol table in Java? 2. Use Java’s built in HashMap. HashMap symbolTable = new HashMap ();

8  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/m ap/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

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

11 It also has two useful (static/class) methods: 1. FindObjectOfType- just one 2. FindObject s OfType- 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  Find (any) one. good example

13  Find all. good example

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

15 GameObject inherits from Object. It is the base class for all entities in Unity scenes. Provides useful static methods: 1. FindWithTag Returns one active GameObject tagged tag. Returns null if no GameObject was found. 2. FindGameObjectsWithTag Returns a list of active GameObjects tagged tag. Returns null if no GameObject was found. 3. Find Finds a game object by name (from Object), and returns it.

16

17

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

19 Note support for hierarchy.

20 Rotates hand at every update.

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

22 basic collision detection

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

25

26  Provides great detail.

27 advanced collision detection

28  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 “Mathematics for 3D Game Programming & Computer Graphics” by E. Lengyel is a good reference.

30  Define a point or vector (in 3D space) as an ordered triple of numbers. P =

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

32  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  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: Intersection of a ray and a box. Let a box, B, be defined by six planes: 1.x = 0(left) 2.x = r x (right) 3.y = 0(bottom) 4.y = r y (top) 5.z = 0(far/back) 6.z = r z (near/front) where r x, r y, and r z are the dimensions of B.

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

36  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  Examine components of V (individually) to determine which planes need to be considered. › Using V x as an example:  If V x = 0, then the ray cannot intersect either of the planes x = 0 or x = r x because V is parallel to them.  If V x > 0, then we do not need to check the plane x = r x since it faces away from V.  If V x < 0, then we don’t need to check the plane x = 0 (since it faces away from V). › And similarly for V y and V z.

38  Now we have the point where a ray intersects a plane (but it may be a point that is outside of the face).

39  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 = r x is given by t = (r x – Q x ) / V x.

40  Continuing this example, to be within the face, the y and z values of P(t) must satisfy both of the following: 1. 0 ≤ P(t) y ≤ r y 2. 0 ≤ P(t) z ≤ r z  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

42 predefined constants

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

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

45

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

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

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

49

50 How might one use collision detection in a game?

51 1. traps, mines, bombs, etc. 2. teleportation 3. acquire assets 4. secret doorways/portals/gates 5. enter a vehicle 6. projectile collision 7. Does the enemy see me?  S/he should only see me when facing me, and when not too far away.  What if I’m hiding behind something? Can the enemy see through things?)


Download ppt " 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?"

Similar presentations


Ads by Google