30
How to develop a simple 2D game with physics engine IVAN KUŠT, ANDROID TEAM LEADER

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

  • Upload
    infinum

  • View
    328

  • Download
    1

Embed Size (px)

Citation preview

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

How to develop a simple 2D game with physics engineIVAN KUŠT, ANDROID TEAM LEADER

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

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

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

SO WHAT DO WE NEED?

• A way to spawn the objects on the top

• Simple physics - gravity and collisions

• Scoring and time limit

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

01SPAWNING OBJECTS

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

OBJECTS

• Each object in physics engine is either: • circle • rectangle

• Gravity can be applied or not

• Current position

• Current speed vector (x, y components)

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

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

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

02COLLISIONS

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

02.1COLLISION DETECTION

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

AXIS ALIGNED BOUNDING BOXES

X

Y

• Rectangle that encloses an

object in the game

• Used for collision detection: • easiest • fastest

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

AABB COLLISION DETECTION

• Rectangles intersect

• Extreme case: • one rect encloses another

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

CIRCLES

• Distance between centers <=

sum of radiuses

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

CIRCLE VS AABB

• Distance between closest

point of rect and circle center

<= circle radius

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

02.2IMPULSE RESOLUTION

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

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

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

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

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

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

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

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

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

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”

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

IN THE GAME

• Two rectangles on each side

of the bucket that collide with

the balls • mass = infinite

• Simulates bucket collisions

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

02.3SINKING

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

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

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

SOLUTION

TANGENT

NORMAL

• Solution: linear projection • move each object by a

small percentage of the

mass along collision

normal

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

03SCORING

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

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

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

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

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

04ARCHITECTURE OVERVIEW

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

ENGINE ARCHITECTURE

PHYSICS ENGINESOLID

SOLID CIRCLE SOLID RECT

OBJECT GENERATOR

BALL GENERATOR

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

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

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

CONCLUSIONS

• Simple game can be implemented without using OpenGL

• Various screen sizes must be taken into account: • easier to catch if resolution is higher

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

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