1 / 40

Lecture 3

Lecture 3. Announcements. Next week only: we’re in 506. Design checks. Sign up early Sign up early If you can’t make the one you signed up for, ask another student to switch The sooner you go, the sooner you can get feedback and fix potential problems

mervin
Download Presentation

Lecture 3

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. Lecture 3 Announcements

  2. Next week only: we’re in 506

  3. Design checks • Sign up early • Sign up early • If you can’t make the one you signed up for, ask another student to switch • The sooner you go, the sooner you can get feedback and fix potential problems • There are exactly 20 slots, and exactly 20 of you • Sign up early

  4. Tac 1+2 feedback • Use Vecs instead of x,y pair passing • It gets WAY worse in Tou • Beware cutting up sprites every time you draw • Don’t ignore compiler warnings! • Maybe nothing, but maybe dodging a bullet • Looking good! • Playtesting should be fun today!

  5. Tou has been released! • Shoot-’em-up, heavy use of collision detection • Much more open-ended than previous projects • Easier than previous projects if you work smart • Should help those of you combating the snowball of death TAC II TAC I TIC

  6. Announcements Questions?

  7. Lecture 3 Game Worlds

  8. Game Worlds Motivation

  9. How you probably did Tac Viewport Unit Unit Unit Unit Unit Unit Tile[][] Unit Unit Unit Unit Unit Unit AIUnit Unit Unit Unit Unit Bullet GameScreen Application FrontEnd

  10. How does this scale? • Imagine implementing four-player split-screen: Normal UI element Viewports

  11. Solution: separate world+screen Viewport Unit Unit Unit Unit Unit Unit Tile[][] Unit Unit Unit Unit Unit AIUnit Unit Unit Unit Unit Unit Bullet GameScreen Application World FrontEnd

  12. Game Worlds Responsibilities

  13. What does a world do? • Represents all game objects (“entities”) in a single space • “Owns” game coord system • Centralizes entity management • Maintains list, add/remove via queues or iterating over copy • Passes through ticks and draws to entities • Handles global logic • Anything beyond the scope of a single entity • E.g. providing collision detection callbacks to entities Unit Unit Unit Unit Unit Unit Enemy Unit Unit Background Unit Unit Bullet Boss Player Entities World logic World

  14. Entities Entity • Single logical “object” within the game • Stores all state relevant to itself, e.g. draw shape, collision shape, HP, attack cooldown… • Should always know what World it’s in • Lightweight superclass • High-level subclasses in engine, low-level in game • Receives events from World • More than just tick+draw! Physics Entity Background Bullet Damagable Enemy Player Boss

  15. Entity responsibilities • May draw, may not • Spawners, timers, • May use ticks, may not • Static environment objects, background… • Most will probably do both though • Player, items/pickups, • Player input? • Might be better handled by World, your call • Effects on other entities? • Might be better handled by World, your call

  16. Multi-pass logic • Ticking and drawing entities in the wrong order leads to undesirable behavior • Drawing background over everything else • Entities removing themselves during collision detection • World can selectively update state in order • E.g. tick all entities so they update position, *then* check for collisions

  17. Game Worlds Questions?

  18. Lecture 1 Collision Detection I

  19. Collision Detection I Motivation

  20. Collisions have consequences • Object collisions are extremelyimportant in everyday life • Extremely important in games too • Games that use collision detection vastly outnumber those that don’t

  21. What do we want to collide? • Points • Circles • Axis-Aligned Boxes • Convex polygons • Covered next week • Compound shapes • Other shapes • Not covered P min P1 P2 max P4 P5 P3 C r dim = (w,h) h x w y

  22. Collision Detection I Detection Algorithms

  23. Point-Circle • Check if the distance between the point and the center is less than or equal to the radius P P C C r r d d

  24. Circle-Circle • Check if the distance between the two centers is less than or equal to the sum of the radii C2 C2 C1 C1 d d r2 r2 r1 r1

  25. Point-AAB • Check if point is within range on each axis • AND P P max min max min x x y y

  26. AAB-AAB • Ensure overlap on each axis: • AND • AND

  27. Circle-AAB • Check if closest point to circle on AAB is in circle • Closest point: clamp C.x, C.y to [min.x, max.x], [min.y, max.y] • Then just do point-circle collision P P max min min max max min d P=C C C r r r d d=0

  28. Compound-anything • Compound shape checks against sub-shapes • Only compound shapes should ever need to iterate over children

  29. Collision Detection I Questions?

  30. Lecture 1 Tips for Tou I

  31. Write a collision debugger • Collision math is tricky • You will make mistakes • Test your collision code before even putting it in a game • This is “working smart” • You can try TA collision debuggers: dpercy, zdavis

  32. Watch out for key “ghosting” • Due to the way keyboards are built, sometimes keys don’t work while others are being held down • In many cases, as few as 3 can be held at a time • Hardware issue (can’t fix it), you must design around it • E.g. use mouse for some actions • More info, interactive demo:http://www.microsoft.com/appliedsciences/antighostingexplained.mspx

  33. Double dispatch • If you have a Circle and an AAB but only know that they’re Shapes, how do you determine which method to call? void testCollide() { Shapes = new Circle(); Shapes2= new AAB(); s.collides(s2); } abstract classShape { collidesCircle(Circlec); collidesAAB(AABaab); collidesComp(Compm); collides(Shape o); } boolean collides(Shapeo) { if (o instanceofCircle) { return collides((Circle) o); } elseif (o instanceofAAB) { return collides((AAB) o); } elseif (o instanceofComp) { return collides((Comp) o); } else { thrownewIllegalArgumentException(); } }

  34. Double dispatch interfaceShape { collides(Shape o); collidesCircle(Circle c); collidesAAB(AABaab); collidesComp(Comp m); } public class CircleimplementsShape { collides(Shapeo) { returno.collidesCircle(this); } collidesCircle(Circlec) { /*code*/ } collidesAAB(AABaab) { /*code*/ } collidesComp(Compm) { /*code*/ } } public class AABimplementsShape { collides(Shapeo) { returno.collidesAAB(this); } collidesCircle(Circlec) { /*code*/ } collidesAAB(AABaab) { /*code*/ } collidesComp(Compm) { /*code*/ } } public class CompimplementsShape { collides(Shapeo) { returno.collidesComp(this); } collidesCircle(Circlec) { /*code*/ } collidesAAB(AABaab) { /*code*/ } collidesComp(Compm) { /*code*/ } }

  35. Tips for Tou I Java Tip of the Week

  36. Variable arguments! • Wouldn’t it be nice if instead of this: List<Shape> l = new ArrayList<Shape>(); l.add(newCircle()); l.add(newAAB()); l.add(newAAB()); Compound c = newCompound(l); • Or this: Compound c = newCompound(newShape[] {newCircle(), newAAB(), newAAB()}); • You could do this? Compound c = newCompound(newCircle(), newAAB(), newAAB());

  37. Well you can! • The type of the last argument of a constructor/method may end in ‘…’ to indicate “any number of arguments” public Compound(Color c, Shape... shapes) • The “variable” argument will actually be an array of arguments (it’s really just wrapping array creation): public Compound(Color c, Shape... shapes) { for (int i=0; i < shapes.length; ++i) { add(shapes[i]); } }

  38. Passing arrays as varargs • Since it’s really just an array, it’s possible to just pass in an array if you already have one: static voidprintAllOnALine(Object... objs) { for (Object o : objs) System.out.print(“(”+o+”) ”); System.out.println(); } public static void main(String[] args) { printAllOnALine(args); List<?>ents = newWorld().getEntities(); printAllOnALine(list.toArray()); }

  39. Tips for Tou I Questions?

  40. Tac 2 playtesting! More fun than last week!

More Related