SlideShare une entreprise Scribd logo
1  sur  57
Physics in Android™ games
                          Pär Sikö, Martin Gunnarsson




                          #androidphysics

lördag den 5 mars 2011
Popularity




                         Time


lördag den 5 mars 2011
Popularity




                         Time


lördag den 5 mars 2011
Popularity




                         Time


lördag den 5 mars 2011
Popularity




                         Time


lördag den 5 mars 2011
Popularity




                         Time


lördag den 5 mars 2011
Popularity




                         Time


lördag den 5 mars 2011
“Physics (from Ancient Greek: φύσις physis "nature") is a natural science
               that involves the study of matter and its motion through spacetime, as
               well as all related concepts, including energy and force. More broadly, it
               is the general analysis of nature, conducted in order to understand how
               the universe behaves”
                                                                               Wikipedia




lördag den 5 mars 2011
“Physics in Android is when you beat the shit out of all the pigs in Angry Birds”

                                                           Anonymous Angry Birds addict




lördag den 5 mars 2011
Physics based games




lördag den 5 mars 2011
Physics based games




lördag den 5 mars 2011
Physics based games




lördag den 5 mars 2011
Physics based games




lördag den 5 mars 2011
Physics based games




lördag den 5 mars 2011
Physics based games




lördag den 5 mars 2011
Physics 101




lördag den 5 mars 2011
lördag den 5 mars 2011
Theory
                         (for real)




lördag den 5 mars 2011
Features




lördag den 5 mars 2011
Features

                         ► Gravity




lördag den 5 mars 2011
Features

                         ► Gravity
                         ► Collisions




lördag den 5 mars 2011
Features

                         ► Gravity
                         ► Collisions
                         ► Joints




lördag den 5 mars 2011
Features

                         ► Gravity
                         ► Collisions
                         ► Joints
                         ► Extra properties




lördag den 5 mars 2011
Physics Engines




lördag den 5 mars 2011
Architecture


                            Java



                         JNI wrapper


                         Native code




lördag den 5 mars 2011
Physics engines




                         Box2D




lördag den 5 mars 2011
Physics engines




                         libgdx



                         Box2D




lördag den 5 mars 2011
Physics engines


                         AndEngine



                           libgdx



                          Box2D




lördag den 5 mars 2011
AndEngine




lördag den 5 mars 2011
public class Example extends BaseGameActivity {

    	
    	     public Engine onLoadEngine() {
    	     	
    	     }

    	
    	     public void onLoadResources() {
    	     	
    	     }

    	
    	     public Scene onLoadScene() {
    	     	
    	     }

    	
    	     public void onLoadComplete() {
    	     	
    	     }
    }




lördag den 5 mars 2011
onLoadEngine
     private final static int WIDTH = 480;
     private final static int HEIGHT = 320;


     public Engine onLoadEngine() {
     	    Camera camera = new Camera(0, 0, WIDTH, HEIGHT);
     	    RatioResolutionPolicy ratio = new RatioResolutionPolicy(WIDTH, HEIGHT);

     	     EngineOptions options =
                new EngineOptions(true, ScreenOrientation.LANDSCAPE, ratio, camera);

     	     return new Engine(options);
     }




lördag den 5 mars 2011
onLoadResources
     protected Texture texture;
     protected TextureRegion region;


     public void onLoadResources() {
     	    this.texture = new Texture(256, 32);
     	    this.region = TextureRegionFactory.createFromAsset(this.texture, this,
               "gfx/image.png", 0, 0);

     	     this.mEngine.getTextureManager().loadTexture(this.texture);
     }




lördag den 5 mars 2011
Textures and regions




                         2n × 2m px




lördag den 5 mars 2011
Textures and regions




                         2n × 2m px




lördag den 5 mars 2011
onLoadResources
     protected Texture texture;
     protected TextureRegion region;


     public void onLoadResources() {
     	    this.texture = new Texture(256, 32);
     	    this.region = TextureRegionFactory.createFromAsset(this.texture, this,
               "gfx/image.png", 0, 0);

     	     this.mEngine.getTextureManager().loadTexture(this.texture);
     }




lördag den 5 mars 2011
onLoadScene
      public Scene onLoadScene() {
      	    final Scene scene = new Scene(1);
      	    scene.setBackground(new ColorBackground(1f, 1f, 1f));

      	     Sprite sprite = new Sprite(WIDTH / 2, HEIGHT / 2, this.region);
      	     scene.getTopLayer().addEntity(sprite);

      	     return scene;
      }




lördag den 5 mars 2011
onLoadComplete
      public void onLoadComplete() {

      }




lördag den 5 mars 2011
Adding physics




lördag den 5 mars 2011
public Engine onLoadEngine() {
  	    final Camera camera = new Camera(0, 0, WIDTH, HEIGHT);
  	    final RatioResolutionPolicy ratio = new RatioResolutionPolicy(camera.getWidth(),
             camera.getHeight());
  	    return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, ratio, camera));
  }



  public void onLoadResources() {
  	    this.texture = new Texture(256, 32);
  	    this.tileRegions = TextureRegionFactory.createTiledFromAsset(this.texture, this,
             "gfx/blocks.png", 0, 0, BLOCKS, 1);
  	    this.mEngine.getTextureManager().loadTexture(this.texture);
  }



  public Scene onLoadScene() {
  	    final Scene scene = new Scene(1);
  	    scene.setBackground(new ColorBackground(0, 0, 0));

  	     for (int i = 0; i < BLOCKS; i++) {
  	     	    TiledSprite sprite = new TiledSprite(68 + i * 52, 144, this.tileRegions.clone());
  	     	    sprite.setCurrentTileIndex(i);
  	     	    scene.getTopLayer().addEntity(sprite);
  	     }

  	     return scene;
  }




lördag den 5 mars 2011
public class Example extends BaseGameActivity {

        protected PhysicsWorld physicsWorld;
        protected FixtureDef fixtureDef = PhysicsFactory.createFixtureDef(10, 0.5f, 0.5f);

        ...




lördag den 5 mars 2011
onLoadScene
      public Scene onLoadScene() {
      	    final Scene scene = new Scene(1);
      	    scene.setBackground(new ColorBackground(0, 0, 0));

            physicsWorld = new PhysicsWorld(
                 new Vector2(0, SensorManager.GRAVITY_EARTH), true);

      	     for (int i = 0; i < BLOCKS; i++) {
      	     	    TiledSprite sprite = new TiledSprite(68 + i * 52, 144,
                       this.tileRegions.clone());
      	     	    sprite.setCurrentTileIndex(i);
      	     	    scene.getTopLayer().addEntity(sprite);

      	     	     Body body = PhysicsFactory.createBoxBody(physicsWorld, sprite,
      	     	     	    BodyType.DynamicBody, fixtureDefinition);
                  this.physicsWorld.registerPhysicsConnector(new PhysicsConnector(
                  	    sprite, body));
      	     }

            scene.registerUpdateHandler(physicsWorld);
      	     return scene;
      }



lördag den 5 mars 2011
onLoadScene
      final   Shape   ground = new Rectangle(0, HEIGHT - 2, WIDTH, 2);
      final   Shape   roof = new Rectangle(0, 0, WIDTH, 2);
      final   Shape   left = new Rectangle(0, 0, 2, HEIGHT);
      final   Shape   right = new Rectangle(WIDTH - 2, 0, 2, HEIGHT);

      PhysicsFactory.createBoxBody(this.physicsWorld,     ground, BodyType.StaticBody,
           this.fixtureDefinition);
      PhysicsFactory.createBoxBody(this.physicsWorld,     roof, BodyType.StaticBody,
           this.fixtureDefinition);
      PhysicsFactory.createBoxBody(this.physicsWorld,     left, BodyType.StaticBody,
           this.fixtureDefinition);
      PhysicsFactory.createBoxBody(this.physicsWorld,     right, BodyType.StaticBody,
           this.fixtureDefinition);

      scene.getBottomLayer().addEntity(ground);
      scene.getBottomLayer().addEntity(roof);
      scene.getBottomLayer().addEntity(left);
      scene.getBottomLayer().addEntity(right);




lördag den 5 mars 2011
Accelerometer
      public class Example extends BaseGameActivity implements IAccelerometerListener {

      	     public void onAccelerometerChanged(AccelerometerData data) {
      	     	    this.physicsWorld.setGravity(new Vector2(data.getY(), data.getX()));
      	     }


            public Scene onLoadScene() {
      	     	    this.enableAccelerometerSensor(this);

      	     	     ...
      	     }

      	     ...




lördag den 5 mars 2011
Making a game




lördag den 5 mars 2011
Making a game




lördag den 5 mars 2011
Making a game


     ► Load resources
     ► Load scene
     ► Collision detection
     ► Handle touch events




lördag den 5 mars 2011
onLoadResources
      public void onLoadResources() {

      	     texture = new Texture(256, 256, TextureOptions.BILINEAR);

            woodRegion = TextureRegionFactory.createFromAsset(texture, this,
                 "gfx/wood_base.png", 0, 0);
            stoneRegion = TextureRegionFactory.createFromAsset(texture, this,
                 "gfx/stone_base.png", 8, 0);
      	     joltRegion = TextureRegionFactory.createFromAsset(texture, this,
                 "gfx/jolt.png", 72, 0);
      	     bossRegion = TextureRegionFactory.createFromAsset(texture, this,
                 "gfx/boss.png", 136, 0);

            ...

            this.mEngine.getTextureManager().loadTexture(texture);

      }




lördag den 5 mars 2011
onLoadScene
      final Scene scene = new Scene(1);

      scene.setBackground(new RepeatingSpriteBackground(CAMERA_WIDTH,
           CAMERA_HEIGHT, mEngine.getTextureManager(), new AssetTextureSource(this,
           "gfx/background.png")));

      scene.setOnSceneTouchListener(this);

      this.physicsWorld = new PhysicsWorld(new Vector2(0,
           SensorManager.GRAVITY_EARTH), true, 3, 2);

      this.physicsWorld.setContactListener(getContactListener(scene));

      // Position constants: TRANSLATE1, TRANSLATE2, floor1 ...

      // First floor
      add(scene, stoneRegion, TRANSLATE1, floor1, 0);
      add(scene, stoneRegion, TRANSLATE2, floor1, 0);
      add(scene, woodRegion, TRANSLATE1 + WMH / 2f, floor1 - WPH / 2f, -90f);
      ...




lördag den 5 mars 2011
onLoadScene




lördag den 5 mars 2011
onLoadScene
      final Scene scene = new Scene(1);

      scene.setBackground(new RepeatingSpriteBackground(CAMERA_WIDTH,
           CAMERA_HEIGHT, mEngine.getTextureManager(), new AssetTextureSource(this,
           "gfx/background.png")));

      scene.setOnSceneTouchListener(this);

      this.physicsWorld = new PhysicsWorld(new Vector2(0,
           SensorManager.GRAVITY_EARTH), true, 3, 2);

      this.physicsWorld.setContactListener(getContactListener(scene));

      // Position constants: TRANSLATE1, TRANSLATE2, floor1 ...

      // First floor
      add(scene, stoneRegion, TRANSLATE1, floor1, 0);
      add(scene, stoneRegion, TRANSLATE2, floor1, 0);
      add(scene, woodRegion, TRANSLATE1 + WMH / 2f, floor1 - WPH / 2f, -90f);
      ...




lördag den 5 mars 2011
add(...)
      Sprite sprite = new Sprite(xTranslate, yTranslate, region.clone());
      sprite.setRotation(rotation);

      scene.getTopLayer().addEntity(sprite);
      Body body = PhysicsFactory.createBoxBody(physicsWorld, sprite,
      	    bodyType, fixtureDefinition);

      this.physicsWorld.registerPhysicsConnector(new PhysicsConnector(sprite, body));

      return body;




lördag den 5 mars 2011
onLoadScene
      // Add bosses
      add(scene, bossRegion, TRANSLATE2 + 50, floor1, 0);
      add(scene, bossRegion, TRANSLATE1 + 20, floor1, 0);

      // Add jolt cola to throw at the bosses
      add(scene, joltRegion, 50, 300, 0);




lördag den 5 mars 2011
Collision detection
      private ContactListener getContactListener(final Scene scene) {
      	    return new ContactListener() {
      	    	
      	    	    public void beginContact(Contact contact) {
      	    	    	    Fixture fixtureA = contact.getFixtureA();
      	    	    	    Fixture fixtureB = contact.getFixtureB();

      	     	     	      for   (final Body bossBody : bosses.keySet()) {
      	     	     	      	      Fixture boss = bossBody.getFixtureList().get(0);
      	     	     	      	      if (fixtureA == boss || fixtureB == boss) {
      	     	     	      	      	    hitCount++;
      	     	     	      	      	    if (hitCount == 10) {
      	     	     	      	      	    	    // Remove boss from world and scene
      	     	     	      	      	    }
      	     	     	      	      }
      	     	     	      }
      	     	     }
      	     };
      }




lördag den 5 mars 2011
Touch events
      public boolean onSceneTouchEvent(Scene pScene, TouchEvent touch) {
      	    int action = touch.getAction();

      	     if (action == TouchEvent.ACTION_DOWN) {
      	     	    startX = (int) touch.getMotionEvent().getX();
      	     	    startY = (int) touch.getMotionEvent().getY();
      	     	    return true;
      	     }

            if (action == TouchEvent.ACTION_UP) {
      	     	    float xDiff = (int) (startX - touch.getMotionEvent().getX());
      	     	    float yDiff = (int) (startY - touch.getMotionEvent().getY());
      	     	    can.applyLinearImpulse(new Vector2(2 * xDiff, yDiff),
      	     	    	    	   new Vector2(can.getPosition().x, can.getPosition().y));
      	     	
      	     	    addNewCan();
      	     	    return true;
      	     }

      	     return false;
      }




lördag den 5 mars 2011
Performance




lördag den 5 mars 2011
Performance
        60


        45


        30


        15


          0
              0                50     100        150


                         HTC Hero   X10     Xperia Play


lördag den 5 mars 2011
Thank you!

                         per.siko@epsilon.nu
                         martin.gunnarsson@epsilon.nu




lördag den 5 mars 2011

Contenu connexe

Plus de Codemotion

Eward Driehuis - What we learned from 20.000 attacks - Codemotion Amsterdam 2019
Eward Driehuis - What we learned from 20.000 attacks - Codemotion Amsterdam 2019Eward Driehuis - What we learned from 20.000 attacks - Codemotion Amsterdam 2019
Eward Driehuis - What we learned from 20.000 attacks - Codemotion Amsterdam 2019Codemotion
 
Francesco Baldassarri - Deliver Data at Scale - Codemotion Amsterdam 2019 -
Francesco Baldassarri  - Deliver Data at Scale - Codemotion Amsterdam 2019 - Francesco Baldassarri  - Deliver Data at Scale - Codemotion Amsterdam 2019 -
Francesco Baldassarri - Deliver Data at Scale - Codemotion Amsterdam 2019 - Codemotion
 
Martin Förtsch, Thomas Endres - Stereoscopic Style Transfer AI - Codemotion A...
Martin Förtsch, Thomas Endres - Stereoscopic Style Transfer AI - Codemotion A...Martin Förtsch, Thomas Endres - Stereoscopic Style Transfer AI - Codemotion A...
Martin Förtsch, Thomas Endres - Stereoscopic Style Transfer AI - Codemotion A...Codemotion
 
Melanie Rieback, Klaus Kursawe - Blockchain Security: Melting the "Silver Bul...
Melanie Rieback, Klaus Kursawe - Blockchain Security: Melting the "Silver Bul...Melanie Rieback, Klaus Kursawe - Blockchain Security: Melting the "Silver Bul...
Melanie Rieback, Klaus Kursawe - Blockchain Security: Melting the "Silver Bul...Codemotion
 
Angelo van der Sijpt - How well do you know your network stack? - Codemotion ...
Angelo van der Sijpt - How well do you know your network stack? - Codemotion ...Angelo van der Sijpt - How well do you know your network stack? - Codemotion ...
Angelo van der Sijpt - How well do you know your network stack? - Codemotion ...Codemotion
 
Lars Wolff - Performance Testing for DevOps in the Cloud - Codemotion Amsterd...
Lars Wolff - Performance Testing for DevOps in the Cloud - Codemotion Amsterd...Lars Wolff - Performance Testing for DevOps in the Cloud - Codemotion Amsterd...
Lars Wolff - Performance Testing for DevOps in the Cloud - Codemotion Amsterd...Codemotion
 
Sascha Wolter - Conversational AI Demystified - Codemotion Amsterdam 2019
Sascha Wolter - Conversational AI Demystified - Codemotion Amsterdam 2019Sascha Wolter - Conversational AI Demystified - Codemotion Amsterdam 2019
Sascha Wolter - Conversational AI Demystified - Codemotion Amsterdam 2019Codemotion
 
Michele Tonutti - Scaling is caring - Codemotion Amsterdam 2019
Michele Tonutti - Scaling is caring - Codemotion Amsterdam 2019Michele Tonutti - Scaling is caring - Codemotion Amsterdam 2019
Michele Tonutti - Scaling is caring - Codemotion Amsterdam 2019Codemotion
 
Pat Hermens - From 100 to 1,000+ deployments a day - Codemotion Amsterdam 2019
Pat Hermens - From 100 to 1,000+ deployments a day - Codemotion Amsterdam 2019Pat Hermens - From 100 to 1,000+ deployments a day - Codemotion Amsterdam 2019
Pat Hermens - From 100 to 1,000+ deployments a day - Codemotion Amsterdam 2019Codemotion
 
James Birnie - Using Many Worlds of Compute Power with Quantum - Codemotion A...
James Birnie - Using Many Worlds of Compute Power with Quantum - Codemotion A...James Birnie - Using Many Worlds of Compute Power with Quantum - Codemotion A...
James Birnie - Using Many Worlds of Compute Power with Quantum - Codemotion A...Codemotion
 
Don Goodman-Wilson - Chinese food, motor scooters, and open source developmen...
Don Goodman-Wilson - Chinese food, motor scooters, and open source developmen...Don Goodman-Wilson - Chinese food, motor scooters, and open source developmen...
Don Goodman-Wilson - Chinese food, motor scooters, and open source developmen...Codemotion
 
Pieter Omvlee - The story behind Sketch - Codemotion Amsterdam 2019
Pieter Omvlee - The story behind Sketch - Codemotion Amsterdam 2019Pieter Omvlee - The story behind Sketch - Codemotion Amsterdam 2019
Pieter Omvlee - The story behind Sketch - Codemotion Amsterdam 2019Codemotion
 
Dave Farley - Taking Back “Software Engineering” - Codemotion Amsterdam 2019
Dave Farley - Taking Back “Software Engineering” - Codemotion Amsterdam 2019Dave Farley - Taking Back “Software Engineering” - Codemotion Amsterdam 2019
Dave Farley - Taking Back “Software Engineering” - Codemotion Amsterdam 2019Codemotion
 
Joshua Hoffman - Should the CTO be Coding? - Codemotion Amsterdam 2019
Joshua Hoffman - Should the CTO be Coding? - Codemotion Amsterdam 2019Joshua Hoffman - Should the CTO be Coding? - Codemotion Amsterdam 2019
Joshua Hoffman - Should the CTO be Coding? - Codemotion Amsterdam 2019Codemotion
 
Mike Kotsur - What can philosophy teach us about programming - Codemotion Ams...
Mike Kotsur - What can philosophy teach us about programming - Codemotion Ams...Mike Kotsur - What can philosophy teach us about programming - Codemotion Ams...
Mike Kotsur - What can philosophy teach us about programming - Codemotion Ams...Codemotion
 
Mete Atamel - Serverless with Knative - Codemotion Amsterdam 2019
Mete Atamel - Serverless with Knative - Codemotion Amsterdam 2019Mete Atamel - Serverless with Knative - Codemotion Amsterdam 2019
Mete Atamel - Serverless with Knative - Codemotion Amsterdam 2019Codemotion
 
Rahul Shetty - Corporate relocation prediction - Codemotion Amsterdam 2019
Rahul Shetty - Corporate relocation prediction - Codemotion Amsterdam 2019Rahul Shetty - Corporate relocation prediction - Codemotion Amsterdam 2019
Rahul Shetty - Corporate relocation prediction - Codemotion Amsterdam 2019Codemotion
 
Mario Viviani - Designing apps for fire TV - Codemotion Amsterdam 2019
Mario Viviani - Designing apps for fire TV - Codemotion Amsterdam 2019Mario Viviani - Designing apps for fire TV - Codemotion Amsterdam 2019
Mario Viviani - Designing apps for fire TV - Codemotion Amsterdam 2019Codemotion
 
Ilona Demidenko - Conversational Sign Up - Codemotion Amsterdam 2019
Ilona Demidenko - Conversational Sign Up - Codemotion Amsterdam 2019Ilona Demidenko - Conversational Sign Up - Codemotion Amsterdam 2019
Ilona Demidenko - Conversational Sign Up - Codemotion Amsterdam 2019Codemotion
 
Katie Koschland - Ready, steady, crash - Codemotion Amsterdam 2019
Katie Koschland - Ready, steady, crash - Codemotion Amsterdam 2019Katie Koschland - Ready, steady, crash - Codemotion Amsterdam 2019
Katie Koschland - Ready, steady, crash - Codemotion Amsterdam 2019Codemotion
 

Plus de Codemotion (20)

Eward Driehuis - What we learned from 20.000 attacks - Codemotion Amsterdam 2019
Eward Driehuis - What we learned from 20.000 attacks - Codemotion Amsterdam 2019Eward Driehuis - What we learned from 20.000 attacks - Codemotion Amsterdam 2019
Eward Driehuis - What we learned from 20.000 attacks - Codemotion Amsterdam 2019
 
Francesco Baldassarri - Deliver Data at Scale - Codemotion Amsterdam 2019 -
Francesco Baldassarri  - Deliver Data at Scale - Codemotion Amsterdam 2019 - Francesco Baldassarri  - Deliver Data at Scale - Codemotion Amsterdam 2019 -
Francesco Baldassarri - Deliver Data at Scale - Codemotion Amsterdam 2019 -
 
Martin Förtsch, Thomas Endres - Stereoscopic Style Transfer AI - Codemotion A...
Martin Förtsch, Thomas Endres - Stereoscopic Style Transfer AI - Codemotion A...Martin Förtsch, Thomas Endres - Stereoscopic Style Transfer AI - Codemotion A...
Martin Förtsch, Thomas Endres - Stereoscopic Style Transfer AI - Codemotion A...
 
Melanie Rieback, Klaus Kursawe - Blockchain Security: Melting the "Silver Bul...
Melanie Rieback, Klaus Kursawe - Blockchain Security: Melting the "Silver Bul...Melanie Rieback, Klaus Kursawe - Blockchain Security: Melting the "Silver Bul...
Melanie Rieback, Klaus Kursawe - Blockchain Security: Melting the "Silver Bul...
 
Angelo van der Sijpt - How well do you know your network stack? - Codemotion ...
Angelo van der Sijpt - How well do you know your network stack? - Codemotion ...Angelo van der Sijpt - How well do you know your network stack? - Codemotion ...
Angelo van der Sijpt - How well do you know your network stack? - Codemotion ...
 
Lars Wolff - Performance Testing for DevOps in the Cloud - Codemotion Amsterd...
Lars Wolff - Performance Testing for DevOps in the Cloud - Codemotion Amsterd...Lars Wolff - Performance Testing for DevOps in the Cloud - Codemotion Amsterd...
Lars Wolff - Performance Testing for DevOps in the Cloud - Codemotion Amsterd...
 
Sascha Wolter - Conversational AI Demystified - Codemotion Amsterdam 2019
Sascha Wolter - Conversational AI Demystified - Codemotion Amsterdam 2019Sascha Wolter - Conversational AI Demystified - Codemotion Amsterdam 2019
Sascha Wolter - Conversational AI Demystified - Codemotion Amsterdam 2019
 
Michele Tonutti - Scaling is caring - Codemotion Amsterdam 2019
Michele Tonutti - Scaling is caring - Codemotion Amsterdam 2019Michele Tonutti - Scaling is caring - Codemotion Amsterdam 2019
Michele Tonutti - Scaling is caring - Codemotion Amsterdam 2019
 
Pat Hermens - From 100 to 1,000+ deployments a day - Codemotion Amsterdam 2019
Pat Hermens - From 100 to 1,000+ deployments a day - Codemotion Amsterdam 2019Pat Hermens - From 100 to 1,000+ deployments a day - Codemotion Amsterdam 2019
Pat Hermens - From 100 to 1,000+ deployments a day - Codemotion Amsterdam 2019
 
James Birnie - Using Many Worlds of Compute Power with Quantum - Codemotion A...
James Birnie - Using Many Worlds of Compute Power with Quantum - Codemotion A...James Birnie - Using Many Worlds of Compute Power with Quantum - Codemotion A...
James Birnie - Using Many Worlds of Compute Power with Quantum - Codemotion A...
 
Don Goodman-Wilson - Chinese food, motor scooters, and open source developmen...
Don Goodman-Wilson - Chinese food, motor scooters, and open source developmen...Don Goodman-Wilson - Chinese food, motor scooters, and open source developmen...
Don Goodman-Wilson - Chinese food, motor scooters, and open source developmen...
 
Pieter Omvlee - The story behind Sketch - Codemotion Amsterdam 2019
Pieter Omvlee - The story behind Sketch - Codemotion Amsterdam 2019Pieter Omvlee - The story behind Sketch - Codemotion Amsterdam 2019
Pieter Omvlee - The story behind Sketch - Codemotion Amsterdam 2019
 
Dave Farley - Taking Back “Software Engineering” - Codemotion Amsterdam 2019
Dave Farley - Taking Back “Software Engineering” - Codemotion Amsterdam 2019Dave Farley - Taking Back “Software Engineering” - Codemotion Amsterdam 2019
Dave Farley - Taking Back “Software Engineering” - Codemotion Amsterdam 2019
 
Joshua Hoffman - Should the CTO be Coding? - Codemotion Amsterdam 2019
Joshua Hoffman - Should the CTO be Coding? - Codemotion Amsterdam 2019Joshua Hoffman - Should the CTO be Coding? - Codemotion Amsterdam 2019
Joshua Hoffman - Should the CTO be Coding? - Codemotion Amsterdam 2019
 
Mike Kotsur - What can philosophy teach us about programming - Codemotion Ams...
Mike Kotsur - What can philosophy teach us about programming - Codemotion Ams...Mike Kotsur - What can philosophy teach us about programming - Codemotion Ams...
Mike Kotsur - What can philosophy teach us about programming - Codemotion Ams...
 
Mete Atamel - Serverless with Knative - Codemotion Amsterdam 2019
Mete Atamel - Serverless with Knative - Codemotion Amsterdam 2019Mete Atamel - Serverless with Knative - Codemotion Amsterdam 2019
Mete Atamel - Serverless with Knative - Codemotion Amsterdam 2019
 
Rahul Shetty - Corporate relocation prediction - Codemotion Amsterdam 2019
Rahul Shetty - Corporate relocation prediction - Codemotion Amsterdam 2019Rahul Shetty - Corporate relocation prediction - Codemotion Amsterdam 2019
Rahul Shetty - Corporate relocation prediction - Codemotion Amsterdam 2019
 
Mario Viviani - Designing apps for fire TV - Codemotion Amsterdam 2019
Mario Viviani - Designing apps for fire TV - Codemotion Amsterdam 2019Mario Viviani - Designing apps for fire TV - Codemotion Amsterdam 2019
Mario Viviani - Designing apps for fire TV - Codemotion Amsterdam 2019
 
Ilona Demidenko - Conversational Sign Up - Codemotion Amsterdam 2019
Ilona Demidenko - Conversational Sign Up - Codemotion Amsterdam 2019Ilona Demidenko - Conversational Sign Up - Codemotion Amsterdam 2019
Ilona Demidenko - Conversational Sign Up - Codemotion Amsterdam 2019
 
Katie Koschland - Ready, steady, crash - Codemotion Amsterdam 2019
Katie Koschland - Ready, steady, crash - Codemotion Amsterdam 2019Katie Koschland - Ready, steady, crash - Codemotion Amsterdam 2019
Katie Koschland - Ready, steady, crash - Codemotion Amsterdam 2019
 

Physics in Android Games: A Guide to Implementing Realistic Gameplay

  • 1. Physics in Android™ games Pär Sikö, Martin Gunnarsson #androidphysics lördag den 5 mars 2011
  • 2. Popularity Time lördag den 5 mars 2011
  • 3. Popularity Time lördag den 5 mars 2011
  • 4. Popularity Time lördag den 5 mars 2011
  • 5. Popularity Time lördag den 5 mars 2011
  • 6. Popularity Time lördag den 5 mars 2011
  • 7. Popularity Time lördag den 5 mars 2011
  • 8. “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime, as well as all related concepts, including energy and force. More broadly, it is the general analysis of nature, conducted in order to understand how the universe behaves” Wikipedia lördag den 5 mars 2011
  • 9. “Physics in Android is when you beat the shit out of all the pigs in Angry Birds” Anonymous Angry Birds addict lördag den 5 mars 2011
  • 10. Physics based games lördag den 5 mars 2011
  • 11. Physics based games lördag den 5 mars 2011
  • 12. Physics based games lördag den 5 mars 2011
  • 13. Physics based games lördag den 5 mars 2011
  • 14. Physics based games lördag den 5 mars 2011
  • 15. Physics based games lördag den 5 mars 2011
  • 16. Physics 101 lördag den 5 mars 2011
  • 17. lördag den 5 mars 2011
  • 18. Theory (for real) lördag den 5 mars 2011
  • 20. Features ► Gravity lördag den 5 mars 2011
  • 21. Features ► Gravity ► Collisions lördag den 5 mars 2011
  • 22. Features ► Gravity ► Collisions ► Joints lördag den 5 mars 2011
  • 23. Features ► Gravity ► Collisions ► Joints ► Extra properties lördag den 5 mars 2011
  • 25. Architecture Java JNI wrapper Native code lördag den 5 mars 2011
  • 26. Physics engines Box2D lördag den 5 mars 2011
  • 27. Physics engines libgdx Box2D lördag den 5 mars 2011
  • 28. Physics engines AndEngine libgdx Box2D lördag den 5 mars 2011
  • 30. public class Example extends BaseGameActivity { public Engine onLoadEngine() { } public void onLoadResources() { } public Scene onLoadScene() { } public void onLoadComplete() { } } lördag den 5 mars 2011
  • 31. onLoadEngine private final static int WIDTH = 480; private final static int HEIGHT = 320; public Engine onLoadEngine() { Camera camera = new Camera(0, 0, WIDTH, HEIGHT); RatioResolutionPolicy ratio = new RatioResolutionPolicy(WIDTH, HEIGHT); EngineOptions options = new EngineOptions(true, ScreenOrientation.LANDSCAPE, ratio, camera); return new Engine(options); } lördag den 5 mars 2011
  • 32. onLoadResources protected Texture texture; protected TextureRegion region; public void onLoadResources() { this.texture = new Texture(256, 32); this.region = TextureRegionFactory.createFromAsset(this.texture, this, "gfx/image.png", 0, 0); this.mEngine.getTextureManager().loadTexture(this.texture); } lördag den 5 mars 2011
  • 33. Textures and regions 2n × 2m px lördag den 5 mars 2011
  • 34. Textures and regions 2n × 2m px lördag den 5 mars 2011
  • 35. onLoadResources protected Texture texture; protected TextureRegion region; public void onLoadResources() { this.texture = new Texture(256, 32); this.region = TextureRegionFactory.createFromAsset(this.texture, this, "gfx/image.png", 0, 0); this.mEngine.getTextureManager().loadTexture(this.texture); } lördag den 5 mars 2011
  • 36. onLoadScene public Scene onLoadScene() { final Scene scene = new Scene(1); scene.setBackground(new ColorBackground(1f, 1f, 1f)); Sprite sprite = new Sprite(WIDTH / 2, HEIGHT / 2, this.region); scene.getTopLayer().addEntity(sprite); return scene; } lördag den 5 mars 2011
  • 37. onLoadComplete public void onLoadComplete() { } lördag den 5 mars 2011
  • 39. public Engine onLoadEngine() { final Camera camera = new Camera(0, 0, WIDTH, HEIGHT); final RatioResolutionPolicy ratio = new RatioResolutionPolicy(camera.getWidth(), camera.getHeight()); return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, ratio, camera)); } public void onLoadResources() { this.texture = new Texture(256, 32); this.tileRegions = TextureRegionFactory.createTiledFromAsset(this.texture, this, "gfx/blocks.png", 0, 0, BLOCKS, 1); this.mEngine.getTextureManager().loadTexture(this.texture); } public Scene onLoadScene() { final Scene scene = new Scene(1); scene.setBackground(new ColorBackground(0, 0, 0)); for (int i = 0; i < BLOCKS; i++) { TiledSprite sprite = new TiledSprite(68 + i * 52, 144, this.tileRegions.clone()); sprite.setCurrentTileIndex(i); scene.getTopLayer().addEntity(sprite); } return scene; } lördag den 5 mars 2011
  • 40. public class Example extends BaseGameActivity { protected PhysicsWorld physicsWorld; protected FixtureDef fixtureDef = PhysicsFactory.createFixtureDef(10, 0.5f, 0.5f); ... lördag den 5 mars 2011
  • 41. onLoadScene public Scene onLoadScene() { final Scene scene = new Scene(1); scene.setBackground(new ColorBackground(0, 0, 0)); physicsWorld = new PhysicsWorld( new Vector2(0, SensorManager.GRAVITY_EARTH), true); for (int i = 0; i < BLOCKS; i++) { TiledSprite sprite = new TiledSprite(68 + i * 52, 144, this.tileRegions.clone()); sprite.setCurrentTileIndex(i); scene.getTopLayer().addEntity(sprite); Body body = PhysicsFactory.createBoxBody(physicsWorld, sprite, BodyType.DynamicBody, fixtureDefinition); this.physicsWorld.registerPhysicsConnector(new PhysicsConnector( sprite, body)); } scene.registerUpdateHandler(physicsWorld); return scene; } lördag den 5 mars 2011
  • 42. onLoadScene final Shape ground = new Rectangle(0, HEIGHT - 2, WIDTH, 2); final Shape roof = new Rectangle(0, 0, WIDTH, 2); final Shape left = new Rectangle(0, 0, 2, HEIGHT); final Shape right = new Rectangle(WIDTH - 2, 0, 2, HEIGHT); PhysicsFactory.createBoxBody(this.physicsWorld, ground, BodyType.StaticBody, this.fixtureDefinition); PhysicsFactory.createBoxBody(this.physicsWorld, roof, BodyType.StaticBody, this.fixtureDefinition); PhysicsFactory.createBoxBody(this.physicsWorld, left, BodyType.StaticBody, this.fixtureDefinition); PhysicsFactory.createBoxBody(this.physicsWorld, right, BodyType.StaticBody, this.fixtureDefinition); scene.getBottomLayer().addEntity(ground); scene.getBottomLayer().addEntity(roof); scene.getBottomLayer().addEntity(left); scene.getBottomLayer().addEntity(right); lördag den 5 mars 2011
  • 43. Accelerometer public class Example extends BaseGameActivity implements IAccelerometerListener { public void onAccelerometerChanged(AccelerometerData data) { this.physicsWorld.setGravity(new Vector2(data.getY(), data.getX())); } public Scene onLoadScene() { this.enableAccelerometerSensor(this); ... } ... lördag den 5 mars 2011
  • 44. Making a game lördag den 5 mars 2011
  • 45. Making a game lördag den 5 mars 2011
  • 46. Making a game ► Load resources ► Load scene ► Collision detection ► Handle touch events lördag den 5 mars 2011
  • 47. onLoadResources public void onLoadResources() { texture = new Texture(256, 256, TextureOptions.BILINEAR); woodRegion = TextureRegionFactory.createFromAsset(texture, this, "gfx/wood_base.png", 0, 0); stoneRegion = TextureRegionFactory.createFromAsset(texture, this, "gfx/stone_base.png", 8, 0); joltRegion = TextureRegionFactory.createFromAsset(texture, this, "gfx/jolt.png", 72, 0); bossRegion = TextureRegionFactory.createFromAsset(texture, this, "gfx/boss.png", 136, 0); ... this.mEngine.getTextureManager().loadTexture(texture); } lördag den 5 mars 2011
  • 48. onLoadScene final Scene scene = new Scene(1); scene.setBackground(new RepeatingSpriteBackground(CAMERA_WIDTH, CAMERA_HEIGHT, mEngine.getTextureManager(), new AssetTextureSource(this, "gfx/background.png"))); scene.setOnSceneTouchListener(this); this.physicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), true, 3, 2); this.physicsWorld.setContactListener(getContactListener(scene)); // Position constants: TRANSLATE1, TRANSLATE2, floor1 ... // First floor add(scene, stoneRegion, TRANSLATE1, floor1, 0); add(scene, stoneRegion, TRANSLATE2, floor1, 0); add(scene, woodRegion, TRANSLATE1 + WMH / 2f, floor1 - WPH / 2f, -90f); ... lördag den 5 mars 2011
  • 50. onLoadScene final Scene scene = new Scene(1); scene.setBackground(new RepeatingSpriteBackground(CAMERA_WIDTH, CAMERA_HEIGHT, mEngine.getTextureManager(), new AssetTextureSource(this, "gfx/background.png"))); scene.setOnSceneTouchListener(this); this.physicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), true, 3, 2); this.physicsWorld.setContactListener(getContactListener(scene)); // Position constants: TRANSLATE1, TRANSLATE2, floor1 ... // First floor add(scene, stoneRegion, TRANSLATE1, floor1, 0); add(scene, stoneRegion, TRANSLATE2, floor1, 0); add(scene, woodRegion, TRANSLATE1 + WMH / 2f, floor1 - WPH / 2f, -90f); ... lördag den 5 mars 2011
  • 51. add(...) Sprite sprite = new Sprite(xTranslate, yTranslate, region.clone()); sprite.setRotation(rotation); scene.getTopLayer().addEntity(sprite); Body body = PhysicsFactory.createBoxBody(physicsWorld, sprite, bodyType, fixtureDefinition); this.physicsWorld.registerPhysicsConnector(new PhysicsConnector(sprite, body)); return body; lördag den 5 mars 2011
  • 52. onLoadScene // Add bosses add(scene, bossRegion, TRANSLATE2 + 50, floor1, 0); add(scene, bossRegion, TRANSLATE1 + 20, floor1, 0); // Add jolt cola to throw at the bosses add(scene, joltRegion, 50, 300, 0); lördag den 5 mars 2011
  • 53. Collision detection private ContactListener getContactListener(final Scene scene) { return new ContactListener() { public void beginContact(Contact contact) { Fixture fixtureA = contact.getFixtureA(); Fixture fixtureB = contact.getFixtureB(); for (final Body bossBody : bosses.keySet()) { Fixture boss = bossBody.getFixtureList().get(0); if (fixtureA == boss || fixtureB == boss) { hitCount++; if (hitCount == 10) { // Remove boss from world and scene } } } } }; } lördag den 5 mars 2011
  • 54. Touch events public boolean onSceneTouchEvent(Scene pScene, TouchEvent touch) { int action = touch.getAction(); if (action == TouchEvent.ACTION_DOWN) { startX = (int) touch.getMotionEvent().getX(); startY = (int) touch.getMotionEvent().getY(); return true; } if (action == TouchEvent.ACTION_UP) { float xDiff = (int) (startX - touch.getMotionEvent().getX()); float yDiff = (int) (startY - touch.getMotionEvent().getY()); can.applyLinearImpulse(new Vector2(2 * xDiff, yDiff), new Vector2(can.getPosition().x, can.getPosition().y)); addNewCan(); return true; } return false; } lördag den 5 mars 2011
  • 56. Performance 60 45 30 15 0 0 50 100 150 HTC Hero X10 Xperia Play lördag den 5 mars 2011
  • 57. Thank you! per.siko@epsilon.nu martin.gunnarsson@epsilon.nu lördag den 5 mars 2011