Programming physics games with Python and OpenGL

4,523 views

Published on

Published in: Technology, Art & Photos
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,523
On SlideShare
0
From Embeds
0
Number of Embeds
353
Actions
Shares
0
Downloads
44
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide
  • Very fast moving objects (eg. bullets) may have passed completely through each other in a single time step – compute time of first intersection
  • Programming physics games with Python and OpenGL

    1. 1. Programming Physics Games with OpenGL Daniel Pope ~ @lordmauve Code samples are available at https://bitbucket.org/lordmauve/physicsgames
    2. 2. Outline • Introduction to OpenGL • Introduction to physics engines • Connecting the two • A bit more about physics engines • Tips on writing games
    3. 3. What is OpenGL? • Low-level 2D and 3D graphics library • Usually hardware accelerated • Can only draw points, lines, triangles • But triangles can be textured • GPUs can evaluate a short program for each vertex or fragment in a high-level language
    4. 4. What is a physics engine? Given a model of the world at time t Apply physical laws to compute a model of the world at time t + ∆t (for small ∆t)
    5. 5. (demo 1)
    6. 6. PyBox2D http://pybox2d.googlecode.com http://www.pymunk.org
    7. 7. PyODE http://pyode.sourceforge.net/ PyNewt http://code.google.com/p/pynewt/ PyBullet https://launchpad.net/pybullet
    8. 8. OpenGL wrapper/scenegraph • Higher level API • Window Creation • Texture Loading • Sprites/Texture Atlases • Model Loading/Rendering/Animation • PVS – Potentially Visible Set calculation • Graphics Algorithms
    9. 9. pyglet cocos2d kivy http://www.pyglet.org/ http://cocos2d.org/ http://kivy.org/
    10. 10. Panda3D http://www.panda3d.org/ Python-Ogre http://www.python-ogre.org/ PyCrystal http://www.crystalspace3d.org/main/PyCrystal
    11. 11. apt-get install python-box2d apt-get install python-pyglet (pyBox2D == 2.0.2ish)
    12. 12. What a game does while True: dt = wait_one_frame() process_input() update_world(dt) draw_world()
    13. 13. (demo 2)
    14. 14. A physics simulation • Newton's Laws • Collisions • Constraints (eg. Joints)
    15. 15. Newtonian Quantities Mass/Inertia Force Position Velocity Acceleration Momentum Impulse
    16. 16. Rotational Equivalents Linear Angular Mass/Inertia Moment of Inertia Force Moment/Torque Position Angle Velocity Angular Velocity Acceleration Angular Acceleration Momentum Angular Momentum Impulse Angular Impulse
    17. 17. Approximately net_force = sum(forces) acceleration = force / mass velocity += acceleration * dt position += velocity * dt forces = [GRAVITY * mass] + rotational equivalents Time step
    18. 18. Collisions • Detect when two shapes intersect • Broad phase (O(n log n)?, approximate) • Narrow phase (O(n²)) • Exchange momentum • Move them apart, keep them apart
    19. 19. Shapes are convex polygons/polyhedra ✔✗ ✔
    20. 20. Separating Axis Theorem Given two convex shapes, it is possible to draw a straight line between them if and only if the two do not intersect.
    21. 21. Bodies Collections of shapes Compute centre of mass Compute moment of inertia
    22. 22. Creating bodies
    23. 23. Linking physical objects with OpenGL • Draw sprites • Create physical models corresponding to sprites • Instantiate objects into physics simulation Each frame: • Step time • Read position + angle of body • Draw OpenGL quads/sprites at position + angle
    24. 24. (0, 1) (0, 0) (1, 0) (1, 1) (0, 1) (0, 0) (1, 0) (1, 1) Texture Coordinates
    25. 25. Drawing textured quads
    26. 26. (demo 3)
    27. 27. Collision Callbacks What happens in game when shapes collide? Register a callback to take action • Destroy/change the object • Create a joint • Play a sound
    28. 28. tanks demo
    29. 29. Joints • pin joint – bodies attached at a point, can rotate • prismatic joint – keep bodies aligned, like a piston or a train on a rail • distance joint – keep points on the two bodies at the same distance from one another, like a tow-bar or crank shaft • pulley joint – the position of a body along one axis is linked to the position of another body along another axis
    30. 30. Pin Joint
    31. 31. Joint motors/limits • Motors apply torques or forces between the bodies • Joints can have limits – your knees do not bend backwards
    32. 32. truck demo heli demo
    33. 33. Features I've not yet mentioned • Collision groups/masks • Sensors • Non-rotating bodies/Infinite moment of inertia • Introspecting the world • Sleeping
    34. 34. Complications I've skipped over • Constraint solving • Overconstraint • Stability
    35. 35. Graphics and Sound • Cute and colourful is attractive • Simple shapes and outlines are much easier than other styles • Free/CC game art resources available
    36. 36. The art of tuning a physics engine • Tuning magic numbers: • Forces • Torques • Density/Friction/Restitution • Re-shaping bodies/redrawing sprites • At which point does a force act? • Joints/joint limits/joint motors • Collision groups/filters
    37. 37. “To every action there is always an equal and opposite reaction: or the forces of two bodies on each other are always equal and are directed in opposite directions.” - Archimedes
    38. 38. Compound effects • Buoyancy • Lift • Explosions • Soft bodies • Drag effects • Joints or objects 'snapping'
    39. 39. Physics Games • Intuitive for players • The game is mastering the physics • Constrain degrees of freedom to balance difficulty/reward/punishment
    40. 40. Physics Puzzles Overlapping constraints • Physical constraints • Thing in the way • Thing not in the right place • How to overcome gravity • Wrong physical behaviour unless... • Non-physical constraints • Player can't breathe underwater • Only x can pass through this barrier
    41. 41. Reasons to use a physics engine • Tons of maths for free • Cool effects/cheap animations • Game world responds to player; player is empowered/immersed • Extra layer of complexity/interest for the player • Intuitive puzzles
    42. 42. Reasons not to use a physics engine • Extra dependency/pain to install • Time consuming to tune • Too many degrees of freedom may make some mechanics difficult or impossible to work in
    43. 43. Daniel Pope @lordmauve Code samples are available at https://bitbucket.org/lordmauve/physicsgames

    ×