SlideShare a Scribd company logo
How to develop a
simple 2D game with
physics engine
IVAN KUŠT, ANDROID TEAM LEADER
THE GAME
• Balls with random numbers 0
- 9 are falling from the sky
• User must catch as many as
possible in the bucket that
can move along the bottom
• Number of the ball defines
score for catching the ball
SO WHAT DO WE NEED?
• A way to spawn the objects on the top
• Simple physics - gravity and collisions
• Scoring and time limit
01SPAWNING OBJECTS
OBJECTS
• Each object in physics engine is either:
• circle
• rectangle

• Gravity can be applied or not
• Current position
• Current speed vector (x, y components)
SPAWNING OBJECTS
• Balls are spawned at a time
interval
• Random size (with min and
max values)
• Random position
• Once spawned, object is
added to physics engine 

(which applies gravity and
resolves collisions of the
object)
SPAWNING AREA
02COLLISIONS
02.1COLLISION DETECTION
AXIS ALIGNED BOUNDING BOXES
X
Y
• Rectangle that encloses an
object in the game
• Used for collision detection:
• easiest
• fastest
AABB COLLISION DETECTION
• Rectangles intersect
• Extreme case:
• one rect encloses another
CIRCLES
• Distance between centers <=
sum of radiuses
CIRCLE VS AABB
• Distance between closest
point of rect and circle center
<= circle radius
02.2IMPULSE RESOLUTION
OBJECTS COLLIDED - NOW WHAT?
• Calculate new speed vectors for each object
• Two dimensional elastic collision
• Every object has an impulse or momentum:

Impulse = mass * velocity


WHEN OBJECTS COLLIDE
• The relative velocity of one particle with respect to the other
is reversed by the collision
• The average of the momenta before and after the collision is
the same for both particles
CIRCLE VS CIRCLE
TANGENT
NORMAL
• Overall velocity split into two
perpendicular velocities
• One along common normal 

(line of collision)

• One perpendicular to
common normal
X
Y
CIRCLE VS AABB
TANGENT
NORMAL
• Calculate new velocities
depending on the masses of
the object
• Recalculate new velocities to
the x and y aligned velocity
components
X
Y
SIMULATING REALITY
• In reality, some kinetic energy is lost on collision

(the ball never bounces back from the ground to the same
height)
• Restitution coefficient
• multiply calculated velocities

• Feels more “real”
IN THE GAME
• Two rectangles on each side
of the bucket that collide with
the balls
• mass = infinite
• Simulates bucket collisions
02.3SINKING
SINKING
• What if an object with low restitution hits a wall (or floor)
with infinite mass?
• It begins to sink (the expected behavior would be for the ball
to stay next to the wall or floor)
• due to floating point error that accumulates over time
SOLUTION
TANGENT
NORMAL
• Solution: linear projection
• move each object by a
small percentage of the
mass along collision
normal
03SCORING
SCORING
• Goal of the game is to collect as many balls as possible in a
limited amount of time

• Score for a “catch” is equal to number shown on the ball

SCORING
• A rectangular region is
defined
• if a ball is in the region it is
removed from physics
engine and score is
increased
SCORING REGION
04ARCHITECTURE OVERVIEW
ENGINE ARCHITECTURE
PHYSICS ENGINESOLID
SOLID CIRCLE SOLID RECT
OBJECT GENERATOR
BALL GENERATOR
GAME IMPLEMENTATION
• Implemented as a custom View (no OpenGL)
• Constant redraw is forced
• invalidate() posted in a loop using Handler
• On each frame:

1. appliy gravity to all objects

2. check and resolve collisions

3. generate new objects
4. draw new frame based on state of objects
CONCLUSIONS
• Simple game can be implemented without using OpenGL
• Various screen sizes must be taken into account:
• easier to catch if resolution is higher

REFERENCES
• http://gamedevelopment.tutsplus.com/tutorials/how-to-
create-a-custom-2d-physics-engine-the-basics-and-
impulse-resolution--gamedev-6331
• http://gamedevelopment.tutsplus.com/tutorials/when-
worlds-collide-simulating-circle-circle-collisions--
gamedev-769
• https://en.wikipedia.org/wiki/Elastic_collision
• http://hypertextbook.com/facts/2006/restitution.shtml

More Related Content

PPTX
Tutorial two cubes falling on a floor due to gravity
PPTX
Unite Boston 2015 Physics
PPTX
PDF
Physics Solutions for Innovative Game Design
ODP
Programming physics games with Python and OpenGL
PPTX
Flash Particle System
PPTX
A graphic library and an application for simple curve manipolation
PDF
OGDC 2014_Build your own Particle System_Mr. Nguyen Dang Quang
Tutorial two cubes falling on a floor due to gravity
Unite Boston 2015 Physics
Physics Solutions for Innovative Game Design
Programming physics games with Python and OpenGL
Flash Particle System
A graphic library and an application for simple curve manipolation
OGDC 2014_Build your own Particle System_Mr. Nguyen Dang Quang

Similar to Infinum Android Talks #15 - How to develop a simple 2D game with physics engine (20)

PDF
OGDC 2014: Build your own particle system
PDF
Physics artifacts
PPTX
Animation programming
PPTX
Fool me twice
PPT
November 30, Projects
PPT
Paris Master Class 2011 - 06 Gpu Particle System
PDF
Game Physics Test
PDF
SPU Physics
PPT
4.3 real time game physics
PPTX
Get moving: An overview of physics in DOTS – Unite Copenhagen 2019
PPTX
Critical Design Review and Report
PPTX
Unity workshop
PPTX
Haroon alam
PDF
Bullet Physic Engine SDK
PDF
Unity introduction for programmers
PDF
team 1 poster
PPTX
VIRTUAL REALITY & AUGMENTED REALITY
PPTX
A Highly Parallel Semi-Dataflow FPGA Architecture for Large-Scale N-Body Simu...
PPTX
Game engineering_Tappy plane - jiit13-14
OGDC 2014: Build your own particle system
Physics artifacts
Animation programming
Fool me twice
November 30, Projects
Paris Master Class 2011 - 06 Gpu Particle System
Game Physics Test
SPU Physics
4.3 real time game physics
Get moving: An overview of physics in DOTS – Unite Copenhagen 2019
Critical Design Review and Report
Unity workshop
Haroon alam
Bullet Physic Engine SDK
Unity introduction for programmers
team 1 poster
VIRTUAL REALITY & AUGMENTED REALITY
A Highly Parallel Semi-Dataflow FPGA Architecture for Large-Scale N-Body Simu...
Game engineering_Tappy plane - jiit13-14
Ad

More from Infinum (20)

PDF
Infinum Android Talks #20 - Making your Android apps fast like Blue Runner an...
PDF
Infinum Android Talks #20 - DiffUtil
PDF
Infinum Android Talks #20 - Benefits of using Kotlin
PDF
Infinum iOS Talks #4 - Making our VIPER more reactive
PDF
Infinum iOS Talks #4 - Making your Swift networking code more awesome with Re...
PDF
Infinum Android Talks #13 - Using ViewDragHelper
PDF
Infinum Android Talks #14 - Log4j
PDF
Infinum Android Talks #9 - Making your app location-aware
PDF
Infinum Android Talks #14 - Gradle plugins
PDF
Infinum Android Talks #14 - Facebook for Android API
PDF
Infinum Android Talks #19 - Stop wasting time fixing bugs with TDD by Domagoj...
PDF
Infinum Android Talks #18 - Create fun lists by Ivan Marić
PDF
Infinum Android Talks #18 - In-app billing by Ivan Marić
PDF
Infinum Android Talks #18 - How to cache like a boss by Željko Plesac
PDF
Infinum iOS Talks #2 - VIPER for everybody by Damjan Vujaklija
PDF
Infinum iOS Talks #2 - Xamarin by Ivan Đikić
PDF
Infinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho Poluta
PDF
Infinum iOS Talks #1 - Swift done right by Ivan Dikic
PDF
Infinum iOS Talks #1 - Becoming an iOS developer swiftly by Vedran Burojevic
PDF
Infinum Android Talks #17 - Testing your Android applications by Ivan Kust
Infinum Android Talks #20 - Making your Android apps fast like Blue Runner an...
Infinum Android Talks #20 - DiffUtil
Infinum Android Talks #20 - Benefits of using Kotlin
Infinum iOS Talks #4 - Making our VIPER more reactive
Infinum iOS Talks #4 - Making your Swift networking code more awesome with Re...
Infinum Android Talks #13 - Using ViewDragHelper
Infinum Android Talks #14 - Log4j
Infinum Android Talks #9 - Making your app location-aware
Infinum Android Talks #14 - Gradle plugins
Infinum Android Talks #14 - Facebook for Android API
Infinum Android Talks #19 - Stop wasting time fixing bugs with TDD by Domagoj...
Infinum Android Talks #18 - Create fun lists by Ivan Marić
Infinum Android Talks #18 - In-app billing by Ivan Marić
Infinum Android Talks #18 - How to cache like a boss by Željko Plesac
Infinum iOS Talks #2 - VIPER for everybody by Damjan Vujaklija
Infinum iOS Talks #2 - Xamarin by Ivan Đikić
Infinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho Poluta
Infinum iOS Talks #1 - Swift done right by Ivan Dikic
Infinum iOS Talks #1 - Becoming an iOS developer swiftly by Vedran Burojevic
Infinum Android Talks #17 - Testing your Android applications by Ivan Kust
Ad

Recently uploaded (20)

PPTX
ai tools demonstartion for schools and inter college
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
AI in Product Development-omnex systems
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
Online Work Permit System for Fast Permit Processing
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Nekopoi APK 2025 free lastest update
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
System and Network Administration Chapter 2
PPTX
Introduction to Artificial Intelligence
PPTX
history of c programming in notes for students .pptx
ai tools demonstartion for schools and inter college
Upgrade and Innovation Strategies for SAP ERP Customers
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
How Creative Agencies Leverage Project Management Software.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 41
AI in Product Development-omnex systems
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Online Work Permit System for Fast Permit Processing
Design an Analysis of Algorithms I-SECS-1021-03
How to Migrate SBCGlobal Email to Yahoo Easily
Softaken Excel to vCard Converter Software.pdf
Adobe Illustrator 28.6 Crack My Vision of Vector Design
How to Choose the Right IT Partner for Your Business in Malaysia
Nekopoi APK 2025 free lastest update
Wondershare Filmora 15 Crack With Activation Key [2025
System and Network Administration Chapter 2
Introduction to Artificial Intelligence
history of c programming in notes for students .pptx

Infinum Android Talks #15 - How to develop a simple 2D game with physics engine