Presentation is loading. Please wait.

Presentation is loading. Please wait.

L ECTURE 10 Announcements. Final 1 Feedback Almost completely done with your 2D game engine! – Congratulations! – Feel free to use it/improve after the.

Similar presentations

Presentation on theme: "L ECTURE 10 Announcements. Final 1 Feedback Almost completely done with your 2D game engine! – Congratulations! – Feel free to use it/improve after the."— Presentation transcript:

1 L ECTURE 10 Announcements

2 Final 1 Feedback Almost completely done with your 2D game engine! – Congratulations! – Feel free to use it/improve after the class is over (some of us have/still are) Time to start showing off your product – ~2.5 weeks of gameplay coding! – Content creation! – Tons of playtesting! More on public playtesting later…

3 QUESTIONS? Announcements

4 L ECTURE 10 Sound


6 Sound in Games In the real world, computers have sound Background music Sound effects Can be an important part of gameplay – Listening for footsteps – Dramatic music

7 Sound File Formats Many ways to encode and store sound Open standards – Ogg Vorbis – FLAC Closed standards – mp3 – m4a – wav

8 Sampled Audio mp3, wav, and most other familiar extensions Usually recordings of live sounds Samples of sound wave at regular intervals Prevalent in modern games Refers to data type, not origin – Touchtone telephone is generated but still sampled

9 Generated Audio MIDI File provides information on instruments and notes – Similar to sheet music Sound cards translate from instruments/notes to sound Can instruct computer to play something even if you can’t play it Used to be popular to save space, not as common now

10 Compressed vs. Uncompressed Compressed Sound Files Lossy or Lossless? – Lossy remove “least important” parts of sound wave – Lossless just use smart compression on raw wave Smaller file size (esp. lossy) Lossy is lower quality Slower to decode and play Often used for music Uncompressed Sound Files Record as much as possible of sound wave Much larger file size Usually high quality Faster to decode and play Often used for sound effects

11 Buffering Decompressing and decoding is slow Read sound into buffer, play back from buffer Size of buffer depends on speed of system Playback delay while buffer is filled Buffer Sound file Sound device Decoding


13 javax.sound.sampled AudioSystem : Provides factory methods for loading audio sources Clip : Any audio that can be loaded prior to playback Line : Any source of streaming audio DataLine : An implementation of Line with helpful media functionality (start, stop, drain, etc) Other classes for mixing, ports, and other utilities File file = new File(“mysound.wav”); InputStream in = new BufferedInputStream( new FileInputStream(myFile) ); AudioInputStream stream = AudioSystem.getAudioInputStream(in); Clip clip = AudioSystem.getClip();; clip.start();

14 javax.sound.midi MidiSystem : The AudioSystem for MIDI files Sequencer : Plays MIDI sounds Other classes for manipulation of instruments, notes, and soundbanks – So you can create MIDI sounds in realtime – Much harder to manipulate samples Sequence song = MidiSystem.getSequence(new File(“mysong.mid”)); Sequencer midiPlayer = MidiSystem.getSequencer();; midiPlayer.setSequence(song); midiPlayer.setLoopCount(0); midiPlayer.start();

15 Alternatives? Some drawbacks of the built-in sound classes… – Imprecise control over exact playback start/stop positions – Almost impossible to manipulate or even examine samples in realtime – While Java offers pan and reverb, other libraries offer more varied effects But it’s very effective for simple background music and sfx!

16 OpenAL Cross-platform audio API modeled after OpenGL Pros: – Built for positional sound (distance attenuation, Doppler shift, etc all built in) – More fine-grain control available Cons: – Single listener model – Modeled on OpenGL

17 Others Most other libraries are platform-specific or wrappers for OpenAL …except for synthesis libraries! – Jsyn, Beads, etc – Useful for composer programs and the like, not so much for sound playback


19 L ECTURE 10 Collision Detection III

20 Point of Collision We want to find out where shapes hit In our simulation, colliding shapes are intersecting – Generally the intersection is small – So we choose a point that approximately represents where the intersection is

21 Poly-Poly When two polygons (AABs are polygons too!) collide, at least one vertex of one shape is inside the other (almost always) – If there’s only one point, use that as the point of collision – If there’s more than one, average them!

22 Circle-Circle

23 Circle-Poly

24 QUESTIONS? Collision Detection III

25 L ECTURE 9 Physics III


27 Rotation We currently have shapes that don’t rotate First step is to be able to rotate shapes Next step is to provide collision response for rotating entities

28 Terminology θ ω

29 Basics Your physical entities should have an angle, angular velocity, and angular acceleration You should integrate these as before But whenever you do this you have to physically rotate the shape public class PhysicalEntity{ float angle, aVel, aAcc; void move(float time) { //integrate position aVel += aAcc*time; angle += aVel*time; aAcc = 0; rotate(aVel*time); } }

30 Rotating Shapes What shapes do we need to rotate? AAB doesn’t rotate, by definition Circles are circles – You still need angular values for the circle though, what if a hitbox is a circle? Therefore only polygons need to rotate Rotate polygons by rotating their vertices

31 Centroid of Polygon

32 Rotating Polygons θ

33 Inertia

34 QUESTIONS? Rotation


36 Impulse and Forces How can we cause shapes to rotate in the world? Currently we are applying impulse/forces the centroids of entities Apply impulse/force to object, but not at centroid

37 Impulse and Forces

38 Angular Impulse and Forces

39 Collision Response We need to change the impulse we calculated in Physics II It’s now a different value that is applied at some specific point – It’s applied to the point of collision!

40 Some Definitions

41 Collision Response

42 Fixed Rotation

43 QUESTIONS? Rotational Physics


45 Friction

46 Frictional Force a b a b

47 Relative Velocity

48 How Much Force? From physics, the friction force on object A due to object B is proportional to the force exerted on object A by object B We don’t really have that force… – But we did apply impulse to the objects!

49 The Force

50 Disclaimer This friction works for the case when the relative velocity is linear With rotation, things become much more difficult If you want to combine these, good luck! ω

51 QUESTIONS? Friction

52 L ECTURE 10 Spatial Acceleration Structures

53 Collisions aren’t cheap

54 Can we do better? We can tell that some collisions don’t even need to be checked Spatial acceleration data structures reduce # of collisions by taking advantage of spatial locality – Use as a replacement for your Set/List – Can even implement Java Collection !

55 Bounding boxes Every shape has a bounding box – min/max x/y values of any point in the shape Function of the shape: – AAB – itself – Circle – center ± (r,r) – Poly – min/max over all vertices – Compound – min/max over all subshapes

56 Quadtrees Like a binary tree, but 4 children Divide world into quadrants – Recursively subdivide quadrants based on # of entities in a quadrant – Only try colliding with entities in your quadrant Demo: 21/javascript-quadtree- implementation/ 21/javascript-quadtree- implementation/

57 Quadtrees - insert Starting at the root node: – If this node isn’t split yet, add the entity here If adding the entity results in X entities, split and re-insert everything in the split quadrant – Else, find the quadrant that should bound the entity’s bounding box If one does, recur down that node If none do, add the entity to this node

58 Quadtrees - retrieve Gets all the entities for possible collisions Starting at the root node: with an accumulator – Add all entities in this node to accumulator – Again, determine which quadrant this entity should reside in If none, recur down all children – any entity could be valid! Else, recur down that child quadrant

59 Quadtrees – pros/cons Pros – Fairly easy to implement – Good at reducing # of collisions Cons – Only works with bounded worlds – Assumes objects are uniformly distributed

60 Can we do even better? What if objects are highly concentrated (non-uniformly distributed)? What if the world is unbounded? Where to mark quadrants?

61 KD-trees K-dimensional trees (in our case, k = 2) Traversing to children is subdividing by either x or y – At each subdivision, choose the optimal x or y value to subdivide by – Efficiently splits up entities into buckets

62 Choosing the split axis



65 KD-trees – add/retrieve Implementation details are the same as quadtrees, except for splitting/traversal – Splitting – each node is an x or y node, subdivide based on coordinate at mean/median/etc.. – Traversal – traverse based on x or y value – left is less than, right is greater than Subdivision may involve sorting – how to optimize # of sorts required?

66 KD-trees – pros/cons Pros – Takes entity distribution into account – Great at reducing # of collisions Cons – Trickier to implement – Slightly higher construction time

67 Using KD/quad trees On each tick: – Rebuild the tree (clear and then re-insert all entities) – Use retrieve(PhysicsEntity) to get entities to attempt collision with for each entity – Run standard collision detection algorithm – ??? – Profit!

68 QUESTIONS? Spatial Acceleration Structures

69 L ECTURE 10 Tips for Final 2


71 Finding Playtesters CS students are easy targets, try the sun lab or MS lab – Ask nicely – Don’t ask busy people Keep your audience in mind, however – It probably isn’t all CS students

72 Don’t Interrupt! Be a fly on the wall – Say as little as possible – Don’t offer hints or instructions Your game should speak for itself – You won’t be there to offer instructions when your game is released

73 When to Interrupt Player is frustrated or taking too long in a single area You’re no longer getting good feedback If the player moves on, you will resume getting good feedback

74 Keep a Log What is the player getting stuck on? – Make it smoother What is the player enjoying? – Emphasize it What is the player ignoring? – Take it out if it’s unnecessary Consider having the game keep an automated log – Analytics is wonderful!

75 QUESTIONS? Tips for Final 2

76 JAVA TIP OF THE WEEK Tips for Final 2

77 Default Visibility You already know public, private, protected What happens when there’s no visibility modifier? Then the field/method is visible to anything in the same package

78 A Helpful Table Own ClassSame Package SubclassAll Others public Visible protected Visible No (default)Visible No private VisibleNo

79 Some General Visibility Tips Use private wherever possible – “Encapsulation” If you have a large amount of protected methods you want truly protected, consider separating them into an interface

80 QUESTIONS? Tips for Final 2

81 N O P LAYTESTING ? Playtest with random people this week!

Download ppt "L ECTURE 10 Announcements. Final 1 Feedback Almost completely done with your 2D game engine! – Congratulations! – Feel free to use it/improve after the."

Similar presentations

Ads by Google