physics in android™ games · “physics (from ancient greek: φύσις physis...
TRANSCRIPT
![Page 1: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/1.jpg)
Physics in Android™ gamesPär Sikö, Martin Gunnarsson
#androidphysics
![Page 2: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/2.jpg)
Pop
ular
ity
Time
![Page 3: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/3.jpg)
Pop
ular
ity
Time
![Page 4: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/4.jpg)
Pop
ular
ity
Time
![Page 5: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/5.jpg)
Pop
ular
ity
Time
![Page 6: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/6.jpg)
Pop
ular
ity
Time
![Page 7: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/7.jpg)
Pop
ular
ity
Time
![Page 8: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/8.jpg)
“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
![Page 9: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/9.jpg)
“Physics in Android is when you beat the shit out of all the pigs in Angry Birds”
Anonymous Angry Birds addict
![Page 10: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/10.jpg)
Physics based games
![Page 11: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/11.jpg)
Physics based games
![Page 12: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/12.jpg)
Physics based games
![Page 13: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/13.jpg)
Physics based games
![Page 14: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/14.jpg)
Physics based games
![Page 15: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/15.jpg)
Physics based games
![Page 16: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/16.jpg)
Physics 101
![Page 17: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/17.jpg)
![Page 18: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/18.jpg)
Theory(for real)
![Page 19: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/19.jpg)
Features
![Page 20: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/20.jpg)
Features
►Gravity
![Page 21: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/21.jpg)
Features
►Gravity►Collisions
![Page 22: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/22.jpg)
Features
►Gravity►Collisions►Joints
![Page 23: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/23.jpg)
Features
►Gravity►Collisions►Joints►Extra properties
![Page 24: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/24.jpg)
Differences
2D graphics Physics engine
![Page 25: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/25.jpg)
Differences
2D graphics Physics engine
![Page 26: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/26.jpg)
Differences
2D graphics Physics engine
![Page 27: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/27.jpg)
Differences
2D graphics Physics engine
80px
![Page 28: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/28.jpg)
Differences
2D graphics Physics engine
80px 4m
![Page 29: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/29.jpg)
Physics Engines
![Page 30: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/30.jpg)
JNI wrapper
Architecture
Native code
Java
![Page 31: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/31.jpg)
Physics engines
Box2D
![Page 32: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/32.jpg)
Physics engines
Box2DJBox2D
![Page 33: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/33.jpg)
Physics engines
Box2DJBox2D
libgdx
![Page 34: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/34.jpg)
Physics engines
Box2DJBox2D
AndEngine
libgdx
![Page 35: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/35.jpg)
AndEngine
![Page 36: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/36.jpg)
public class Example extends BaseGameActivity {
public Engine onLoadEngine() { }
public void onLoadResources() { }
public Scene onLoadScene() { }
public void onLoadComplete() { }}
![Page 37: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/37.jpg)
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);}
onLoadEngine
![Page 38: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/38.jpg)
protected Texture texture;protected TextureRegion region;
public void onLoadResources() { this.texture = new Texture(256, 32); this.region = TextureRegionFactory.createFromAsset(this.texture, this,
"gfx/anchor.png", 0, 0);
this.mEngine.getTextureManager().loadTexture(this.texture);}
onLoadResources
![Page 39: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/39.jpg)
Textures and regions
2n × 2n px
![Page 40: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/40.jpg)
Textures and regions
2n × 2n px
![Page 41: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/41.jpg)
protected Texture texture;protected TextureRegion region;
public void onLoadResources() { this.texture = new Texture(256, 32); this.region = TextureRegionFactory.createFromAsset(this.texture, this,
"gfx/jman.png", 0, 0);
this.mEngine.getTextureManager().loadTexture(this.texture);}
onLoadResources
![Page 42: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/42.jpg)
public Scene onLoadScene() { final Scene scene = new Scene(1); scene.setBackground(new ColorBackground(0, 0, 0));
Sprite sprite = new Sprite(WIDTH / 2, HEIGHT / 2, this.region); scene.getTopLayer().addEntity(sprite);
return scene;}
onLoadScene
![Page 43: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/43.jpg)
public void onLoadComplete() {
}
onLoadComplete
![Page 44: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/44.jpg)
Adding physics
![Page 45: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/45.jpg)
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/jfokus.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(94 + i * 52, 144, this.tileRegions.clone()); sprite.setCurrentTileIndex(i); scene.getTopLayer().addEntity(sprite); }
return scene;}
![Page 46: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/46.jpg)
public class Example extends BaseGameActivity {
protected PhysicsWorld physicsWorld;protected FixtureDef fixtureDef = PhysicsFactory.createFixtureDef(10, 0.5f, 0.5f);
...
![Page 47: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/47.jpg)
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(94 + 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;}
onLoadScene
![Page 48: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/48.jpg)
onLoadScenefinal 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);
![Page 49: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/49.jpg)
Accelerometerpublic class Example extends BaseGameActivity implements IAccelerometerListener {
public Scene onLoadScene() { this.enableAccelerometerSensor(this);
... }
public void onAccelerometerChanged(AccelerometerData data) { this.physicsWorld.setGravity(new Vector2(data.getY(), data.getX())); }
...
![Page 50: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/50.jpg)
onLoadScenefor (int i = 0; i < BLOCKS; i++) {
// Create blockTiledSprite block = new TiledSprite(x, 144, this.tileRegions.clone());block.setCurrentTileIndex(i);scene.getTopLayer().addEntity(block);
Body blockBody = PhysicsFactory.createBoxBody(physicsWorld, block,BodyType.DynamicBody, fixtureDefinition);physicsWorld.registerPhysicsConnector(new PhysicsConnector(block, blockBody));
...
![Page 51: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/51.jpg)
onLoadScene...
// Create fixed point TiledSprite fixed = new TiledSprite(x, 10, anchorRegion); scene.getTopLayer().addEntity(fixed);
Body fixedBody = PhysicsFactory.createBoxBody(physicsWorld, fixed, BodyType.StaticBody, fixtureDefinition); physicsWorld.registerPhysicsConnector(new PhysicsConnector(fixed, fixedBody));
// Create joint RevoluteJointDef revoluteJointDef = new RevoluteJointDef(); revoluteJointDef.initialize(fixedBody, blockBody, fixedBody.getWorldCenter());
physicsWorld.createJoint(revoluteJointDef);}
![Page 52: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/52.jpg)
Making a game
![Page 53: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/53.jpg)
Making a game
►Load resources►Load scene►Collision detection►Handle touch events
![Page 54: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/54.jpg)
onLoadResourcespublic 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);
}
![Page 55: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/55.jpg)
onLoadScenefinal 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 flooradd(scene, stoneRegion, TRANSLATE1, floor1, 0);add(scene, stoneRegion, TRANSLATE2, floor1, 0);add(scene, woodRegion, TRANSLATE1 + WMH / 2f, floor1 - WPH / 2f, -90f);...
![Page 56: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/56.jpg)
onLoadScene
![Page 57: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/57.jpg)
onLoadScenefinal 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 flooradd(scene, stoneRegion, TRANSLATE1, floor1, 0);add(scene, stoneRegion, TRANSLATE2, floor1, 0);add(scene, woodRegion, TRANSLATE1 + WMH / 2f, floor1 - WPH / 2f, -90f);...
![Page 58: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/58.jpg)
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;
![Page 59: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/59.jpg)
onLoadScene// Add bossesadd(scene, bossRegion, TRANSLATE2 + 50, floor1, 0);add(scene, bossRegion, TRANSLATE1 + 20, floor1, 0);
// Add jolt cola to throw at the bossesadd(scene, joltRegion, 50, 300, 0);
![Page 60: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/60.jpg)
Collision detectionprivate 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 } } } } };}
![Page 61: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/61.jpg)
Touch eventspublic 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;}
![Page 62: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/62.jpg)
Performance
![Page 63: Physics in Android™ games · “Physics (from Ancient Greek: φύσις physis "nature") is a natural science that involves the study of matter and its motion through spacetime,](https://reader030.vdocuments.us/reader030/viewer/2022040408/5eb5098738fea373605445b8/html5/thumbnails/63.jpg)
Performance
0
15
30
45
60
0 50 100 150
HTC Hero X10 Xperia Play