infinum android talks #15 - how to develop a simple 2d game with physics engine
TRANSCRIPT
How to develop a simple 2D game with physics engineIVAN 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 collisions3. 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