AndEngine GLES1:在碰撞过程中识别物体

时间:2014-06-03 20:45:30

标签: android box2d andengine

我正在创造一个连锁反应'打字游戏。

在onLoadResources中,我创建了4个静态实体(地面,屋顶,左边框和右边框)作为边界。然后我创建了30个动态物体(它们是反弹的球)。

我的问题是,如何检测哪个球刚刚与边界相撞?

我现在拥有它的方式,我相信最后一个' particle_body'创建的,是唯一一个在碰撞过程中被修改过的人。

有什么建议吗?

这是我的代码:

this.mPhysicsWorld = new FixedStepPhysicsWorld(60, new Vector2(0, 0), false); // Gravity (x, y) scene.registerUpdateHandler(mPhysicsWorld);

    // Create the ground
    groundFixture = PhysicsFactory.createFixtureDef(1, 1.0f, 0.5f);
    ground = new Rectangle(0, CAMERA_HEIGHT-1, CAMERA_WIDTH, CAMERA_HEIGHT);
    ground.setColor(0.0f, 0.0f, 0.0f);
    ground_body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, this.ground, BodyType.StaticBody, groundFixture);
    ground_body.setUserData("ground");
    mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(ground, ground_body, true, false));
    scene.attachChild(ground);

    // Create the roof
    roofFixture = PhysicsFactory.createFixtureDef(1, 1.0f, 0.5f);
    roof = new Rectangle(0, 0, CAMERA_WIDTH, 1);
    roof.setColor(0.0f, 0.0f, 0.0f);
    roof_body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, this.roof, BodyType.StaticBody, roofFixture);
    roof_body.setUserData("roof");
    mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(roof, roof_body, true, false));
    scene.attachChild(roof);

    // Create the left border
    leftBorderFixture = PhysicsFactory.createFixtureDef(1, 1.0f, 0.5f);
    leftBorder = new Rectangle(0, 0, 1, CAMERA_HEIGHT);
    leftBorder.setColor(0.0f, 0.0f, 0.0f);
    leftBorder_body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, this.leftBorder, BodyType.StaticBody, leftBorderFixture);
    leftBorder_body.setUserData("leftBorder");
    mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(leftBorder, leftBorder_body, true, false));
    scene.attachChild(leftBorder);

    // Create the right border
    rightBorderFixture = PhysicsFactory.createFixtureDef(1, 1.0f, 0.5f);
    rightBorder = new Rectangle(CAMERA_WIDTH-1, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
    rightBorder.setColor(0.0f, 0.0f, 0.0f);
    rightBorder_body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, this.rightBorder, BodyType.StaticBody, rightBorderFixture);
    rightBorder_body.setUserData("rightBorder");
    mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(rightBorder, rightBorder_body, true, false));
    scene.attachChild(rightBorder);

    // Prepare a random number generator
    Random rand = new Random();

    // Create Particles
    for(int i=0; i < 30; i++)
    {
        int x = rand.nextInt(CAMERA_WIDTH) - 1;
        int y = rand.nextInt(CAMERA_HEIGHT) - 1;
        this.particle = new Particle(x, y, this.mPurpleParticleTextureRegion, 100);
        particle.setScale(0.3f);
        scene.attachChild(particle);

        particleFixture = PhysicsFactory.createFixtureDef(10, 0.9f, 0.1f);
        particle_body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, this.particle, BodyType.DynamicBody, particleFixture);
        particle_body.setUserData("particle_" + i);
        mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(this.particle, particle_body, true, false));

        // Assign a random velocity to the particle
        x = rand.nextInt(20) + 3;
        y = rand.nextInt(20) + 3;   
        particle_body.setLinearVelocity(new Vector2(x, y));  // x, y impulse

        mParticleList.add(particle);
        mBodyList.add(particle_body);
        mFixtureList.add(particleFixture);
        particleNumAR[i] = i;
    }

    // Create the Physics Collision Detection listener
    mPhysicsWorld.setContactListener(new ContactListener() 
    {    
        public void endContact(Contact contact)
        {
        }

        public void beginContact(Contact contact)
        {

            if (contact.getFixtureA().getBody().getUserData() != null &&
                contact.getFixtureB().getBody().getUserData() != null) 
            {
                final String objA = (String)contact.getFixtureA().getBody().getUserData();
                final String objB = (String)contact.getFixtureB().getBody().getUserData();

                // Particle hits ground
                if (objA.equals("ground") || objB.equals("ground")) 
                {
                        Random rand2 = new Random();

                        int xx = rand2.nextInt(20) + 3;
                        int yy = rand2.nextInt(10) + 3;   
                        particle_body.setLinearVelocity(new Vector2(xx, yy));  // x, y impulse
                }

                // Particle hits roof
                if (objA.equals("roof") || objB.equals("roof")) 
                {
                        Random rand2 = new Random();

                        int xx = rand2.nextInt(20) + 3;
                        int yy = rand2.nextInt(10) + 3;   
                        particle_body.setLinearVelocity(new Vector2(xx, yy));  // x, y impulse
                }

                // Particle hits left border
                if (objA.equals("leftBorder") || objB.equals("leftBorder")) 
                {
                        Random rand2 = new Random();

                        int xx = rand2.nextInt(10) + 3;
                        int yy = rand2.nextInt(20) + 3;   
                        particle_body.setLinearVelocity(new Vector2(xx, yy));  // x, y impulse
                }

                // Particle hits right border
                if (objA.equals("rightBorder") || objB.equals("rightBorder")) 
                {
                        Random rand2 = new Random();

                        int xx = rand2.nextInt(10) + 3;
                        int yy = rand2.nextInt(20) + 3;   
                        particle_body.setLinearVelocity(new Vector2(xx, yy));  // x, y impulse
                }
            }
        }

        @Override
        public void preSolve(Contact contact, Manifold oldManifold) 
        {
        }

        @Override
        public void postSolve(Contact contact, ContactImpulse impulse) 
        {
        }
    });

// Create the ground groundFixture = PhysicsFactory.createFixtureDef(1, 1.0f, 0.5f); ground = new Rectangle(0, CAMERA_HEIGHT-1, CAMERA_WIDTH, CAMERA_HEIGHT); ground.setColor(0.0f, 0.0f, 0.0f); ground_body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, this.ground, BodyType.StaticBody, groundFixture); ground_body.setUserData("ground"); mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(ground, ground_body, true, false)); scene.attachChild(ground); // Create the roof roofFixture = PhysicsFactory.createFixtureDef(1, 1.0f, 0.5f); roof = new Rectangle(0, 0, CAMERA_WIDTH, 1); roof.setColor(0.0f, 0.0f, 0.0f); roof_body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, this.roof, BodyType.StaticBody, roofFixture); roof_body.setUserData("roof"); mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(roof, roof_body, true, false)); scene.attachChild(roof); // Create the left border leftBorderFixture = PhysicsFactory.createFixtureDef(1, 1.0f, 0.5f); leftBorder = new Rectangle(0, 0, 1, CAMERA_HEIGHT); leftBorder.setColor(0.0f, 0.0f, 0.0f); leftBorder_body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, this.leftBorder, BodyType.StaticBody, leftBorderFixture); leftBorder_body.setUserData("leftBorder"); mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(leftBorder, leftBorder_body, true, false)); scene.attachChild(leftBorder); // Create the right border rightBorderFixture = PhysicsFactory.createFixtureDef(1, 1.0f, 0.5f); rightBorder = new Rectangle(CAMERA_WIDTH-1, 0, CAMERA_WIDTH, CAMERA_HEIGHT); rightBorder.setColor(0.0f, 0.0f, 0.0f); rightBorder_body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, this.rightBorder, BodyType.StaticBody, rightBorderFixture); rightBorder_body.setUserData("rightBorder"); mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(rightBorder, rightBorder_body, true, false)); scene.attachChild(rightBorder); // Prepare a random number generator Random rand = new Random(); // Create Particles for(int i=0; i < 30; i++) { int x = rand.nextInt(CAMERA_WIDTH) - 1; int y = rand.nextInt(CAMERA_HEIGHT) - 1; this.particle = new Particle(x, y, this.mPurpleParticleTextureRegion, 100); particle.setScale(0.3f); scene.attachChild(particle); particleFixture = PhysicsFactory.createFixtureDef(10, 0.9f, 0.1f); particle_body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, this.particle, BodyType.DynamicBody, particleFixture); particle_body.setUserData("particle_" + i); mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(this.particle, particle_body, true, false)); // Assign a random velocity to the particle x = rand.nextInt(20) + 3; y = rand.nextInt(20) + 3; particle_body.setLinearVelocity(new Vector2(x, y)); // x, y impulse mParticleList.add(particle); mBodyList.add(particle_body); mFixtureList.add(particleFixture); particleNumAR[i] = i; } // Create the Physics Collision Detection listener mPhysicsWorld.setContactListener(new ContactListener() { public void endContact(Contact contact) { } public void beginContact(Contact contact) { if (contact.getFixtureA().getBody().getUserData() != null && contact.getFixtureB().getBody().getUserData() != null) { final String objA = (String)contact.getFixtureA().getBody().getUserData(); final String objB = (String)contact.getFixtureB().getBody().getUserData(); // Particle hits ground if (objA.equals("ground") || objB.equals("ground")) { Random rand2 = new Random(); int xx = rand2.nextInt(20) + 3; int yy = rand2.nextInt(10) + 3; particle_body.setLinearVelocity(new Vector2(xx, yy)); // x, y impulse } // Particle hits roof if (objA.equals("roof") || objB.equals("roof")) { Random rand2 = new Random(); int xx = rand2.nextInt(20) + 3; int yy = rand2.nextInt(10) + 3; particle_body.setLinearVelocity(new Vector2(xx, yy)); // x, y impulse } // Particle hits left border if (objA.equals("leftBorder") || objB.equals("leftBorder")) { Random rand2 = new Random(); int xx = rand2.nextInt(10) + 3; int yy = rand2.nextInt(20) + 3; particle_body.setLinearVelocity(new Vector2(xx, yy)); // x, y impulse } // Particle hits right border if (objA.equals("rightBorder") || objB.equals("rightBorder")) { Random rand2 = new Random(); int xx = rand2.nextInt(10) + 3; int yy = rand2.nextInt(20) + 3; particle_body.setLinearVelocity(new Vector2(xx, yy)); // x, y impulse } } } @Override public void preSolve(Contact contact, Manifold oldManifold) { } @Override public void postSolve(Contact contact, ContactImpulse impulse) { } });

1 个答案:

答案 0 :(得分:0)

您可以使用联系人的其他夹具。 其中一个灯具将是你的一面墙。另一个夹具将是你碰撞的粒子。

所以你可以这样做(未经测试的伪代码):

// I don't know what class the Box2D body is in this case (b2Body maybe? or just Body?)
// so please insert appropriate type

final String objA = (String)contact.getFixtureA().getBody().getUserData();
Body bodyA = contact.getFixtureA().getBody();
final String objB = (String)contact.getFixtureB().getBody().getUserData();
Body bodyB = contact.getFixtureB().getBody();


Body particleBody = objA.startsWith("particle_") ? bodyA : bodyB; 

...

// then use particleBody for setting the velocity in your different if cases

particleBody.setLinearVelocity(new Vector2(x,y));
相关问题