SlideShare una empresa de Scribd logo
1 de 56
Programming Physics Games with OpenGL
Daniel Pope ~ @lordmauve
Code samples are available at
https://bitbucket.org/lordmauve/physicsgames
Outline
• Introduction to OpenGL
• Introduction to physics engines
• Connecting the two
• A bit more about physics engines
• Tips on writing games
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
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)
(demo 1)
PyBox2D
http://pybox2d.googlecode.com
http://www.pymunk.org
PyODE
http://pyode.sourceforge.net/
PyNewt
http://code.google.com/p/pynewt/
PyBullet
https://launchpad.net/pybullet
OpenGL wrapper/scenegraph
• Higher level API
• Window Creation
• Texture Loading
• Sprites/Texture Atlases
• Model Loading/Rendering/Animation
• PVS – Potentially Visible Set calculation
• Graphics Algorithms
pyglet
cocos2d
kivy
http://www.pyglet.org/
http://cocos2d.org/
http://kivy.org/
Panda3D
http://www.panda3d.org/
Python-Ogre
http://www.python-ogre.org/
PyCrystal
http://www.crystalspace3d.org/main/PyCrystal
apt-get install python-box2d
apt-get install python-pyglet
(pyBox2D == 2.0.2ish)
What a game does
while True:
dt = wait_one_frame()
process_input()
update_world(dt)
draw_world()
(demo 2)
A physics simulation
• Newton's Laws
• Collisions
• Constraints (eg. Joints)
Newtonian Quantities
Mass/Inertia
Force
Position
Velocity
Acceleration
Momentum
Impulse
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
Approximately
net_force = sum(forces)
acceleration = force / mass
velocity += acceleration * dt
position += velocity * dt
forces = [GRAVITY * mass]
+ rotational equivalents
Time step
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
Shapes are convex polygons/polyhedra
✔✗ ✔
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.
Bodies
Collections of shapes
Compute centre of mass
Compute moment of inertia
Creating bodies
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
(0, 1)
(0, 0) (1, 0)
(1, 1)
(0, 1)
(0, 0)
(1, 0)
(1, 1)
Texture Coordinates
Drawing textured quads
(demo 3)
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
tanks demo
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
Pin Joint
Joint motors/limits
• Motors apply torques or forces between the bodies
• Joints can have limits – your knees do not bend
backwards
truck demo
heli demo
Features I've not yet mentioned
• Collision groups/masks
• Sensors
• Non-rotating bodies/Infinite moment of inertia
• Introspecting the world
• Sleeping
Complications I've skipped over
• Constraint solving
• Overconstraint
• Stability
Graphics and Sound
• Cute and colourful is attractive
• Simple shapes and outlines are much easier than
other styles
• Free/CC game art resources available
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
“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
Compound effects
• Buoyancy
• Lift
• Explosions
• Soft bodies
• Drag effects
• Joints or objects 'snapping'
Physics Games
• Intuitive for players
• The game is mastering the physics
• Constrain degrees of freedom to balance
difficulty/reward/punishment
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
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
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
Daniel Pope
@lordmauve
Code samples are available at
https://bitbucket.org/lordmauve/physicsgames

Más contenido relacionado

Similar a Programming physics games with Python and OpenGL

Physics Solutions for Innovative Game Design
Physics Solutions for Innovative Game DesignPhysics Solutions for Innovative Game Design
Physics Solutions for Innovative Game DesignJohn Wilker
 
High speed multibody dynamics _ sherman_Simbody _ 2006.ppt
High speed multibody dynamics _ sherman_Simbody _ 2006.pptHigh speed multibody dynamics _ sherman_Simbody _ 2006.ppt
High speed multibody dynamics _ sherman_Simbody _ 2006.pptMariaMarque
 
【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~
【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~
【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~Unity Technologies Japan K.K.
 
4-Game Physics (Game Design and Development)
4-Game Physics (Game Design and Development)4-Game Physics (Game Design and Development)
4-Game Physics (Game Design and Development)Hafiz Ammar Siddiqui
 
Ai architectureand designpatternsgdc2009
Ai architectureand designpatternsgdc2009Ai architectureand designpatternsgdc2009
Ai architectureand designpatternsgdc2009SinisterM
 
From Experimentation to Production: The Future of WebGL
From Experimentation to Production: The Future of WebGLFrom Experimentation to Production: The Future of WebGL
From Experimentation to Production: The Future of WebGLFITC
 
GDC 2010 - A Dynamic Component Architecture for High Performance Gameplay - M...
GDC 2010 - A Dynamic Component Architecture for High Performance Gameplay - M...GDC 2010 - A Dynamic Component Architecture for High Performance Gameplay - M...
GDC 2010 - A Dynamic Component Architecture for High Performance Gameplay - M...Terrance Cohen
 
Lecture 5 - Procedural Content Generation
Lecture 5 - Procedural Content GenerationLecture 5 - Procedural Content Generation
Lecture 5 - Procedural Content GenerationLuke Dicken
 
Scene Graphs & Component Based Game Engines
Scene Graphs & Component Based Game EnginesScene Graphs & Component Based Game Engines
Scene Graphs & Component Based Game EnginesBryan Duggan
 
Game Programming 11 - Game Physics
Game Programming 11 - Game PhysicsGame Programming 11 - Game Physics
Game Programming 11 - Game PhysicsNick Pruehs
 
Killzone Shadow Fall: Threading the Entity Update on PS4
Killzone Shadow Fall: Threading the Entity Update on PS4Killzone Shadow Fall: Threading the Entity Update on PS4
Killzone Shadow Fall: Threading the Entity Update on PS4jrouwe
 
Presentation sanlab workshops
Presentation sanlab workshopsPresentation sanlab workshops
Presentation sanlab workshopsArtur Roszczyk
 
Cross platform physics games - NDC 2014
Cross platform physics games - NDC 2014Cross platform physics games - NDC 2014
Cross platform physics games - NDC 2014Runegri
 
Collision Detection an Overview
Collision Detection an OverviewCollision Detection an Overview
Collision Detection an Overviewslantsixgames
 

Similar a Programming physics games with Python and OpenGL (20)

august23.ppt
august23.pptaugust23.ppt
august23.ppt
 
Soc research
Soc researchSoc research
Soc research
 
Physics Solutions for Innovative Game Design
Physics Solutions for Innovative Game DesignPhysics Solutions for Innovative Game Design
Physics Solutions for Innovative Game Design
 
Core Animation
Core AnimationCore Animation
Core Animation
 
High speed multibody dynamics _ sherman_Simbody _ 2006.ppt
High speed multibody dynamics _ sherman_Simbody _ 2006.pptHigh speed multibody dynamics _ sherman_Simbody _ 2006.ppt
High speed multibody dynamics _ sherman_Simbody _ 2006.ppt
 
【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~
【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~
【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~
 
4-Game Physics (Game Design and Development)
4-Game Physics (Game Design and Development)4-Game Physics (Game Design and Development)
4-Game Physics (Game Design and Development)
 
october23.ppt
october23.pptoctober23.ppt
october23.ppt
 
Ai architectureand designpatternsgdc2009
Ai architectureand designpatternsgdc2009Ai architectureand designpatternsgdc2009
Ai architectureand designpatternsgdc2009
 
From Experimentation to Production: The Future of WebGL
From Experimentation to Production: The Future of WebGLFrom Experimentation to Production: The Future of WebGL
From Experimentation to Production: The Future of WebGL
 
GDC 2010 - A Dynamic Component Architecture for High Performance Gameplay - M...
GDC 2010 - A Dynamic Component Architecture for High Performance Gameplay - M...GDC 2010 - A Dynamic Component Architecture for High Performance Gameplay - M...
GDC 2010 - A Dynamic Component Architecture for High Performance Gameplay - M...
 
Lecture 5 - Procedural Content Generation
Lecture 5 - Procedural Content GenerationLecture 5 - Procedural Content Generation
Lecture 5 - Procedural Content Generation
 
Communityday2013
Communityday2013Communityday2013
Communityday2013
 
Scene Graphs & Component Based Game Engines
Scene Graphs & Component Based Game EnginesScene Graphs & Component Based Game Engines
Scene Graphs & Component Based Game Engines
 
Game Programming 11 - Game Physics
Game Programming 11 - Game PhysicsGame Programming 11 - Game Physics
Game Programming 11 - Game Physics
 
Killzone Shadow Fall: Threading the Entity Update on PS4
Killzone Shadow Fall: Threading the Entity Update on PS4Killzone Shadow Fall: Threading the Entity Update on PS4
Killzone Shadow Fall: Threading the Entity Update on PS4
 
Object-Orientated Design
Object-Orientated DesignObject-Orientated Design
Object-Orientated Design
 
Presentation sanlab workshops
Presentation sanlab workshopsPresentation sanlab workshops
Presentation sanlab workshops
 
Cross platform physics games - NDC 2014
Cross platform physics games - NDC 2014Cross platform physics games - NDC 2014
Cross platform physics games - NDC 2014
 
Collision Detection an Overview
Collision Detection an OverviewCollision Detection an Overview
Collision Detection an Overview
 

Último

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 

Último (20)

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 

Programming physics games with Python and OpenGL

Notas del editor

  1. Very fast moving objects (eg. bullets) may have passed completely through each other in a single time step – compute time of first intersection