1 / 81

Lecture 10

Lecture 10. Announcements. 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!

Télécharger la présentation

Lecture 10

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 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. Announcements Questions?

  4. Lecture 10 Sound

  5. Sound Sound Applications

  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 • OggVorbis • 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 1100100110101011011101011001000110101

  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 Uncompressed Sound Files Record as much as possible of sound wave Much larger file size Usually high quality Faster to decodeand play Often used for sound effects • 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

  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 Sound device Buffer Decoding Sound file

  12. Sound Sound Implementation

  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 Filefile = new File(“mysound.wav”); InputStreamin = new BufferedInputStream( new FileInputStream(myFile) ); AudioInputStreamstream = AudioSystem .getAudioInputStream(in); Clipclip = AudioSystem.getClip(); clip.open(stream); clip.start();

  14. javax.sound.midi • MidiSystem: The AudioSystemfor 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”)); SequencermidiPlayer = MidiSystem.getSequencer(); midiPlayer.open(); 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

  18. Sound Questions?

  19. Lecture 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 • Circle-Circle is easy: • It’s on the line connecting the centers, with ratio of the radii • +() • Remember this is in world (absolute) coordinates

  23. Circle-Poly • If vertices of the poly are within the circle, then average them • If not, then take the point along the MTV: • (Depends on MTV direction)

  24. Collision Detection III Questions?

  25. Lecture 9 Physics III

  26. Physics III Rotation

  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 ω • Let’s define some things: • Angle, θ (CCW) • Angular velocity, ω • Angular acceleration, α • Moment of Inertia, • Analogous to mass (inertia) for rotation θ

  29. Basics public class PhysicalEntity{ float angle, aVel, aAcc; void move(float time) { //integrate position aVel += aAcc*time; angle += aVel*time; aAcc = 0; rotate(aVel*time); } } • 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

  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 • Every shape rotates around its centroid • The centroid of a polygon with vertices is: • Where • and are coordinates of vertices in CCW order

  32. Rotating Polygons • To rotate a polygon, rotate each vertex by the angle • These vectors are the vertices relative to the centroid! • Remember to update edges as well θ

  33. Inertia • We also need the moment of inertia of an object • You can define or calculate it • Circle: • Polygon:

  34. Rotation Questions?

  35. Physics III Rotational Physics

  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 • Now your impulses and forces have a magnitude and a point of application • is relative to the centroid • The magnitude is actually a vector • for impulse and for force from now on or

  38. Angular Impulse and Forces • In relation with angular velocity and acceleration: or

  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 • More definitions: • , are the vectors from the centroids of the shapes to the collision point • , are the perpendiculars to , • is the normalized MTV

  41. Collision Response • Magnitude of the impulse • , are projections of velocities onto the • The impulse is in the direction of , determine the sign based on your MTV direction

  42. Fixed Rotation • Just like with static shapes, there should also be shapes that don’t rotate • Just like with the previous impulse equation, have a special case for non-rotating objects • Replace with if the entity doesn’t rotate • Note that if both objects don’t rotate, the equation reduces to the old equation

  43. Rotational Physics Questions?

  44. Physics III Friction

  45. Friction • We don’t want everything to be slippery • Friction slows things down • Give every physical entity a friction value greater than 0

  46. Frictional Force • The frictional force is parallel to the surface of contact • i.e. perpendicular to MTV • The direction is determined by the direction of the relative velocity (1D): a b a b

  47. Relative Velocity • Only velocity perpendicular to the MTV is relevant • Direction of the perpendicular () doesn’t matter • Consistency matters

  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 • So we have • is the impulse applied in collision response • is a constant

  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!

More Related