简体   繁体   English

带阵列的Box2d碰撞检测

[英]Box2d Collision Detection with Arrays

I'm developing in AndEngine I have nearly completed my game, but unfortunately, it suffers from a low fps occasionally due to the fact that I am checking .collideswith a lot in my update loop. 我正在AndEngine中进行开发,我已经快完成游戏了,但是不幸的是,由于我在更新循环中经常检查.collides的事实,偶尔会出现帧速低的问题。 I now understand that this is the problem, and I have been trying to change it using Box2d with bodies and such as in theory, this is exactly what I need, but I can't get a grasp around it! 我现在知道这是问题所在,我一直在尝试使用Box2d结合身体进行更改,例如从理论上讲,这正是我所需要的,但是我无法理解它!

Basically, I have 4 arrays; 基本上,我有4个数组; one for cars, three for enemies. 一台用于汽车,三台用于敌人。 Cars drive from left to right, and if they make contact with any of these enemies, it's speed changes depending. 汽车从左到右行驶,如果它们与这些敌人中的任何一个接触,则速度会有所变化。 Do I have to allocate a body to each one of cars and enemies when I am loading their arrays? 加载数组时,是否必须为每辆汽车和敌人分配一个车身? And then how do I check? 然后如何检查? do I run a for loop, and then an 'isBodyContacted(carBody, iceBergBody);' 我运行for循环,然后运行“ isBodyContacted(carBody,iceBergBody);” in the update look? 在更新外观? It's a bit cofusing! 有点令人困惑!

For reference, loading my car: 供参考,装载我的车:

private void loadCar() {
            for (int i = 0; i < rManager.getInstance().carArray.length; i++) {
                    rManager.getInstance().carArray[i] = new Car(new Sprite(
                                    rManager.getInstance().spawnPoint[i].getSpawnPos().x,
                                    rManager.getInstance().spawnPoint[i].getSpawnPos().y,
                                    rManager.getInstance().car_region,
                                    engine.getVertexBufferObjectManager()) {
                            @Override
                            public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
                                            final float pTouchAreaLocalX,
                                            final float pTouchAreaLocalY) {

                                    if (pSceneTouchEvent.isActionMove()
                                                    && GameManager.getInstance().getDebugMode() == true) {
                                            this.setPosition(pSceneTouchEvent.getX(),
                                                            pSceneTouchEvent.getY());
                                    }
                                    return true;
                            }
                    });
            }

            for (int i = 0; i < rManager.getInstance().carArray.length; i++) {
                    rManager.getInstance().carArray[i].getCarSprite().setScale(0.5f);
                    rManager.getInstance().carArray[i].getCarSprite().setAnchorCenter(
                                    0, 0);
                    colourX = (randNumber(2, 9));
                    colourY = (randNumber(2, 9));
                    colourZ = (randNumber(2, 9));
                    colourX /= 10;
                    colourY /= 10;
                    colourZ /= 10;
                    rManager.getInstance().carArray[i].getCarSprite().setColor(colourZ,
                                    colourY, colourZ);
                    attachChild(rManager.getInstance().carArray[i].getCarSprite());
                    this.registerTouchArea(rManager.getInstance().carArray[i]
                                    .getCarSprite());
                    this.setTouchAreaBindingOnActionDownEnabled(true);
            }
    }

Loading an enemy.. 装载敌人

void loadIceBerg() {
            for (int i = 0; i < rManager.getInstance().iceBergArray.length; i++) {
                    rManager.getInstance().iceBergArray[i] = new IceBergEnemy(
                                    new Sprite(randNumber(200, 700), randNumber(0, 480),
                                                    rManager.getInstance().iceBerg_region,
                                                    engine.getVertexBufferObjectManager()) {
                                            @Override
                                            public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
                                                    this.setPosition(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
                                                    if (this.getY() > rManager.getInstance().camera.getHeight() - (this.getHeight() / 2) || this.getY() < 0 + (this.getHeight() / 2))
                                                    {
                                                            this.setPosition(200 - (pDifficultyLevel * 20),
                                                                            -100);
                                                                                                                            showScore(50, " ice berg!");
                                                            this.setVisible(false);

                                                    }      

                                                    if(pSceneTouchEvent.isActionUp())
                                                    {
                                                            updateScore(50);
                                                            GameManager.getInstance().updateTotalIceBergEnemiesKilled();

                                                    }



                                                    return true;
                                            }
                                    });

                    rManager.getInstance().iceBergArray[i].getIceBergSprite()
                                    .setPosition(
                                                    rManager.getInstance().iceBergArray[i]
                                                                    .getIceBergSprite().getX(),
                                                    rManager.getInstance().iceBergArray[i]
                                                                    .getIceBergSprite().getY());
                    rManager.getInstance().iceBergArray[i].getIceBergSprite().setScale(
                                    0.8f);
                    this.registerTouchArea(rManager.getInstance().iceBergArray[i]
                                    .getIceBergSprite());
                    this.setTouchAreaBindingOnActionDownEnabled(true);

                    attachChild(rManager.getInstance().iceBergArray[i]
                                    .getIceBergSprite());
            }
    }

Some collision! 一些碰撞!

else if (rManager.getInstance().iceBergArray[2]
                                                    .getIceBergSprite().collidesWith(
                                                                    rManager.getInstance().carArray[r]
                                                                                    .getCarSprite())) {
                                            rManager.getInstance().carArray[r].setCarSpeed(0f);
                                    }

                                    else if (rManager.getInstance().iceBergArray[3]
                                                    .getIceBergSprite().collidesWith(
                                                                    rManager.getInstance().carArray[r]
                                                                                    .getCarSprite())) {
                                            rManager.getInstance().carArray[r].setCarSpeed(0f);
                                    }

                                    else if (rManager.getInstance().iceBergArray[4]
                                                    .getIceBergSprite().collidesWith(
                                                                    rManager.getInstance().carArray[r]
                                                                                    .getCarSprite())) {
                                            rManager.getInstance().carArray[r].setCarSpeed(0f);
                                    }

                                    else if (rManager.getInstance().iceBergArray[5]
                                                    .getIceBergSprite().collidesWith(
                                                                    rManager.getInstance().carArray[r]
                                                                                    .getCarSprite())) {
                                            rManager.getInstance().carArray[r].setCarSpeed(0f);
                                    }

I have found many tutorials, but none seem to reference an array of objects, and that is exactly what I need. 我找到了很多教程,但似乎都没有引用对象数组,而这正是我所需要的。 If someone could help me or provide some sort of quick tutorial, then that would be greatly appreciated. 如果有人可以帮助我或提供某种快速教程,那么将不胜感激。 Thanks!! 谢谢!!

You should not use any loops to check each possible combination of collision. 您不应使用任何循环来检查每种可能的碰撞组合。 Instead, you should register a listener for the whole World and set user data on your bodies (eg type = "car", "enemy"). 相反,您应该为整个世界注册一个侦听器,并在您的身体上设置用户数据(例如,type =“ car”,“ enemy”)。 Then in the listener, you just check which two objects collided. 然后在侦听器中,您只需检查两个对象发生了碰撞。 Check out this example: 看看这个例子:

http://www.andengine.org/forums/gles2/collision-events-t7140.html#p31300 http://www.andengine.org/forums/gles2/collision-events-t7140.html#p31300

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM