CSE 390 – Computer Game Programming Physics Modeling
Live Billiards, by Terra Game
Internship Annoucement • SONY/BMG wants more Stony Brook game development interns – Apply ASAP for Summer 07 & Fall 07 – Send resumes to
[email protected] – Subject: Game Development – Can be done for course credit as part of game specialization • Lots of other internship opportunities as well – Ex: Three Rings (see http://www.threerings.net/jobs/index.html) – Lots of others listed in Yahoo group database
Project Announcement • If you are interested in a games-related research project, please let me know – can fulfill research requirement for game programming specialization
• Project work will start this summer • In this project, a team of students will develop a serious game using the Torque Game Engine – more information for those who participate at organizational meeting at end of semester
Unreal Engine 3.0 & Physics http://www.unrealtechnology.com/html/technology/ue30.shtml
• Rigid body physics system supporting player interaction with physical game object, ragdoll character animation, complex vehicles, and dismemberable objects. • All renderable materials have physical properties such as friction. • Physics-driven sound. • Fully integrated support for physics-based vehicles, including player control, AI, and networking. • Visual physics modeling tool built into UnrealEd, supporting creation of optimized collision primitives for models and skeletal animated meshes; constraint editing; and interactive physics simulation and tweaking ineditor."
Physics & Hardware • Fascinating & realistic physics in games is currently a reality • Ex: Chronic Logic’s Gish – – – – –
http://www.chroniclogic.com/?gish.htm 2005 IGF Grand prize winner 2D side scroller, main character: a ball of tar Fascinating & realistic physics used in game Good gameplay
• True physics behavior is impossible – physics don’t follow a frames per second schedule – physics can be very demanding on your processor – Soon you’ll play with physics cards • http://www.ageia.com/technology.html • PhysX, a Physics Processing Unit (PPU)
Ageia’s PhysX Processor Architecture • Has been designed to enable radical acceleration of: – – – – – – –
Rigid body dynamics Universal collision detection Finite element analysis Soft body dynamics Fluid dynamics Hair simulation Clothing simulation
• These are very expensive operations computationally (fluid, hair, etc …) • Ageia’s NovodeX Physics SDK – An asynchronous physics-modeling API that allows for the use of software-only and/or hardware-accelerated features in games
Fundamental Laws of Physics • •
Assumption: we’re talking Newtonian physics, not quantum or Star Trek physics Review of basics terms: – – –
Mass (m): a measurement of how much matter an object is made of in kilograms (not weight) Time (t): in our games, a frame represents a virtual unit of time, in advanced 3D games, real time is used Position (s): location of an object in space (where on object?) • •
–
Velocity (v): instantaneous rate of speed of an object • •
–
Physicists use an object’s center of gravity Most game programmers use the center of a bounding box ds/dt For us, this translates to pixels/frame
Acceleration (a): rate of change of velocity •
dv/dt
Common Calculations • New Position with constant velocity: xt = x0 + (v * t) • New Velocity (vt) with Acceleration: vt = v0 + (a * t) • New Position (xt) with Acceleration: xt = x0 + (v0 * t) + (a * t2 / 2) •
NOTE – each of these calculations are in one dimension only How would you handle movement in multiple dimensions?
• –
You would perform similar calculations on y & z axes
Force • Force (F): – F = m * a, measured in kg * m/s2, or just N (Newtons)
• Force in games: – Apply artificial forces like explosions to an object & compute the resulting acceleration (a = F/m) – Two objects collide & you wish to compute the forces on each other – A game weapon has a certain force, but can fire different virtual mass shells (thus different accelerations)
• If multiple forces are acting on an object, they may be summed for a total force fx = f1x + f2x + f3x
Momentum • (P) – A property that objects in motion have – P = m * v, measured in kg * m/s
• Force equation can be reduced to: – F = dP/dt
• Momentum transfer – if 2 objects collide: – A perfectly elastic collision: a ball hits a wall with velocity vi & bounces off with the same velocity • Momentum is conserved
– An imperfect elastic collision: some energy is converted into heat, work, & deformation of objects • Momentum is reduced after collision
Two blocks colliding head-on • Block one before collision: ma & vai • Block two before collision: mb & vbi • If momentum is conserved after collision: (ma * vai) + (mb * vbi) = (ma * vaf) + (mb * vbf) • Problems – 2 unknowns (vaf & vbf) – Need a second equation (Kinetic energy equation) – ke = (m * v2)/2, where ke is never negative Joules (J) – Insert the ke equation into our momentum equation
vaf = ((2 * mb * vbi) + vai * (ma – mb))/(ma + mb)
Modeling Gravity Effects •
Gravity is a common game effect, two cases: – –
Two or more objects with relatively the same mass Two objects where the mass of one object is much greater than the other
• Gravitational force between any two objects: F = G * m1 * m2 / r2 G = gravitational constant, 6.67x10-11 N*m2*kg-2 • Example, 70 kg person standing on earth: F = 6.67x10-11 * 70kg * 5.98x1024kg/(6.38x106m)2 = 685.93N Flbs = 685.93N/(4.45N/lb) = 155 lbs • Again, Force (F) can be used to calculate acceleration
Modeling a Gravity Well •
Using gravity effects, we can model a ship flying near a black hole in a space game 1. Make up a mass for the ship 2. Make up a mass for the black hole that’s much larger than the ship 3. Make up your own G that matches your desired game-play effect 4. Figure out the Force & convert it to acceleration (m/a) 5. Include this acceleration in calculating the ship’s velocity –
will be greater as the ship gets closer to the black hole
Modeling objects falling from the sky • Assumption – Acceleration on an object due to gravity is 9.8 m/s2 – Use whatever you want to improve game-play • results will vary depending on scale of game & game objects
• Velocity of the object at a given time: v(t) = v0 + (9.8 m/s2) * t • Position of the object at a given time: y(t) = y0 + (v0 * t) + ((9.8 m/s2) * t2 / 2)
Example: Ball falling from top of screen int y_pos = 0, y_velocity = 0, gravity = 9; while (y_pos < SCREEN_BOTTOM) { y_pos += y_velocity; // DRAW BALL at y_pos y_velocity += gravity; }
Example: Ball falling with curved trajectory int
x_pos y_pos x_velocity y_velocity gravity
= = = = =
0, 0, 2, 0, 9;
while (y_pos < SCREEN_BOTTOM) { x_pos += x_velocity; y_pos += y_velocity; // DRAW BALL at y_pos y_velocity += gravity; }
Modeling Projectile Trajectories • Problem assumptions: – – – –
Ground plane at y = 0 Tank located at (0, 0) Tank barrel pointed at angle of inclination theta (θ) Tank shoots shell at velocity v Vix = v * cos θ Viy = v * sin θ
• To calculate the position at any time t: vy(t) = viy – ((9.8 m/s2) * t) y(t) = y0 + (v0 * t) + ((9.8 m/s2) * t2 / 2) x(t) = vix * t
• When will it hit the ground & how far will it go? t = (viy * (sin θ) /a) * 2
Trajectory Example float x_pos = 0, y_pos = SCREEN_BOTTOM, x_vel = 0, y_vel = 0, gravity = 9.8, velocity = INIT_SHELL_VEL, angle = INIT_ANGLE; x_vel = velocity * cos(angle); y_vel = velocity * sin(angle); while (y_pos < SCREEN_BOTTOM) { x_pos += x_vel; y_pos += y_vel; // DRAW SHELL OBJECT y_vel += gravity; x_vel -= WIND_FACTOR; }
Basic Collision Response • Video games have traditionally use simplified elastic collisions – this is rapidly changing
• Simple x,y bounce physics – Ex: balls bouncing off sides of pool table – When a ball hits one of the sides, it reflects off the side at an angle equal & opposite to its initial trajectory – Trigonometric calculations are expensive – Trick: Walls are EAST, WEST, NORTH, & SOUTH • Program velocity to be opposite direction depending on which wall is hit • Reduce velocity slightly after each collision • If EAST wall is hit, reverse x velocity, etc …
– Friction – after initial velocity, use an acceleration with negative value to gradually slow down ball
Other Interesting Game Physics • Computing the Collision Response with Planes of any Orientation • Simple Kinematics – The mechanics of moving linked chains of rigid bodies – Useful for 3D Animation with wireframe objects
• Particle Systems – Physics models that simulate small particles – Good for explosions, vapor trails, sand storms, etc …
Constructing Physics Models for Games • Physics engine might keep track of: – Position & velocity of objects – Angular velocity of objects – Mass, frictional coefficient, & other physical properties of objects – Physics engine geometry for objects – External universal forces info such as wind, gravity, …
• Calculations might be generated once per frame to perform all physics modeling – of course, soon physics cards will help do these things asynchronously