一种方式墙壁andengine [英] One way walls in andengine

查看:226
本文介绍了一种方式墙壁andengine的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何创建andengine即单向墙壁时从顶部的身体秋天底在墙上,身体应与墙壁碰撞,当从底部去到顶部,它忽略了碰撞,让身体穿过壁?
我学下面的教程 http://www.iforce2d.net/b2dtut/one-way-walls 并试图在Android中,但没有努力实现这一点。以下是我迄今所做的:

 公共类JumpingBallActivity扩展SimpleBaseGameActivity实现IOnSceneTouchListener,IAccelerationListener,ContactListener {    私人INT CAMERA_WIDTH = 480;
    私人INT CAMERA_HEIGHT = 800;
    私人BitmapTextureAtlas mBitmapTextureAtlas;
    私人ITextureRegion smileyRegion;
    私人场景MSCENE;
    私人相机拍照;
    私人雪碧smileySprite;
    私营机构smileyBody;
    私人PhysicsWorld mPhysicsWorld;
    私营机构slabWallBody;
    私人FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(0.6f,0.0,0.0);
    最后FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(1,0.0,0.0);
    @覆盖
    公共EngineOptions onCreateEngineOptions(){
        // TODO自动生成方法存根        相机=新照相机(0,0,CAMERA_WIDTH,CAMERA_HEIGHT);        返回新EngineOptions(真,ScreenOrientation.PORTRAIT_FIXED,新RatioResolutionPolicy(CAMERA_WIDTH,CAMERA_HEIGHT),摄像头);
    }    @覆盖
    保护无效onCreateResources(){
        // TODO自动生成方法存根
        BitmapTextureAtlasTextureRegionFactory.setAssetBasePath(GFX /);        mBitmapTextureAtlas =新BitmapTextureAtlas(this.getTextureManager(),84,84,TextureOptions.BILINEAR);
        smileyRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mBitmapTextureAtlas,getAssets(),smiley.png,0,0);
        mBitmapTextureAtlas.load();
        enableAccelerationSensor(本);    }    @覆盖
    保护场景onCreateScene(){
        // TODO自动生成方法存根
        this.mScene =新场景();
        this.mScene.setBackground(新背景(0,0,0));
        this.mScene.setOnSceneTouchListener(本);
        this.mPhysicsWorld =新PhysicsWorld(新Vector2(0,15),FALSE);
        最终VertexBufferObjectManager vertexBufferObjectManager = this.getVertexBufferObjectManager();
        矩形Rect1的=新矩形((CAMERA_WIDTH / 2) - 100,(CAMERA_HEIGHT / 2)+ 50,200,10,vertexBufferObjectManager);
        slabWallBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld,Rect1的,BodyType.StaticBody,wallFixtureDef);
        slabWallBody.setUserData(板);
        mPhysicsWorld.registerPhysicsConnector(新PhysicsConnector(Rect1的,slabWallBody,假的,假的));
        smileySprite =新的雪碧(smileyRegion.getWidth(),smileyRegion.getHeight(),smileyRegion,vertexBufferObjectManager);
        smileyBody = PhysicsFactory.createCircleBody(mPhysicsWorld,smileySprite,BodyType.DynamicBody,FIXTURE_DEF);
        smileyBody.setUserData(笑脸);
        mPhysicsWorld.registerPhysicsConnector(新PhysicsConnector(smileySprite,smileyBody,真,假));
        最终的矩形地面=新的Rectangle(0,CAMERA_HEIGHT - 2,CAMERA_WIDTH,2,vertexBufferObjectManager);
        最终矩形屋顶=新的Rectangle(0,0,CAMERA_WIDTH,2,vertexBufferObjectManager);
        最后矩形左=新的Rectangle(0,0,2,CAMERA_HEIGHT,vertexBufferObjectManager);
        最后矩形右=新的Rectangle(CAMERA_WIDTH - 2,0,2,CAMERA_HEIGHT,vertexBufferObjectManager);        PhysicsFactory.createBoxBody(this.mPhysicsWorld,地面,BodyType.StaticBody,wallFixtureDef).setUserData(墙);
        PhysicsFactory.createBoxBody(this.mPhysicsWorld,屋顶,BodyType.StaticBody,wallFixtureDef).setUserData(墙);
        PhysicsFactory.createBoxBody(this.mPhysicsWorld,左,BodyType.StaticBody,wallFixtureDef).setUserData(墙);
        PhysicsFactory.createBoxBody(this.mPhysicsWorld,右,BodyType.StaticBody,wallFixtureDef).setUserData(墙);        mScene.attachChild(smileySprite);
        mScene.attachChild(Rect1的);
        mScene.attachChild(右);
        mScene.attachChild(左);
        mScene.attachChild(屋顶);
        mScene.attachChild(地);        this.mScene.registerUpdateHandler(this.mPhysicsWorld);
        mPhysicsWorld.setContactListener(本);
        mScene.setOnSceneTouchListener(本);
        返回MSCENE;
    }    @覆盖
    公共布尔onSceneTouchEvent(场景pScene,的TouchEvent pSceneTouchEvent){
        // TODO自动生成方法存根
        smileyBody.setLinearVelocity(0,-15);
        返回false;
    }    @覆盖
    公共无效onAccelerationAccuracyChanged(AccelerationData pAccelerationData){
        // TODO自动生成方法存根    }    @覆盖
    公共无效onAccelerationChanged(AccelerationData pAccelerationData){
        // TODO自动生成方法存根
        最终Vector2比重= Vector2Pool.obtain(0,15);
        this.mPhysicsWorld.setGravity(重力);        Vector2Pool.recycle(重力);        smileyBody.setLinearVelocity(pAccelerationData.getX()* 2,smileyBody.getLinearVelocity()Y。);
    }    @覆盖
    公共无效beginContact(联系方式联系我们){
        // TODO自动生成方法存根
// Log.d(开始接触,开始接触);
// Log.d(开始接触,smileyBody.getLinearVelocity()的toString()+);        夹具fixtureA = contact.getFixtureA();
        夹具fixtureB = contact.getFixtureB();        夹具platFormFixture = NULL;
        夹具ballFixture = NULL;
        //也弄不明白什么怎么回事在code挺身而出
        如果(fixtureA.getBody()getUserData()等于(板)及。&放大器; fixtureB.getBody()getUserData()等于(笑脸)){
            platFormFixture = fixtureA;
            ballFixture = fixtureB;        }        如果(fixtureA.getBody()getUserData()等于(笑脸)及。&放大器; fixtureB.getBody()getUserData()等于(板)。。){
            platFormFixture = fixtureB;
            ballFixture = fixtureA;
        }
        如果(platFormFixture == NULL){
            返回;
        }        体platFormBody = platFormFixture.getBody();
        体ballBody = ballFixture.getBody();        INT为NumPoints = contact.getWorldManifold()getNumberOfContactPoints()。
        WorldManifold worldManiFold = contact.getWorldManifold();        的for(int i = 0; I<为NumPoints;我++){
            Vector2 vector2 = ballBody.getLinearVelocityFromWorldPoint(worldManiFold.getPoints()[我]);
            Vector2的Vector3 = platFormBody.getLinearVelocityFromWorldPoint(worldManiFold.getPoints()[我]);
            Vector2的Vector4 = platFormBody.getLocalVector(vector2.sub(的Vector3));
            如果(vector4.y< -1){
// Log.d(碰撞忽略,碰撞忽略);
                返回;
            }
            否则如果(vector4.y&所述; 1){
                Vector2 localPoint = platFormBody.getLocalPoint(worldManiFold.getPoints()[我]);
                Log.d(局部Y,localPoint.y +);
                浮动F = 1.1F;
                如果(localPoint.y>的F - 0.05f){
                    返回;
                }
            }
        }        contact.setEnabled(假);    }    @覆盖
    公共无效endContact(联系方式联系我们){
        // TODO自动生成方法存根
        contact.setEnabled(真);
// Log.d(端部接触,端部接触);
// Log.d(端部接触,smileyBody.getLinearVelocity()的toString());
    }    Vector2伏;    @覆盖
    公共无效preSOLVE(联系方式联系我们,集成块oldManifold){
        // TODO自动生成方法存根
// Log.d(preSOLVE,preSOLVE);
// Log.d(preSOLVE,smileyBody.getLinearVelocity()的toString());    }    @覆盖
    公共无效postSolve(联系方式联系我们,ContactImpulse冲动){
        // TODO自动生成方法存根
// Log.d(postsolve,postsolve);
// Log.d(postsolve,smileyBody.getLinearVelocity()的toString());
    }}


解决方案

我已经实现所需的功能和这里给出的解决方案,code。我不知道它是如何工作的,但现在它的工作。

 公共类JumpingBallActivity扩展SimpleBaseGameActivity实现IOnSceneTouchListener,IAccelerationListener,ContactListener {    私人INT CAMERA_WIDTH = 480;
    私人INT CAMERA_HEIGHT = 800;
    私人BitmapTextureAtlas mBitmapTextureAtlas;
    私人ITextureRegion smileyRegion;
    私人场景MSCENE;
    私人相机拍照;
    私人雪碧smileySprite;
    私营机构smileyBody;
    私人PhysicsWorld mPhysicsWorld;
    私营机构slabWallBody;
    私人FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(0.6f,0.0,0.0);
    最后FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(1,0.0,0.0);
    @覆盖
    公共EngineOptions onCreateEngineOptions(){
        // TODO自动生成方法存根        相机=新照相机(0,0,CAMERA_WIDTH,CAMERA_HEIGHT);        返回新EngineOptions(真,ScreenOrientation.PORTRAIT_FIXED,新RatioResolutionPolicy(CAMERA_WIDTH,CAMERA_HEIGHT),摄像头);
    }    @覆盖
    保护无效onCreateResources(){
        // TODO自动生成方法存根
        BitmapTextureAtlasTextureRegionFactory.setAssetBasePath(GFX /);        mBitmapTextureAtlas =新BitmapTextureAtlas(this.getTextureManager(),84,84,TextureOptions.BILINEAR);
        smileyRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mBitmapTextureAtlas,getAssets(),smiley.png,0,0);
        mBitmapTextureAtlas.load();
        enableAccelerationSensor(本);    }    @覆盖
    保护场景onCreateScene(){
        // TODO自动生成方法存根
        this.mScene =新场景();
        this.mScene.setBackground(新背景(0,0,0));
        this.mScene.setOnSceneTouchListener(本);
        this.mPhysicsWorld =新PhysicsWorld(新Vector2(0,15),FALSE);
        最终VertexBufferObjectManager vertexBufferObjectManager = this.getVertexBufferObjectManager();
        矩形Rect1的=新矩形((CAMERA_WIDTH / 2) - 100,(CAMERA_HEIGHT / 2)+ 50,200,10,vertexBufferObjectManager);
        slabWallBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld,Rect1的,BodyType.StaticBody,wallFixtureDef);
        slabWallBody.setUserData(板);
        mPhysicsWorld.registerPhysicsConnector(新PhysicsConnector(Rect1的,slabWallBody,假的,假的));
        smileySprite =新的雪碧(smileyRegion.getWidth(),smileyRegion.getHeight(),smileyRegion,vertexBufferObjectManager);
        smileyBody = PhysicsFactory.createCircleBody(mPhysicsWorld,smileySprite,BodyType.DynamicBody,FIXTURE_DEF);
        smileyBody.setUserData(笑脸);
        mPhysicsWorld.registerPhysicsConnector(新PhysicsConnector(smileySprite,smileyBody,真,假));
        最终的矩形地面=新的Rectangle(0,CAMERA_HEIGHT - 2,CAMERA_WIDTH,2,vertexBufferObjectManager);
        最终矩形屋顶=新的Rectangle(0,0,CAMERA_WIDTH,2,vertexBufferObjectManager);
        最后矩形左=新的Rectangle(0,0,2,CAMERA_HEIGHT,vertexBufferObjectManager);
        最后矩形右=新的Rectangle(CAMERA_WIDTH - 2,0,2,CAMERA_HEIGHT,vertexBufferObjectManager);        PhysicsFactory.createBoxBody(this.mPhysicsWorld,地面,BodyType.StaticBody,wallFixtureDef).setUserData(墙);
        PhysicsFactory.createBoxBody(this.mPhysicsWorld,屋顶,BodyType.StaticBody,wallFixtureDef).setUserData(墙);
        PhysicsFactory.createBoxBody(this.mPhysicsWorld,左,BodyType.StaticBody,wallFixtureDef).setUserData(墙);
        PhysicsFactory.createBoxBody(this.mPhysicsWorld,右,BodyType.StaticBody,wallFixtureDef).setUserData(墙);        mScene.attachChild(smileySprite);
        mScene.attachChild(Rect1的);
        mScene.attachChild(右);
        mScene.attachChild(左);
        mScene.attachChild(屋顶);
        mScene.attachChild(地);        this.mScene.registerUpdateHandler(this.mPhysicsWorld);
        mPhysicsWorld.setContactListener(本);
        mScene.setOnSceneTouchListener(本);
        返回MSCENE;
    }    @覆盖
    公共布尔onSceneTouchEvent(场景pScene,的TouchEvent pSceneTouchEvent){
        // TODO自动生成方法存根
        smileyBody.setLinearVelocity(0,-15);
        返回false;
    }    @覆盖
    公共无效onAccelerationAccuracyChanged(AccelerationData pAccelerationData){
        // TODO自动生成方法存根    }    @覆盖
    公共无效onAccelerationChanged(AccelerationData pAccelerationData){
        // TODO自动生成方法存根
        最终Vector2比重= Vector2Pool.obtain(0,15);
        this.mPhysicsWorld.setGravity(重力);        Vector2Pool.recycle(重力);        smileyBody.setLinearVelocity(pAccelerationData.getX()* 2,smileyBody.getLinearVelocity()Y。);
    }    @覆盖
    公共无效beginContact(联系方式联系我们){
        // TODO自动生成方法存根
// Log.d(开始接触,开始接触);
// Log.d(开始接触,smileyBody.getLinearVelocity()的toString()+);    }    @覆盖
    公共无效endContact(联系方式联系我们){
        // TODO自动生成方法存根
        contact.setEnabled(真);
// Log.d(端部接触,端部接触);
// Log.d(端部接触,smileyBody.getLinearVelocity()的toString());
    }    Vector2伏;    @覆盖
    公共无效preSOLVE(联系方式联系我们,集成块oldManifold){
        // TODO自动生成方法存根
// Log.d(preSOLVE,preSOLVE);
// Log.d(preSOLVE,smileyBody.getLinearVelocity()的toString());
        夹具fixtureA = contact.getFixtureA();
        夹具fixtureB = contact.getFixtureB();        夹具platFormFixture = NULL;
        夹具ballFixture = NULL;        如果(fixtureA.getBody()getUserData()等于(板)及。&放大器; fixtureB.getBody()getUserData()等于(笑脸)){
            platFormFixture = fixtureA;
            ballFixture = fixtureB;        }        如果(fixtureA.getBody()getUserData()等于(笑脸)及。&放大器; fixtureB.getBody()getUserData()等于(板)。。){
            platFormFixture = fixtureB;
            ballFixture = fixtureA;
        }
        如果(platFormFixture == NULL){
            返回;
        }        体platFormBody = platFormFixture.getBody();
        体ballBody = ballFixture.getBody();        WorldManifold worldManiFold = contact.getWorldManifold();
        INT为NumPoints = contact.getWorldManifold()getNumberOfContactPoints()。
        的for(int i = 0; I<为NumPoints;我++){
            Vector2 vector2 = ballBody.getLinearVelocityFromWorldPoint(worldManiFold.getPoints()[我]);
            如果(vector2.y℃,){
                contact.setEnabled(假);
            }
        }// WorldManifold worldManiFold = contact.getWorldManifold();
// Vector2正常= worldManiFold.getNormal();
//
//如果(normal.y!= -1){
// contact.setEnabled(假);
//}
    }    @覆盖
    公共无效postSolve(联系方式联系我们,ContactImpulse冲动){
        // TODO自动生成方法存根
// Log.d(postsolve,postsolve);
// Log.d(postsolve,smileyBody.getLinearVelocity()的toString());
    }}

How can we create one way walls in andengine i.e when a body fall from top to bottom on a wall, the body should collide with wall and when from going from bottom to top, it ignores the collision and allow body to pass through wall? I studied following tutorial http://www.iforce2d.net/b2dtut/one-way-walls and tried to implement this in android but not working. Here is what I have done so far:

public class JumpingBallActivity extends SimpleBaseGameActivity implements IOnSceneTouchListener, IAccelerationListener, ContactListener {

    private int CAMERA_WIDTH = 480;
    private int CAMERA_HEIGHT = 800;
    private BitmapTextureAtlas mBitmapTextureAtlas;
    private ITextureRegion smileyRegion;
    private Scene mScene;
    private Camera camera;
    private Sprite smileySprite;
    private Body smileyBody;
    private PhysicsWorld mPhysicsWorld;
    private Body slabWallBody;
    private FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(0.6f, 0.0f, 0.0f);
    final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(1, 0.0f, 0.0f);


    @Override
    public EngineOptions onCreateEngineOptions() {
        // TODO Auto-generated method stub

        camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

        return new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
    }

    @Override
    protected void onCreateResources() {
        // TODO Auto-generated method stub
        BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

        mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 84, 84, TextureOptions.BILINEAR);
        smileyRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mBitmapTextureAtlas, getAssets(), "smiley.png", 0, 0);
        mBitmapTextureAtlas.load();
        enableAccelerationSensor(this);

    }

    @Override
    protected Scene onCreateScene() {
        // TODO Auto-generated method stub
        this.mScene = new Scene();
        this.mScene.setBackground(new Background(0, 0, 0));
        this.mScene.setOnSceneTouchListener(this);
        this.mPhysicsWorld = new PhysicsWorld(new Vector2(0, 15), false);


        final VertexBufferObjectManager vertexBufferObjectManager = this.getVertexBufferObjectManager();
        Rectangle rect1 = new Rectangle((CAMERA_WIDTH/2) - 100, (CAMERA_HEIGHT/2) + 50, 200, 10, vertexBufferObjectManager);
        slabWallBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, rect1, BodyType.StaticBody, wallFixtureDef);
        slabWallBody.setUserData("slab");
        mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(rect1, slabWallBody, false, false));


        smileySprite = new Sprite(smileyRegion.getWidth(), smileyRegion.getHeight(), smileyRegion, vertexBufferObjectManager);
        smileyBody = PhysicsFactory.createCircleBody(mPhysicsWorld, smileySprite,  BodyType.DynamicBody, FIXTURE_DEF);
        smileyBody.setUserData("smiley");
        mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(smileySprite, smileyBody, true, false));


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

        PhysicsFactory.createBoxBody(this.mPhysicsWorld, ground, BodyType.StaticBody, wallFixtureDef).setUserData("wall");
        PhysicsFactory.createBoxBody(this.mPhysicsWorld, roof, BodyType.StaticBody, wallFixtureDef).setUserData("wall");
        PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyType.StaticBody, wallFixtureDef).setUserData("wall");
        PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyType.StaticBody, wallFixtureDef).setUserData("wall");

        mScene.attachChild(smileySprite);
        mScene.attachChild(rect1);
        mScene.attachChild(right);
        mScene.attachChild(left);
        mScene.attachChild(roof);
        mScene.attachChild(ground);

        this.mScene.registerUpdateHandler(this.mPhysicsWorld);
        mPhysicsWorld.setContactListener(this);
        mScene.setOnSceneTouchListener(this);
        return mScene;
    }

    @Override
    public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
        // TODO Auto-generated method stub
        smileyBody.setLinearVelocity(0, -15);
        return false;
    }

    @Override
    public void onAccelerationAccuracyChanged(AccelerationData pAccelerationData) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onAccelerationChanged(AccelerationData pAccelerationData) {
        // TODO Auto-generated method stub
        final Vector2 gravity = Vector2Pool.obtain(0, 15);
        this.mPhysicsWorld.setGravity(gravity);

        Vector2Pool.recycle(gravity);

        smileyBody.setLinearVelocity(pAccelerationData.getX() * 2, smileyBody.getLinearVelocity().y);
    }

    @Override
    public void beginContact(Contact contact) {
        // TODO Auto-generated method stub
//      Log.d("begin contact", "begin contact");
//      Log.d("begin contact", smileyBody.getLinearVelocity().toString() + "");

        Fixture fixtureA = contact.getFixtureA();
        Fixture fixtureB = contact.getFixtureB();

        Fixture platFormFixture = null;
        Fixture ballFixture = null;
        // also could not understand whats going on here in the code coming forward
        if(fixtureA.getBody().getUserData().equals("slab") && fixtureB.getBody().getUserData().equals("smiley")) {
            platFormFixture = fixtureA;
            ballFixture = fixtureB;

        }

        if(fixtureA.getBody().getUserData().equals("smiley") && fixtureB.getBody().getUserData().equals("slab")) {
            platFormFixture = fixtureB;
            ballFixture = fixtureA;
        }


        if(platFormFixture == null) {
            return;
        }

        Body platFormBody = platFormFixture.getBody();
        Body ballBody = ballFixture.getBody();



        int numPoints = contact.getWorldManifold().getNumberOfContactPoints();
        WorldManifold worldManiFold = contact.getWorldManifold();

        for(int i = 0; i < numPoints; i++) {
            Vector2 vector2 = ballBody.getLinearVelocityFromWorldPoint(worldManiFold.getPoints()[i]);
            Vector2 vector3 = platFormBody.getLinearVelocityFromWorldPoint(worldManiFold.getPoints()[i]);
            Vector2 vector4 = platFormBody.getLocalVector(vector2.sub(vector3));
            if(vector4.y < -1) {
//              Log.d("collision ignored", "collision ignored");
                return;
            }
            else if(vector4.y < 1) {
                Vector2 localPoint = platFormBody.getLocalPoint(worldManiFold.getPoints()[i]);
                Log.d("local y", localPoint.y + "");
                float f = 1.1f;
                if(localPoint.y > f - 0.05f) {
                    return;
                }
            }
        }

        contact.setEnabled(false);



    }

    @Override
    public void endContact(Contact contact) {
        // TODO Auto-generated method stub
        contact.setEnabled(true);
//      Log.d("end contact", "end contact");
//      Log.d("end contact", smileyBody.getLinearVelocity().toString());
    }

    Vector2 v;

    @Override
    public void preSolve(Contact contact, Manifold oldManifold) {
        // TODO Auto-generated method stub
//      Log.d("presolve", "presolve");
//      Log.d("presolve", smileyBody.getLinearVelocity().toString());

    }

    @Override
    public void postSolve(Contact contact, ContactImpulse impulse) {
        // TODO Auto-generated method stub
//      Log.d("postsolve", "postsolve");
//      Log.d("postsolve", smileyBody.getLinearVelocity().toString());
    }

}

解决方案

I have achieved the desired functionality and given the solution code here. I don't know how does it work but its working now.

public class JumpingBallActivity extends SimpleBaseGameActivity implements IOnSceneTouchListener, IAccelerationListener, ContactListener {

    private int CAMERA_WIDTH = 480;
    private int CAMERA_HEIGHT = 800;
    private BitmapTextureAtlas mBitmapTextureAtlas;
    private ITextureRegion smileyRegion;
    private Scene mScene;
    private Camera camera;
    private Sprite smileySprite;
    private Body smileyBody;
    private PhysicsWorld mPhysicsWorld;
    private Body slabWallBody;
    private FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(0.6f, 0.0f, 0.0f);
    final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(1, 0.0f, 0.0f);


    @Override
    public EngineOptions onCreateEngineOptions() {
        // TODO Auto-generated method stub

        camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

        return new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
    }

    @Override
    protected void onCreateResources() {
        // TODO Auto-generated method stub
        BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

        mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 84, 84, TextureOptions.BILINEAR);
        smileyRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mBitmapTextureAtlas, getAssets(), "smiley.png", 0, 0);
        mBitmapTextureAtlas.load();
        enableAccelerationSensor(this);

    }

    @Override
    protected Scene onCreateScene() {
        // TODO Auto-generated method stub
        this.mScene = new Scene();
        this.mScene.setBackground(new Background(0, 0, 0));
        this.mScene.setOnSceneTouchListener(this);
        this.mPhysicsWorld = new PhysicsWorld(new Vector2(0, 15), false);


        final VertexBufferObjectManager vertexBufferObjectManager = this.getVertexBufferObjectManager();
        Rectangle rect1 = new Rectangle((CAMERA_WIDTH/2) - 100, (CAMERA_HEIGHT/2) + 50, 200, 10, vertexBufferObjectManager);
        slabWallBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, rect1, BodyType.StaticBody, wallFixtureDef);
        slabWallBody.setUserData("slab");
        mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(rect1, slabWallBody, false, false));


        smileySprite = new Sprite(smileyRegion.getWidth(), smileyRegion.getHeight(), smileyRegion, vertexBufferObjectManager);
        smileyBody = PhysicsFactory.createCircleBody(mPhysicsWorld, smileySprite,  BodyType.DynamicBody, FIXTURE_DEF);
        smileyBody.setUserData("smiley");
        mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(smileySprite, smileyBody, true, false));


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

        PhysicsFactory.createBoxBody(this.mPhysicsWorld, ground, BodyType.StaticBody, wallFixtureDef).setUserData("wall");
        PhysicsFactory.createBoxBody(this.mPhysicsWorld, roof, BodyType.StaticBody, wallFixtureDef).setUserData("wall");
        PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyType.StaticBody, wallFixtureDef).setUserData("wall");
        PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyType.StaticBody, wallFixtureDef).setUserData("wall");

        mScene.attachChild(smileySprite);
        mScene.attachChild(rect1);
        mScene.attachChild(right);
        mScene.attachChild(left);
        mScene.attachChild(roof);
        mScene.attachChild(ground);

        this.mScene.registerUpdateHandler(this.mPhysicsWorld);
        mPhysicsWorld.setContactListener(this);
        mScene.setOnSceneTouchListener(this);
        return mScene;
    }

    @Override
    public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
        // TODO Auto-generated method stub
        smileyBody.setLinearVelocity(0, -15);
        return false;
    }

    @Override
    public void onAccelerationAccuracyChanged(AccelerationData pAccelerationData) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onAccelerationChanged(AccelerationData pAccelerationData) {
        // TODO Auto-generated method stub
        final Vector2 gravity = Vector2Pool.obtain(0, 15);
        this.mPhysicsWorld.setGravity(gravity);

        Vector2Pool.recycle(gravity);

        smileyBody.setLinearVelocity(pAccelerationData.getX() * 2, smileyBody.getLinearVelocity().y);
    }

    @Override
    public void beginContact(Contact contact) {
        // TODO Auto-generated method stub
//      Log.d("begin contact", "begin contact");
//      Log.d("begin contact", smileyBody.getLinearVelocity().toString() + "");

    }

    @Override
    public void endContact(Contact contact) {
        // TODO Auto-generated method stub
        contact.setEnabled(true);
//      Log.d("end contact", "end contact");
//      Log.d("end contact", smileyBody.getLinearVelocity().toString());
    }

    Vector2 v;

    @Override
    public void preSolve(Contact contact, Manifold oldManifold) {
        // TODO Auto-generated method stub
//      Log.d("presolve", "presolve");
//      Log.d("presolve", smileyBody.getLinearVelocity().toString());
        Fixture fixtureA = contact.getFixtureA();
        Fixture fixtureB = contact.getFixtureB();

        Fixture platFormFixture = null;
        Fixture ballFixture = null;

        if(fixtureA.getBody().getUserData().equals("slab") && fixtureB.getBody().getUserData().equals("smiley")) {
            platFormFixture = fixtureA;
            ballFixture = fixtureB;

        }

        if(fixtureA.getBody().getUserData().equals("smiley") && fixtureB.getBody().getUserData().equals("slab")) {
            platFormFixture = fixtureB;
            ballFixture = fixtureA;
        }


        if(platFormFixture == null) {
            return;
        }

        Body platFormBody = platFormFixture.getBody();
        Body ballBody = ballFixture.getBody();

        WorldManifold worldManiFold = contact.getWorldManifold();
        int numPoints = contact.getWorldManifold().getNumberOfContactPoints();
        for(int i = 0; i < numPoints; i++){
            Vector2 vector2 = ballBody.getLinearVelocityFromWorldPoint(worldManiFold.getPoints()[i]);
            if(vector2.y < 0) {
                contact.setEnabled(false);
            }
        }

//      WorldManifold worldManiFold = contact.getWorldManifold();
//      Vector2 normal = worldManiFold.getNormal();
//      
//      if(normal.y != -1) {
//          contact.setEnabled(false);
//      }
    }

    @Override
    public void postSolve(Contact contact, ContactImpulse impulse) {
        // TODO Auto-generated method stub
//      Log.d("postsolve", "postsolve");
//      Log.d("postsolve", smileyBody.getLinearVelocity().toString());
    }

}

这篇关于一种方式墙壁andengine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆