1 / 51

Lecture 1

Lecture 1. Announcements. Tic is over!. Collaboration policies!. Design checks. How was the signup process? Were they helpful ? Would you rather have more TA hours instead?. You can run demos!. cs195n_demo tac2 zdavis. cs195n_demo tic zdavis. Tic first impressions.

ludwig
Download Presentation

Lecture 1

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 1 Announcements

  2. Tic is over!

  3. Collaboration policies!

  4. Design checks • How was the signup process? • Were they helpful? • Would you rather have more TA hours instead?

  5. You can run demos! cs195n_demo tac2 zdavis cs195n_demo tic zdavis

  6. Tic first impressions • Lots of carrying around separate x, y • This leads to copy-pasting x’s code to write y’s code • Which leads to forgetting to replace an x with a y • Use Vec2i and Vec2f instead! • Lots of mouse usage, better than TAs • :-D

  7. BGD plug • The Brown Game Developers group is meeting on Friday, 4:30p-5:30p in 345 • Can be a useful source of advice, within the bounds of collab policy

  8. Announcements Questions?

  9. Lecture 1 Viewports

  10. Viewports Motivation

  11. Sometimes screen space is hard • Theoretically everything can be done in screen space • But some things can be very hard • Most notably, when the entire game isn’t visible at one time Video time!

  12. Game space vs. Screen space Game-space • In nearly all games, it makes sense to think of the game as existing in its own “space” • The UI in screen space has a “view” into the game world which is just like any other UI element 0,0 0,0 1,0 1,0 2,0 2,0 0,1 0,1 1,1 1,1 2,1 2,1 Screen 0,2 0,2 1,2 1,2 2,2 2,2 UI UI UI UI

  13. The math • Scale = screen size / game size • (in pixels per game coordinate unit) • Game point to screen: • Minus game upper left • Multiply by scale • Add screen upper left • Screen point to game: • Do the OPPOSITE of the steps IN REVERSE 0,0 1,0 0.5, 0.8 2,0 Scale: 100 px/unit 1.5, 2.0 2,1 120, 20 0,2 1,2 2,2 1.5, 2.0 1.0, 1.2 100, 120 220, 140 220, 140

  14. Viewports Implementation

  15. Implementing viewports • (Optional) Set the clip (g.clipRect()) • You will draw out of bounds otherwise • “Set” the “transform” • Draw the game-space in its own coordinates • “Restore” the “transform” • Restore the clip (if you set it)

  16. The “transform” • For many of you, this is as simple as: • When drawing a viewport, make a note inside your Graphics2D wrapper that you need to transform game->screen coordinates whenever a shape is drawn • Unmark when finished drawing the viewport • For cs123 students who want to be fancy, use AffineTransform and the setTransform() and getTransform() of Graphics2D • AffineTransform used like OpenGL

  17. Viewports Questions?

  18. Lecture 1 Pathfinding

  19. Pathfinding Motivation

  20. Why is pathfinding important? • NPCs need to navigate an environment that has obstructions • Represented as a graph • Goal: find minimum cost path from A to B • Cost includes factors such as distance, terrain, position of enemies.

  21. Pathfinding Dijkstra’s Algorithm

  22. Dijkstra’s • Basic idea: • Process nodes in order of shortest distance from start • To process a node, update cost to each neighbor and add to PriorityQueue, then never process this node again • Each node keeps track of shortest distance and pointer to previous node • When it’s time to process the end node, you’re done

  23. Why Dijkstra’s can be gross

  24. Pathfinding A*

  25. General idea • Dijkstra’s assumes it’s impossible to predict cost • This is overly pessimistic • In pathfinding, we at least know the general direction we want to go • A* is a graph traversal algorithm that takes advantage of this

  26. How does it work? • Uses a “heuristic” to guess the cost from any given node to the destination node • Heuristic passed by the caller • In addition to tracking distance from start, track heuristic value for each node • Prioritize in PriorityQueue based on distance+heuristic • This can be as simple as the Euclidean distance between the given and destination node, but can also try to take other factors • Just be sure to NEVER OVERESTIMATE (suboptimal results)

  27. Pathfinding Questions?

  28. Lecture 1 Content Management I

  29. Content Management I What is content?

  30. Content • Types of content • Sprites, images, textures • Music, sound effects • Level/map files • Scripts • Dialogue • A single logical piece of content is called an “asset”

  31. Content Management I Why not hardcode ASSETS?

  32. Hardcoding • Extreme executable bloat • Large games cannot fit in memory • Have to recompile entire program every time an asset changes • Still okay sometimes (e.g. the program icon in Windows) Your RAM: 8GB Dragon Age: Origins: 20GB

  33. Solution: break into files • Engine/game can load and unload assets as necessary or desired • It’s like JIT for your game content! • Non-programmers don’t need to compile • Level designers only need to touch map files • Artists only need to touch image files • Programmers compile builds for everyone else • More maintainable in general

  34. Content Management I Questions?

  35. Lecture 1 Tips for Tac I

  36. Tips for Tac I File Parsing

  37. File parsing! • Good news: game-side • Bad news: So many things can go wrong! • Map file can’t be opened • Map file is empty • Map file is a directory • Map file is a JPEG • Is a map file, but has inconsistent data

  38. Parse safely • Read in a line, then parse it, repeat • At least you can report the line count where an error happened • Recommended classes: • BufferedReader (for reading lines) • Scanner+StringReader (for parsing each line) • Catch, wrap, and rethrow exceptions • Level parsing should only throw a LevelParseException that is caused by other exceptions

  39. If possible, report errors nicely • Good errors: • “Line 1: ‘,’ is not a valid map size” • “Line 7: Row 3 was 24 tiles wide, map is 25 tiles wide” • “Line 2: Expected boolean, got ‘FLSE’ instead” • Bad errors: • “java.util.InputMismatchException (null)” • “The map could not be parsed. Goodbye.” • (program crashes with giant stack trace) • If this happens you will get an incomplete

  40. Tips for Tac I Unit Movement

  41. Unit movement 3.0, 0.0 • Tiles best stored in an array (discrete indices) • But game space is continuous! • Define tile x,y to take up space [x,x+1) and [y,y+1) • Then: Vec2i.fromFloored() • Move unit centers with Vec2f.lerpTo() 0,0 1,0 2,0 0.7, 0.6 0,1 1,1 2,1 1.5, 2.1 0,2 1,2 2,2

  42. Unit exclusion ??? ??? ??? ??? • Unit sitting on tile is obvious • But what about while moving? • When is it no longer on the first tile? • When does it officially reach the second tile? • Can it briefly monopolize two tiles at once?

  43. Tips for Tac I Miscellaneous Tips

  44. Miscellaneous Tips • Zooming is multiplicative, not additive • Rolling mouse wheel should * or / the scale factor • Watch out for weird behavior when changing a unit’s path while it’s moving • E.g. units moving diagonally

  45. Tips for Tac I Java Tip of the Week

  46. Generics are cool! • You’ve used generics before… but have you ever written them? • It’s as easy as: public classSimpleContainer<T> {privateT object;public voidsetObject(Tob) { object = ob; }publicTgetObject() { return object; }}

  47. Generics are cool! • Can use extends and super to bound the type public classAnimalHouse<AextendsAnimal> {privateA animal;public voidhouseAnimal(A a) { animal = a; }publicvoidfeedAnimal() { animal.eat(); }} AnimalHouse<Dog> kennel; // okay AnimalHouse<Rock> mountain; // compile error

  48. Bounds can be confusing • Ever used enums in Java? (if not, you will soon!) • This is the declaration of the Enum superclass: public classEnum<EextendsEnum<E>> • Starts to make sense once you realize it doesn’t infinitely expand • You will have to do something similar if you want to have a generifiedGraphNode object

  49. Want to know more? • Effective Java by Joshua Bloch has an excellent chapter on generics • Gives examples of where advanced generics are useful • Can be found in the back of the Sun Lab

  50. Tips for Tac I Questions?

More Related