Advertisement
fishfpg

Launch new intent using physics. See line 147

Oct 6th, 2011
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.09 KB | None | 0 0
  1. import static org.anddev.andengine.extension.physics.box2d.util.constants.PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT;
  2.  
  3. import org.anddev.andengine.engine.Engine;
  4. import org.anddev.andengine.engine.camera.Camera;
  5. import org.anddev.andengine.engine.options.EngineOptions;
  6. import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
  7. import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
  8. import org.anddev.andengine.entity.primitive.Rectangle;
  9. import org.anddev.andengine.entity.scene.Scene;
  10. import org.anddev.andengine.entity.scene.Scene.IOnSceneTouchListener;
  11. import org.anddev.andengine.entity.scene.background.ColorBackground;
  12. import org.anddev.andengine.entity.shape.Shape;
  13. import org.anddev.andengine.entity.sprite.AnimatedSprite;
  14. import org.anddev.andengine.entity.util.FPSLogger;
  15. import org.anddev.andengine.extension.physics.box2d.PhysicsConnector;
  16. import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
  17. import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;
  18. import org.anddev.andengine.extension.physics.box2d.util.Vector2Pool;
  19. import org.anddev.andengine.input.touch.TouchEvent;
  20. import org.anddev.andengine.opengl.texture.TextureOptions;
  21. import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
  22. import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
  23. import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
  24. import org.anddev.andengine.sensor.accelerometer.AccelerometerData;
  25. import org.anddev.andengine.sensor.accelerometer.IAccelerometerListener;
  26. import org.anddev.andengine.util.Debug;
  27.  
  28. import android.content.Intent;
  29. import android.hardware.SensorManager;
  30. import android.widget.Toast;
  31.  
  32. import com.badlogic.gdx.math.Vector2;
  33. import com.badlogic.gdx.physics.box2d.Body;
  34. import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
  35. import com.badlogic.gdx.physics.box2d.FixtureDef;
  36. import com.badlogic.gdx.physics.box2d.PolygonShape;
  37.  
  38. /**
  39.  * (c) 2010 Nicolas Gramlich
  40.  * (c) 2011 Zynga
  41.  *
  42.  * @author Nicolas Gramlich
  43.  * @since 18:47:08 - 19.03.2010
  44.  */
  45. public class PhysicsExample extends BaseExample implements IAccelerometerListener, IOnSceneTouchListener {
  46.     // ===========================================================
  47.     // Constants
  48.     // ===========================================================
  49.  
  50.     private static final int CAMERA_WIDTH = 720;
  51.     private static final int CAMERA_HEIGHT = 480;
  52.  
  53.     private static final FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);
  54.  
  55.     // ===========================================================
  56.     // Fields
  57.     // ===========================================================
  58.  
  59.     private BitmapTextureAtlas mBitmapTextureAtlas;
  60.  
  61.     private TiledTextureRegion mBoxFaceTextureRegion;
  62.     private TiledTextureRegion mCircleFaceTextureRegion;
  63.     private TiledTextureRegion mTriangleFaceTextureRegion;
  64.     private TiledTextureRegion mHexagonFaceTextureRegion;
  65.  
  66.     private Scene mScene;
  67.  
  68.     private PhysicsWorld mPhysicsWorld;
  69.     private int mFaceCount = 0;
  70.  
  71.     // ===========================================================
  72.     // Constructors
  73.     // ===========================================================
  74.  
  75.     // ===========================================================
  76.     // Getter & Setter
  77.     // ===========================================================
  78.  
  79.     // ===========================================================
  80.     // Methods for/from SuperClass/Interfaces
  81.     // ===========================================================
  82.  
  83.     @Override
  84.     public Engine onLoadEngine() {
  85.         Toast.makeText(this, "Touch the screen to add objects.", Toast.LENGTH_LONG).show();
  86.         final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
  87.         final EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
  88.         engineOptions.getTouchOptions().setRunOnUpdateThread(true);
  89.         return new Engine(engineOptions);
  90.     }
  91.  
  92.     @Override
  93.     public void onLoadResources() {
  94.         /* Textures. */
  95.         this.mBitmapTextureAtlas = new BitmapTextureAtlas(64, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
  96.         BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
  97.  
  98.         /* TextureRegions. */
  99.         this.mBoxFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "face_box_tiled.png", 0, 0, 2, 1); // 64x32
  100.         this.mCircleFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "face_circle_tiled.png", 0, 32, 2, 1); // 64x32
  101.         this.mTriangleFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "face_triangle_tiled.png", 0, 64, 2, 1); // 64x32
  102.         this.mHexagonFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "face_hexagon_tiled.png", 0, 96, 2, 1); // 64x32
  103.         this.mEngine.getTextureManager().loadTexture(this.mBitmapTextureAtlas);
  104.     }
  105.  
  106.     @Override
  107.     public Scene onLoadScene() {
  108.         this.mEngine.registerUpdateHandler(new FPSLogger());
  109.  
  110.         this.mScene = new Scene();
  111.         this.mScene.setBackground(new ColorBackground(0, 0, 0));
  112.         this.mScene.setOnSceneTouchListener(this);
  113.  
  114.         this.mPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false);
  115.  
  116.         final Shape ground = new Rectangle(0, CAMERA_HEIGHT - 2, CAMERA_WIDTH, 2);
  117.         final Shape roof = new Rectangle(0, 0, CAMERA_WIDTH, 2);
  118.         final Shape left = new Rectangle(0, 0, 2, CAMERA_HEIGHT);
  119.         final Shape right = new Rectangle(CAMERA_WIDTH - 2, 0, 2, CAMERA_HEIGHT);
  120.  
  121.         final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0.5f, 0.5f);
  122.         PhysicsFactory.createBoxBody(this.mPhysicsWorld, ground, BodyType.StaticBody, wallFixtureDef);
  123.         PhysicsFactory.createBoxBody(this.mPhysicsWorld, roof, BodyType.StaticBody, wallFixtureDef);
  124.         PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyType.StaticBody, wallFixtureDef);
  125.         PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyType.StaticBody, wallFixtureDef);
  126.  
  127.         this.mScene.attachChild(ground);
  128.         this.mScene.attachChild(roof);
  129.         this.mScene.attachChild(left);
  130.         this.mScene.attachChild(right);
  131.  
  132.         this.mScene.registerUpdateHandler(this.mPhysicsWorld);
  133.  
  134.         return this.mScene;
  135.     }
  136.  
  137.     @Override
  138.     public void onLoadComplete() {
  139.  
  140.     }
  141.  
  142.     @Override
  143.     public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent) {
  144.         if(this.mPhysicsWorld != null) {
  145.             if(pSceneTouchEvent.isActionDown()) {
  146.                 this.addFace(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
  147.                 this.startActivity(new Intent(this, PhysicsExample.class));
  148.                 return true;
  149.             }
  150.         }
  151.         return false;
  152.     }
  153.  
  154.     @Override
  155.     public void onAccelerometerChanged(final AccelerometerData pAccelerometerData) {
  156.         final Vector2 gravity = Vector2Pool.obtain(pAccelerometerData.getX(), pAccelerometerData.getY());
  157.         this.mPhysicsWorld.setGravity(gravity);
  158.         Vector2Pool.recycle(gravity);
  159.     }
  160.  
  161.     @Override
  162.     public void onResumeGame() {
  163.         super.onResumeGame();
  164.  
  165.         this.enableAccelerometerSensor(this);
  166.     }
  167.  
  168.     @Override
  169.     public void onPauseGame() {
  170.         super.onPauseGame();
  171.  
  172.         this.disableAccelerometerSensor();
  173.     }
  174.  
  175.     // ===========================================================
  176.     // Methods
  177.     // ===========================================================
  178.  
  179.     private void addFace(final float pX, final float pY) {
  180.         this.mFaceCount++;
  181.         Debug.d("Faces: " + this.mFaceCount);
  182.  
  183.         final AnimatedSprite face;
  184.         final Body body;
  185.  
  186.         if(this.mFaceCount % 4 == 0) {
  187.             face = new AnimatedSprite(pX, pY, this.mBoxFaceTextureRegion);
  188.             body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF);
  189.         } else if (this.mFaceCount % 4 == 1) {
  190.             face = new AnimatedSprite(pX, pY, this.mCircleFaceTextureRegion);
  191.             body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF);
  192.         } else if (this.mFaceCount % 4 == 2) {
  193.             face = new AnimatedSprite(pX, pY, this.mTriangleFaceTextureRegion);
  194.             body = PhysicsExample.createTriangleBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF);
  195.         } else {
  196.             face = new AnimatedSprite(pX, pY, this.mHexagonFaceTextureRegion);
  197.             body = PhysicsExample.createHexagonBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF);
  198.         }
  199.  
  200.         face.animate(200);
  201.  
  202.         this.mScene.attachChild(face);
  203.         this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, body, true, true));
  204.     }
  205.  
  206.     /**
  207.      * Creates a {@link Body} based on a {@link PolygonShape} in the form of a triangle:
  208.      * <pre>
  209.      *  /\
  210.      * /__\
  211.      * </pre>
  212.      */
  213.     private static Body createTriangleBody(final PhysicsWorld pPhysicsWorld, final Shape pShape, final BodyType pBodyType, final FixtureDef pFixtureDef) {
  214.         /* Remember that the vertices are relative to the center-coordinates of the Shape. */
  215.         final float halfWidth = pShape.getWidthScaled() * 0.5f / PIXEL_TO_METER_RATIO_DEFAULT;
  216.         final float halfHeight = pShape.getHeightScaled() * 0.5f / PIXEL_TO_METER_RATIO_DEFAULT;
  217.  
  218.         final float top = -halfHeight;
  219.         final float bottom = halfHeight;
  220.         final float left = -halfHeight;
  221.         final float centerX = 0;
  222.         final float right = halfWidth;
  223.  
  224.         final Vector2[] vertices = {
  225.                 new Vector2(centerX, top),
  226.                 new Vector2(right, bottom),
  227.                 new Vector2(left, bottom)
  228.         };
  229.  
  230.         return PhysicsFactory.createPolygonBody(pPhysicsWorld, pShape, vertices, pBodyType, pFixtureDef);
  231.     }
  232.  
  233.     /**
  234.      * Creates a {@link Body} based on a {@link PolygonShape} in the form of a hexagon:
  235.      * <pre>
  236.      *  /\
  237.      * /  \
  238.      * |  |
  239.      * |  |
  240.      * \  /
  241.      *  \/
  242.      * </pre>
  243.      */
  244.     private static Body createHexagonBody(final PhysicsWorld pPhysicsWorld, final Shape pShape, final BodyType pBodyType, final FixtureDef pFixtureDef) {
  245.         /* Remember that the vertices are relative to the center-coordinates of the Shape. */
  246.         final float halfWidth = pShape.getWidthScaled() * 0.5f / PIXEL_TO_METER_RATIO_DEFAULT;
  247.         final float halfHeight = pShape.getHeightScaled() * 0.5f / PIXEL_TO_METER_RATIO_DEFAULT;
  248.  
  249.         /* The top and bottom vertex of the hexagon are on the bottom and top of hexagon-sprite. */
  250.         final float top = -halfHeight;
  251.         final float bottom = halfHeight;
  252.  
  253.         final float centerX = 0;
  254.  
  255.         /* The left and right vertices of the heaxgon are not on the edge of the hexagon-sprite, so we need to inset them a little. */
  256.         final float left = -halfWidth + 2.5f / PIXEL_TO_METER_RATIO_DEFAULT;
  257.         final float right = halfWidth - 2.5f / PIXEL_TO_METER_RATIO_DEFAULT;
  258.         final float higher = top + 8.25f / PIXEL_TO_METER_RATIO_DEFAULT;
  259.         final float lower = bottom - 8.25f / PIXEL_TO_METER_RATIO_DEFAULT;
  260.  
  261.         final Vector2[] vertices = {
  262.                 new Vector2(centerX, top),
  263.                 new Vector2(right, higher),
  264.                 new Vector2(right, lower),
  265.                 new Vector2(centerX, bottom),
  266.                 new Vector2(left, lower),
  267.                 new Vector2(left, higher)
  268.         };
  269.  
  270.         return PhysicsFactory.createPolygonBody(pPhysicsWorld, pShape, vertices, pBodyType, pFixtureDef);
  271.     }
  272.  
  273.     // ===========================================================
  274.     // Inner and Anonymous Classes
  275.     // ===========================================================
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement