L ECTURE 9 Announcements. Initial M3 Feedback These all look really good! Lots of creative ideas this week You’re now done with all the standard projects.

Slides:



Advertisements
Similar presentations
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 6 Object Oriented Programming in Java Language Basics Objects.
Advertisements

Network synchronization of Online Games Li, Zetan.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Rasterization and Ray Tracing in Real-Time Applications (Games) Andrew Graff.
Cosc 5/4730 Game Design. A short game design primer. A game or animation is built on an animation loop. – Instance variables of “objects” are updated.
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
CS 300 – Lecture 20 Intro to Computer Architecture / Assembly Language Caches.
Online Chess Project 3 Due date: April 17 th. Introduction Third in series of three projects This project focuses on adding online support –2 players.
CS 201 Functions Debzani Deb.
© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology 1 Let’s get started. Let’s start by selecting an architecture from among.
Code Generation CS 480. Can be complex To do a good job of teaching about code generation I could easily spend ten weeks But, don’t have ten weeks, so.
AGD: 5. Game Arch.1 Objective o to discuss some of the main game architecture elements, rendering, and the game loop Animation and Games Development.
SE320: Introduction to Computer Games Week 8: Game Programming Gazihan Alankus.
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
TCP Sockets Reliable Communication. TCP As mentioned before, TCP sits on top of other layers (IP, hardware) and implements Reliability In-order delivery.
L ECTURE 9 Announcements. Initial M3 Feedback These all looks really good! Lots of creative ideas this week You’re now done with all the standard projects.
Quoridor Classic Game Manager Kevin Dickerson April 2004.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Chapter 7 Designing Classes. Class Design When we are developing a piece of software, we want to design the software We don’t want to just sit down and.
CSE 381 – Advanced Game Programming 3D Game Architecture.
Lecture 22 Miscellaneous Topics 4 + Memory Allocation.
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
Introduction to Networked Graphics Part 3 of 5: Latency.
CS 153 Design of Operating Systems Spring 2015 Lecture 17: Paging.
I. Basic Network Concepts. I.1 Networks Network Node Address Packet Protocol.
07 Coding Conventions. 2 Demonstrate Developing Local Variables Describe Separating Public and Private Members during Declaration Explore Using System.exit.
Outline Overview Video Format Conversion Connection with An authentication Streaming media Transferring media.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
1 The Internet and Networked Multimedia. 2 Layering  Internet protocols are designed to work in layers, with each layer building on the facilities provided.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts.
به نام خدا تنظیم کننده : فرانه حدادی استاد : مهندس زمانیان تابستان 92.
Problem of the Day  Why are manhole covers round?
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
1 Chapter 28 Networking. 2 Objectives F To comprehend socket-based communication in Java (§28.2). F To understand client/server computing (§28.2). F To.
The Alternative Larry Moore. 5 Nodes and Variant Input File Sizes Hadoop Alternative.
L ECTURE 8 Announcements. 3pm in the CIT lobby – We’ll be contrib- combing! Sign up to get a shirt at
Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.
L ECTURE 6 Announcements. Next week will be great! Three major things are happening next week: – Zynga guest lecture – Your game pitches (more details.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Games Development Game Architecture: Entities CO2301 Games Development 1 Week 22.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Object Oriented Analysis & Design Game Patterns. Contents  What patterns are  Delegation  Game Loop  Scene Graph  Double Buffering  Component 
Warlords Patrick Levoshko SE 558 – Multiplayer Game Design.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0.
Creating a GUI Class An example of class design using inheritance and interfaces.
Chapter 8-Exception Handling/ Robust Programming.
More Patterns CS 124. More Basic Patterns Patterns you’ve already seen (without knowing it) Observer / Listener Wrapper Composite Decorator / Filter Patterns.
CSCI 156: Lab 11 Paging. Our Simple Architecture Logical memory space for a process consists of 16 pages of 4k bytes each. Your program thinks it has.
11 Adding a Bread Bat Session Session Overview  We have created a cheese sprite that bounces around the display  We now need to create a bread.
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
COMP091 – Operating Systems 1 Memory Management. Memory Management Terms Physical address –Actual address as seen by memory unit Logical address –Address.
The Stingray Example Program CMT3311. Stingray - an example 2D game May be useful as a simple case study Most 2D games need to solve generic problems.
COMP 175 | COMPUTER GRAPHICS Remco Chang1/XX13 – GLSL Lecture 13: OpenGL Shading Language (GLSL) COMP 175: Computer Graphics April 12, 2016.
@Yuan Xue Worm Attack Yuan Xue Fall 2012.
Monitoring Dynamic IOC Installations Using the alive Record Dohn Arms Beamline Controls & Data Acquisition Group Advanced Photon Source.
Lecture 9 Announcements.
Chapter 5 Conclusion CIS 61.
Deferred Lighting.
Lecture 8 Announcements.
I. Basic Network Concepts
Operating Systems Chapter 5: Input/Output Management
Game Loop Update & Draw.
Lecture 9 Announcements.
CS297 Graphics with Java and OpenGL
Exceptions and networking
Presentation transcript:

L ECTURE 9 Announcements

Initial M3 Feedback These all look really good! Lots of creative ideas this week You’re now done with all the standard projects Have fun playtesting!

Congrats! No more of our engine requirements! From now on everything you work on is designed by you Good work so far!

Final Coding Time to start coding for your final project This week you’ll be doing the bulk of your engine code Don’t forget to set up a meeting with your mentor TA soon – You probably already know your mentor TA, if not, check your

Don’t forget about old projects! Don’t forget, you need a working version of each milestone to get credit for the class …but focus on your final project

QUESTIONS? Announcements

About “Advanced Topics” These lectures *will not* give you all the information you need to implement your engine features – They are intended as a high-level overview You will be responsible for researching the topics yourself We will make it clear what the expectations are during final grading meetings

L ECTURE 9 Networking

NETWORKING STRATEGIES Networking

The Illusion All players are playing in real-time on the same machine But of course this isn’t possible We need to emulate this as much as possible

The Illusion What the player should see: – Consistent game state – Responsive controls – Difficult to cheat Things working against us: – Game state > bandwidth – Variable or high latency – Antagonistic users

Send the Entire World! Players take turns modifying the game world and pass it back and forth Works alright for turn-based games …but usually it’s bad – RTS: there are a million units – FPS: there are a million players – Fighter: timing is crucial

Modeling the World If we’re sending everything, we’re modeling the world as a uniform chunk – But it really isn’t! – Composed of entities, only some of which need input from a player We need a better model to solve these problems Player 1 My turn Player 2 Not my turn State 1 Read-only World World

Send Commands Model the world as local and shared data – Share player information, powerups, etc – Don’t need to share static level data Each player sends the other all actions that alter shared game world “Deterministic P2P Lockstep” Problem: everything must evaluate the same – Or else there are desyncs Problem: have to wait for all the other players’ commands – So everyone is limited by laggiest player Player 1 State 1 processP1Inputs() sendReceiveInputs() processP2Inputs() State 2 Player 2 State 1 sendReceiveInputs() processP1Inputs() processP2Inputs() State 2

Client-Server Model One player is the authoritative server – Now we don’t have to wait for slow players, just the server Other player is a “dumb terminal” – Sends all input to server – Server updates the world and sends it back Problem: client has to wait for server to respond to perform even basic actions Player 1 (server) State 1 processP1Inputs() processP2Inputs() sendState() State 2 Player 2 (client) State 1 sendInputs() receiveState() State 2

Client-side Prediction Client responds to player input immediately When the server sends back the authoritative game state, client state is overwritten Server State 1 processP1Inputs() processP2Inputs() sendState() State 2 Client State 1 sendInputs() processP2Inputs() receiveState() State 2

Rollback But the server just sent a state that was 100ms in the past! What if games have diverged since then? – For instance, both players think they’ve collected a single powerup We can’t just replace our game world or we’ll lose commands from the local player – Client has to roll back the world and integrate commands since the last known good state

Masking the Timewarp Problem: laggy players experience this jump often Solution: if the server usually sends states from 100ms ago, run the client 100ms behind Turns a jumpy experience into a smooth, only slightly slow one – Very useful if relative timing of commands is important

What about the server? Without rollback: – In an FPS, would need to lead shots because the server won’t register shot until after delay With rollback: – The target could be shot after they think they’ve taken cover – Or we could delay the server player as well… Need to think carefully about both technical requirements and game impacts of any networking model

QUESTIONS? Networking Strategies

IMPLEMENTATION Networking

TCP: Transmission Control Protocol Abstracts over IP All packets are guaranteed to be received and in the correct order Good for sending important, permanent data (websites, databases, etc)

UDP: User Datagram Protocol A very thin shell around IP Much faster than TCP, but no guarantees about reception or order Good for information where only the most recent state matters (streaming, etc)

TCP vs UDP (Very) generally: action games use UDP and turn-based games use TCP – World state updates can be lost without worry, commands not so much Can potentially combine them – TCP sends important data, UDP sends timely data Best choice varies by project – (for naïve version, TCP is fine)

Java Sockets Very good for most purposes Read and write objects to sockets UDP is deprecated for sockets; for UDP use DatagramSocket

Settings Up Sockets Open a connection on a port Open an input/output stream from the socket Read and write to the streams (which use the socket’s protocol) Close the streams and sockets String host = “ ”; int port = 10800; Socket out = new Socket(ip, port); ObjectOutputStream stream; stream = new ObjectOutputStream( out.getStream()); stream.writeObject(“HelloWorld”); stream.close(); out.close(); String host = “ ”; int port = 10800; Socket in = new Socket(ip, port); ObjectInputStream stream; stream = new ObjectInputStream( in.getStream()); System.out.println( stream.readObject()); stream.close(); in.close();

Edge Cases What if… – The client disconnects – The server dies – The client goes insane and sends gibberish – The client loses internet for 30 seconds – The client is malicious – The client changes IP address Handling errors well is vital to player experience

Elegant Disconnects Handle and respond to IO exceptions – Don’t just dump a stack trace Display informative status messages Send heartbeat packets every few seconds – Then respond if server/client hasn’t received a heartbeat in a while Never let the game continue to run in an unrecoverable state!

Java Serializable Need to represent an Object as data to send it over the network To enable serialization, just implement Serializable It’s an empty interface, so why isn’t everything serializable? – Not all objects are useful when serialized ( Thread etc) – Don’t serialize passwords! – Need to provide compatibility

serialVersionUID This is part of the compatibility contract What if… – You serialize a MyGameState – You change the type of a field in MyGameState – You load up the serialized data to the new class definition serialVersionUID is a way of detecting this problem – Serialization will die if the versions don’t match class MyGameState { int playerHealth; int playerX; int playerY; } class MyGameState { int playerHealth; float playerX; float playerY; }

3 rd party libraries that handle the try/catch nonsense of using sockets, connecting, disconnecting Usually have their own serializations Most have both UDP and TCP Usually have onConnect() onDisconnect() onReceived() callbacks Kryonet is one example Oh No Please Not Java Sockets!

To Library or Not to Library Pros: – Can simplify your code a lot – Typically handles serialization – Typically handles threading Cons: – You have less control over the networking model (typically forced into server-client) – You get 2/3 points Recommended library: Kryonet

QUESTIONS? Networking

L ECTURE 9 Open GL

What is OpenGL? Not Swing – java.awt.swing is what you have been using to draw your shapes/sprites – Instead of drawing shapes, you pass vertices to draw shapes – Loading images into memory is done by binding textures instead of storing BufferedImages OpenGL will replace all Swing components in your rendering engine – It’s much faster, and allows the use of shaders Native to C++ – Using libraries such as JOGL or LWJGL, we can use OpenGL in Java

Integrating into your existing engine Create a Graphics interface – loadTexture(String path) – drawTexture(String name, Vec2f posn, Vec2f dim, Vec2f imagePosn, Vec2f imageDim) – Shape drawing! drawRect(MyRectangle r) drawCircle(MyCircle c) Etc. Now your World and Screens can use a generic “Graphics” object – Game is agnostic of implementation – Game doesn’t need to know if Swing or OpenGL is being used Graphics SwingGraphicsGLGraphics

LWJGL Light Weight Java Game Library Wrapper library for OpenGL Gives you a window, event polling, access to drawing calls You can use OpenGL and an orthographic projection to draw your 2D game Gives you OpenGL as a set of static methods within static objects according to which version’s methods you want to use – Most functions that you will be interested in are in the static GL11 object, corresponding to OpenGL 1.1 – Closely mirrors what using OpenGL is actually like in C++

Last time on 1971… public class Application extends SwingFrontEnd { public void onTick(long nanos) public void onDraw(Graphics2D g) public void onKeyPressed(KeyEvent evt) // more device and event types... public void onMouseDragged(MouseEvent evt) } Your job is to replace SwingFrontEnd with your own implementation

The Loop. public void start() { try { Display.setDisplayMode(new DisplayMode(screenSize.x, screenSize.y)); Display.create(); } catch (LWJGLException e) { e.printStackTrace(); System.exit(0); } initGL(); // set up your OpenGL environment long lastTick = Sys.getTime() * / Sys.getTimerResolution(); while (!Display.isCloseRequested()) { pollInput(); // you will write this method to find out what events have happened, and call their methods if (Display.wasResized()) onResize(new Vec2i(Display.getWidth(), Display.getHeight()); long time = Sys.getTime() * / Sys.getTimerResolution(); long delta = time – lastTick; lastTick = time; onTick(time); // abstract method, overridden in Application onDraw(g); // abstract method, overridden in Application, g is a graphics object of your making GL11.glFinish(); // OpenGL is an object, GL11 is OpenGL 1.1, you can use whichever version you like Display.sync(60); // tries to stabilize your game at 60fps } Display.destroy(); AL.destroy(); System.exit(0); } // Display, DisplayMode, Sys, and AL are all objects given to you by LWJGL

L ECTURE 9 Advanced Graphics

Deferred Lighting Advanced rendering technique for pixel perfect lighting All lighting calculations are done in a shader – In 3D, this means recording the 3D screenspace vector of every pixel position on screen – In 2D, your game is orthographic, meaning that using the scale and translation of the viewport, we can easily convert from screen position to world coordinates Called deferred lighting because we draw a fully lit world, and then defer the lighting to a second drawing pass where we multiply each pixel by the light value (from 0-1) at that pixel – This second pass is done in a shader, which for our purposes is a small program that is applied to each pixel on screen and returns a color for that pixel

Deferred Lighting Overall strategy (simplified for 2D): – Draw the scene, storing the color in a texture. Use an OpenGL FrameBuffer for this – Draw all the lights as circles use a shader to combine the light value of the circle with the scene texture, thereby “revealing” the underlying scene GeometryLightsCombined

Deferred Lighting Things that the lighting shader will need to know about: – Scale and translation of the viewport – Radius, color, and position of the light being drawn – The texture that the world was rendered to

Particles What is a particle? – A particle is a tiny entity that is used in massive quantities to create a visually pleasing effect – Usually don’t affect the gameplay in any significant way – Commonly used for explosions

Particles What makes particles look good? Fade out or get smaller linearly over their lifespan – Once the particle is completely gone or transparent, it can be removed from the world Adding some kind of randomness to how they move – Starting position, velocity, acceleration, color, size, shape

Particles Particles are great But they are very slow if not done correctly Things that make them slow: – It’s a lot of information to tick – It’s a lot of information to draw – There are way too many of them to consider doing collision detection against each other

Particles Optimizations? Get ready. Reduce the amount of information in your particles – Vec2f position, Vec2f velocity, boolean alive – maybe some noise values to make them scatter – Less information to tick Don’t make your particles Entities – Keep them in a separate list so that you can tick and draw them all at once – If you are using OpenGL, then binding the particle texture once and then drawing all your particles without unbinding and rebinding the texture is a HUGE improvement Don’t collide them with each other – That’s a lot of math – If you must have your particles collide, have them collide only with entities, not with each other – This means they also don’t need a shape, so they take up less space Keep them in an array – This limits the number of particles you can have (which might be a good thing) – Keeps all the memory contiguous – Once you are trying to allocate more particles than you have room for, the oldest ones are kicked out first Tick them in your draw loop – But you said to never do that! – I know, sorry – but in this instance the speed gain of only having to iterate over them once is worth the terrible design

Parallax Used for foregrounds and backgrounds Each background is scaled by a little bit less, and then translated so that it is offset – Something that is scaled by.5 will translate half as much as something scaled by 1, making backgrounds that are farther back move slower

L ECTURE 10 Tips for Final 1

Plan Ahead Integrating engines can take time Implementing new engine features has to happen in ~2 weeks Make realistic expectations with your mentor TA Hold yourself to high standards – make something you’re proud of!

JAVA TIP OF THE WEEK Tips for Final 1

Constructors are Lame Superconstructors put restrictions on the order complex calculations can be performed Leads to duplicate code in constructors We’d also like to have initialization near the variable source public class MyClass { private int i; private String str; public MyClass(int i) { this.i = i; str = “I’m number ” + i; } public MyClass(OtherClass other) { // We can’t put code before this this(0); for (...) i += 1; str = “I’m number ” + i; } }

public class InitBlockExample { public static final String s; static { String temp; // complicated logic here s = temp; } Initializer blocks! Unlabeled blocks of code directly in the class body Initializer blocks solve problems with duplicated constructor code and allow initialization to be performed at the variable declaration Executed from top to bottom when the class is instantiated

Field initialization shorthand Field initialization is just shorthand for initializer blocks public class MyClass { private static int i = 12; private String str = “”; } public class MyClass { private static int i; static { i = 12; } private String str; { str = “”; }

Good uses Immutable final collections – Lists, maps, etc. Keeping complicated initialization code near field Debugging! public class GoodUses { static final Map m; static { Map t = /*…*/; // lots of puts here m = Collections.immutableMap(t); } int complicatedInit; { // complicated init code } GoodUses(int ap) {} GoodUses(int ap, String s) {} GoodUses() {} }

Other “Fun” Stuff When you specify a main class to run, the JVM: – Loads class via reflection – Calls main() via reflection Thus, static initializers are actually run before main() – Can System.exit(0) at the end of the static initializer to exit gracefully rather than crash with NoSuchMethodException Don’t ever do this public class Mainless { static { String s = “Look, ma! ”; s += “No main!”; System.out.println(s); System.exit(0); }

QUESTIONS? Tips for Final 1

G AME D ESIGN 8 Aesthetics

Remember MDA?

Extra Credits! Great video series on video games and game design Two episodes today: Graphics vs. Aesthetics: Aesthetics of Play:

What to Remember The MDA framework, that mechanics create dynamics which create aesthetics The designer (you) approaches from the mechanics side, but the player approaches from the aesthetics side Decide on your aesthetics early on, then work to bring them to life!

M3 P LAYTESTING ! Last class playtesting!