Upload
darrell-winchester
View
216
Download
3
Embed Size (px)
Citation preview
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…
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
Sound File Formats• Many ways to encode
and store sound• Open standards
– Ogg Vorbis– FLAC
• Closed standards– mp3– m4a– wav
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 sampled11001001101010110111010110010001
10101
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
Compressed vs. UncompressedCompressed 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
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
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.open(stream);clip.start();
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.open(); midiPlayer.setSequence(song);midiPlayer.setLoopCount(0);midiPlayer.start();
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!
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
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
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
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!
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
�⃗�1
�⃗�2
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)
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
Terminology• Let’s define some
things:• Angle, θ (CCW)• Angular velocity, ω• Angular acceleration, α• Moment of Inertia, – Analogous to mass
(inertia) for rotation
θω
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); }}
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
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
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
θ𝑣0′
𝑣0❑
Inertia• We also need the moment of inertia of an
object• You can define or calculate it• Circle: • Polygon:
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
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
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!
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
𝑟𝑎
𝑟𝑏
𝑟𝑏⊥
𝑟𝑎⊥
�̂�
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
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
Friction• We don’t want
everything to be slippery– Friction slows things
down
• Give every physical entity a friction value greater than 0
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
�⃗�𝑡𝑣
𝐹 𝑓𝑟𝑖𝑐𝑡𝑖𝑜𝑛
−𝐹 𝑓𝑟𝑖𝑐𝑡𝑖𝑜𝑛
Relative Velocity• Only velocity
perpendicular to the MTV is relevant
• Direction of the perpendicular () doesn’t matter– Consistency matters
�⃗�𝑎
�⃗�𝑏
�̂�⊥
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!
�⃗� 𝑛𝑜𝑟𝑚𝑎𝑙
𝑣
𝑘 �⃗�𝑛𝑜𝑟𝑚𝑎𝑙
The Force• So we have
• is the impulse applied in collision response
• is a constant
�⃗�𝑎
�⃗�𝑏
�̂�⊥
�⃗�
− �⃗�
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!
ω
𝑢𝑟𝑒𝑙=?�⃗�
Collisions aren’t cheap• An individual collision
calculation is cheap…• But number of collisions
calculated is O(n^2)– 2 objects = 1 calc– 3 objects = 3 calcs– …– n objects = calcs
• What if your world has 1,000 entities? 10,000? 1,000,000?
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<PhysicsEntity>– Can even implement Java
Collection<T>!
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
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: http://www.mikechambers.com/blog/2011/03/21/javascript-quadtree-implementation/
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
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
Quadtrees – pros/cons• Pros– Fairly easy to implement– Good at reducing # of collisions
• Cons– Only works with bounded worlds– Assumes objects are uniformly distributed
Can we do even better?• What if objects are
highly concentrated (non-uniformly distributed)?
• What if the world is unbounded? Where to mark quadrants?
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
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?
KD-trees – pros/cons• Pros– Takes entity distribution into account– Great at reducing # of collisions
• Cons– Trickier to implement– Slightly higher construction time
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!
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
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
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
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!
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
A Helpful Table
Own Class Same Package
Subclass All Others
public Visible Visible Visible Visible
protected Visible Visible Visible No
(default) Visible Visible No No
private Visible No No No
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