630 likes | 694 Views
Lecture 9. Announcements. Congrats!. You are done with all the assignments in this class! 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 Remember, you should be mostly finishing your engine this week
E N D
Lecture 9 Announcements
Congrats! • You are done with all the assignments in this class! • 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 • Remember, you should be mostly finishing your engine this week • Don’t forget to set up a meeting with your mentor TA soon
Announcements Questions?
Lecture 9 Collision Detection III
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 • 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
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)
Collision Detection III Questions?
Lecture 9 Physics III
Physics III Rotation
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 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
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 the 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 θ
Inertia • We also need the moment of inertia of an object • You can define or calculate it • Circle: • Polygon:
Rotation Questions?
Physics III Rotational Physics
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
Angular Impulse and Forces • In relation with angular velocity and acceleration: 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
Rotational Physics Questions?
Physics III Friction
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!
Friction Questions?
Lecture 9 Networking
Networking NETWORKING STRATEGIES
The Illusion • All players are playing in realtime on the same machine • But of course this isn’t possible • We need to emulate this as much as possible
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
Send Commands • 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
Client-Server Model • One player is the authoritative 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
Client-side Prediction • Client responds to player input immediately • When the server sends back the authoritative game state, client state is overwritten
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 • 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
Networking Strategies Questions?
Networking IMPLEMENTATION
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)