1 / 16

Today

Today. Homework Bounding Boxes Quadtrees Per-pixel collision Drawing text (if we have time). Secret Game. Two Parts Part I: Debug Syntax errors Logical errors Improvements Comments Part II: Complete Game Score Lives. Common Bounding Volumes.

risa
Télécharger la présentation

Today

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. Today • Homework • Bounding Boxes • Quadtrees • Per-pixel collision • Drawing text (if we have time)

  2. Secret Game • Two Parts • Part I: Debug • Syntax errors • Logical errors • Improvements • Comments • Part II: Complete Game • Score • Lives

  3. Common Bounding Volumes • Most introductory game programming texts call AABBs simply “bounding boxes” Circle/Sphere Axis-Aligned Bounding Box(AABB) Oriented Bounding Box (OBB) Convex Hull Better bounds, better culling Faster test, less memory

  4. Circle Bounding Box • Simple storage, easy intersection test • Rotationally invariant Compare Euclidean distance between circle centers against sum of circle radii. boolcircle_intersect(circle a, circle b) { Point d; // d = b.c – a.c d.x = a.c.x – b.c.x; d.y = a.c.y – b.c.y; int dist2 = d.x*d.x + d.y*d.y; // d dot d intradiusSum = a.r + b.r; if (dist2 <= radiusSum * radiusSum) { return true; } else { return false; } } r c struct circle { Point c; // center intr; // radius } struct Point { intx; inty; }

  5. Axis-Aligned Bounding Boxes (AABBs) • Three common representations • Min-max • Min-widths • Center-radius min // min.x<=x<=max.x // min.y<=y<=max.y struct AABB { Point min; Point max; } // min.x<=x<=min.x+dx // min.y<=y<=min.y+dy struct AABB { Point min; intdx; // x width intdy; // y width } // | c.x-x | <= rx | c.y-y | <= ry struct AABB { Point c; intrx; // x radius intry; // y radius } max min dx dy ry rx c Can easily be extended to 3D

  6. Axis Aligned Bounding Box Intersection (min-max) • Two AABBs intersect only if they overlap on both axes a.min.x=b.min.x a.min.x>b.max.x a.max.x<b.min.x a.max.y<b.min.y bool IntersectAABB(AABB a, AABB b) { { if (a.max.x < b.min.x || a.min.x < b.max.x) return false; if (a.max.y < b.min.y || a.min.y < b.max.y) return false; return true; } a.min.y=b.min.y a.min.y>b.max.y

  7. Axis Aligned Bounding Box Intersection (min-width) • Two AABBs intersect only if they overlap on both axes (a.min.x-b.min.x)>b.dx -(a.min.x-b.min.x)>a.dx a.min.x=b.min.x boolIntersectAABB(AABB a, AABB b) { { intt; t=a.min.x-b.min.x; if (t>b.dx || -t>a.dx) return false; t=a.min.y-b.min.y; if (t>b.dy || -t>a.dy) return false; return true; } // Note: requires more operations than // min-max case (2 more subtractions, 2 more negations) -(a.min.y-b.min.y)>a.dy a.min.y=b.min.y (a.min.y-b.min.y)>b.dy

  8. AABB Intersection (center-radius) • Two AABBs intersect only if they overlap on both axes a.c.x-b.c.x >a.rx+b.rx b.c.x-a.c.x >a.rx+b.ry a.c.x=b.c.x b.c.y-a.c.y > a.ry+b.ry bool IntersectAABB(AABB a, AABB b) { { if (Abs(a.c.x – b.c.x) > (a.r.dx + b.r.dx)) return false; if (Abs(a.c.y – b.c.y) > (a.r.dy + b.r.dy)) ) return false; return true; } // Note: Abs() typically single instruction on modern processors a.c.y=b.c.y a.c.y-b.c.y >a.ry+b.ry

  9. Trees root 56 • A tree is a data structure • Places contents into nodes • Each node has • Contents • Pointers to 0 or more other levels • Children are represented as references a b 45 75 d c 24 51 // A simple tree node class Class Node { List<Node> children; // List of references to Nodes int contents; } A tree with 5 nodes (root, a, b, c, d).Root node has contents 56.Node a has contents 45, and two children, c and d.Node b has contents 75, and no children.Node c has contents 24, and no children.Node d has contents 51, and no children.

  10. Tree Operations • Adding a child • Create new node • n = new Node(); • Add it to list of children of parent • parentNode.children.Add(n); • Removing a child • Find and remove child node from list of children • parentNode.children.Remove(childToRemove); • (childToRemove is reference to Node of child to be deleted)

  11. Point Quadtree root 0 1 • A tree where each node has four children • Each level represents subdividing space into 4 quadrants • Each node holds information about one quadrant • A deeper tree indicates more subdivisions MX Quadtree Demo http://donar.umiacs.umd.edu/quadtree/points/mxquad.html Demo both points and rectangles 0 3 1 2 2 3 root 1.1 1.0 0 3 1 2 0 1 1.2 1.3 1.0 1.1 1.2 1.3 2 3

  12. C# Node Representation class Node { Point min[4]; Point max[4]; int level; Node Quad[4]; // holds 4 quadrants List<IGameObject> objectList; // list of game objects in a Node } • quads is an array with four elements • Each element is a reference to a Node • Quad[0] – upper left, etc. • A recursive data structure • Nodes hold pointers to Nodes • Min[] is an array with four elements • Each element is upper left point of quadrant • Max[] holds lower right point of each quadrant • Level indicates how many levels down in the tree • Useful for bounding the depth of the tree Quad[0] Quad[1] Quad[2] Quad[3]

  13. Adding object to tree Insert(Node n, IGameObject g) • Iterate through all 4 quadrants of current node (n) • If at maximum depth of the tree • Add IGameObject to objectList and return • If AABB (bounding box) of IGameObject lies fully within a quadrant • Create child node for that quadrant, if necessary • Then call insert on that node (recursion) • Otherwise, AABB does not lie in just one quadrant • Add to contents list at this level • First call is Insert(root, g) • That is, start at root node for insertions

  14. Finding, removing object in tree Find(Node n, IGameObject g) • Iterate through all 4 quadrants of current node (n) • Check if AABB (bounding box) of IGameObject spans multiple quadrants • Yes: IGameObject is in this node. Return node. • At maximum level of tree? • Yes: IGameObject is at this level • If AABB (bounding box) of IGameObject lies fully within a quadrant • Call find on that node (recursion) • Removing object from tree • Found_node = Find(root, g) • Found_node.objectList.Remove(g)

  15. Per-pixel collision

  16. Drawing Text • Create a new SpriteFont in Content folder • Add a SpriteFontmyText variable to your game • In LoadContent(), use myText =Content.Load<SpriteFont>(“SpriteFont1”) • In Draw(), spriteBatch.Begin(); spriteBatch.DrawString(myText, … ); spriteBatch.End();

More Related