• AndEngine学习:PhysicsCollisionFiltering(有过滤器的碰撞检测)


      这一篇很简单,应该先看完我写的另外一篇http://www.cnblogs.com/xiaobo68688/archive/2011/10/31/2230270.html后再读这一篇

    // 声明:只是对AndEngine自带的例子做了修改和少许的改动,并不是完全原创,勿喷!
    public class PhysicsCollisionFiltering extends BaseExample implements IAccelerometerListener, IOnSceneTouchListener
    {
    // ===========================================================
    // Constants
    // ===========================================================

    private static final int CAMERA_WIDTH = 720;
    private static final int CAMERA_HEIGHT = 480;

    // 种类常量
    // 为了区分是否应该对碰撞作出反应
    public static final short CATEGORYBIT_WALL = 1;
    public static final short CATEGORYBIT_BOX = 2;
    public static final short CATEGORYBIT_CIRCLE = 4;

    // 这里记录了应该和什么碰撞
    public static final short MASKBITS_WALL = CATEGORYBIT_WALL + CATEGORYBIT_BOX + CATEGORYBIT_CIRCLE;
    public static final short MASKBITS_BOX = CATEGORYBIT_WALL + CATEGORYBIT_BOX; // Missing: CATEGORYBIT_CIRCLE
    public static final short MASKBITS_CIRCLE = CATEGORYBIT_WALL + CATEGORYBIT_CIRCLE; // Missing: CATEGORYBIT_BOX

    // 注册谁和谁应该碰撞的信息
    public static final FixtureDef WALL_FIXTURE_DEF = PhysicsFactory.createFixtureDef(0, 0.5f, 0.5f, false, CATEGORYBIT_WALL, MASKBITS_WALL, (short)0);
    public static final FixtureDef BOX_FIXTURE_DEF = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f, false, CATEGORYBIT_BOX, MASKBITS_BOX, (short)0);
    public static final FixtureDef CIRCLE_FIXTURE_DEF = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f, false, CATEGORYBIT_CIRCLE, MASKBITS_CIRCLE, (short)0);

    // ===========================================================
    // Fields
    // ===========================================================

    private BitmapTextureAtlas mBitmapTextureAtlas;

    private TiledTextureRegion mBoxFaceTextureRegion;
    private TiledTextureRegion mCircleFaceTextureRegion;

    private Scene mScene;

    private PhysicsWorld mPhysicsWorld;
    private int mFaceCount = 0;

    // ===========================================================
    // Constructors
    // ===========================================================

    // ===========================================================
    // Getter & Setter
    // ===========================================================

    // ===========================================================
    // Methods for/from SuperClass/Interfaces
    // ===========================================================

    public Engine onLoadEngine() {
    Toast.makeText(this, "Touch the screen to add objects.", Toast.LENGTH_LONG).show();
    Toast.makeText(this, "Boxes will only collide with boxes.\nCircles will only collide with circles.", Toast.LENGTH_LONG).show();
    final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
    final EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
    engineOptions.getTouchOptions().setRunOnUpdateThread(true);
    return new Engine(engineOptions);
    }

    public void onLoadResources() {
    /* Textures. */
    this.mBitmapTextureAtlas = new BitmapTextureAtlas(64, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

    /* TextureRegions. */
    this.mBoxFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "face_box_tiled.png", 0, 0, 2, 1); // 64x32
    this.mCircleFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "face_circle_tiled.png", 0, 32, 2, 1); // 64x32
    this.mEngine.getTextureManager().loadTexture(this.mBitmapTextureAtlas);
    }

    public Scene onLoadScene() {
    this.mEngine.registerUpdateHandler(new FPSLogger());

    this.mScene = new Scene();
    this.mScene.setBackground(new ColorBackground(0, 0, 0));
    this.mScene.setOnSceneTouchListener(this);

    this.mPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false);

    final Shape ground = new Rectangle(0, CAMERA_HEIGHT - 2, CAMERA_WIDTH, 2);
    final Shape roof = new Rectangle(0, 0, CAMERA_WIDTH, 2);
    final Shape left = new Rectangle(0, 0, 2, CAMERA_HEIGHT);
    final Shape right = new Rectangle(CAMERA_WIDTH - 2, 0, 2, CAMERA_HEIGHT);

    PhysicsFactory.createBoxBody(this.mPhysicsWorld, ground, BodyType.StaticBody, WALL_FIXTURE_DEF);
    PhysicsFactory.createBoxBody(this.mPhysicsWorld, roof, BodyType.StaticBody, WALL_FIXTURE_DEF);
    PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyType.StaticBody, WALL_FIXTURE_DEF);
    PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyType.StaticBody, WALL_FIXTURE_DEF);

    this.mScene.attachChild(ground);
    this.mScene.attachChild(roof);
    this.mScene.attachChild(left);
    this.mScene.attachChild(right);

    this.mScene.registerUpdateHandler(this.mPhysicsWorld);

    return this.mScene;
    }

    public void onLoadComplete() {

    }

    public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent) {
    if(this.mPhysicsWorld != null) {
    if(pSceneTouchEvent.isActionDown()) {
    this.addFace(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
    return true;
    }
    }
    return false;
    }

    public void onAccelerometerChanged(final AccelerometerData pAccelerometerData) {
    final Vector2 gravity = Vector2Pool.obtain(pAccelerometerData.getX(), pAccelerometerData.getY());
    this.mPhysicsWorld.setGravity(gravity);
    Vector2Pool.recycle(gravity);
    }

    @Override
    public void onResumeGame() {
    super.onResumeGame();

    this.enableAccelerometerSensor(this);
    }

    @Override
    public void onPauseGame() {
    super.onPauseGame();

    this.disableAccelerometerSensor();
    }

    // ===========================================================
    // Methods
    // ===========================================================

    private void addFace(final float pX, final float pY) {
    this.mFaceCount++;
    Debug.d("Faces: " + this.mFaceCount);

    final AnimatedSprite face;
    final Body body;

    if(this.mFaceCount % 2 == 0) {
    face = new AnimatedSprite(pX, pY, this.mBoxFaceTextureRegion);
    body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, BOX_FIXTURE_DEF);
    } else {
    face = new AnimatedSprite(pX, pY, this.mCircleFaceTextureRegion);
    body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, face, BodyType.DynamicBody, CIRCLE_FIXTURE_DEF);
    }

    face.animate(200);

    this.mScene.attachChild(face);
    this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, body, true, true));
    }

    // ===========================================================
    // Inner and Anonymous Classes
    // ===========================================================
    }



  • 相关阅读:
    每日英语:Easing Gaokao Restrictions: a New Year's Gift?
    每日英语:Targeting Grandpa: China’s Seniors Hunger for Ads
    每日英语:Eight Questions: Bianca Bosker on China's 'Original Copies' in Architecture
    每日英语:There's No Avoiding Google+
    如何删除控制面板中没有"删除"菜单的程序
    html中利用javascript实现文本框字数的动态计算
    一条批处理语句ping局域网内的所有机器
    javascript中的"+"运算
    在asp.net中使用分帧页面时,ajax control报access denied错误的解决方法
    gridview和repeater中取得行的序号
  • 原文地址:https://www.cnblogs.com/xiaobo68688/p/2230325.html
Copyright © 2020-2023  润新知