(Gdx-AI)避免冲突行为无效

时间:2019-07-10 11:07:34

标签: libgdx artificial-intelligence gdx-ai

我是Gdx-ai的新手,我无法让CollisionAvoidance行为正常工作。我99%确信我已根据github上的aiMaster类正确设置了它,但它似乎无能为力。我以此为参考,不知道自己错过了什么:[CollisionAvoidanceTest.java] [1]。这是我的代码:

敌机代码:

public EnemyShip(GameScreen screen){
        //irrelevant code, setsup world and other things
        ship = new SteeringEntity(b2body, 10 / SpaceSoccer.PPM);
        ball = new SteeringEntity(ballBody, 15 / SpaceSoccer.PPM);
        iterator = new Iterator<SteeringEntity>() {
            @Override
            public boolean hasNext() {//Temporary code until I can get it working
                return !alreadyRun;
            }

            @Override
            public SteeringEntity next() {
                alreadyRun = true;
                System.out.println(">>");//This doesn't run
                return ball;
            }
        };
        iterable = new Iterable<SteeringEntity>() {
            @Override
            public Iterator<SteeringEntity> iterator() {
                return iterator;
            }

            @Override
            public void forEach(Consumer<? super SteeringEntity> consumer) {

            }

            @Override
            public Spliterator<SteeringEntity> spliterator() {
                return null;
            }
        };
        proximity = new RadiusProximity<Vector2>(ship, iterable, ship.getBoundingRadius());
        CollisionAvoidance<Vector2> avoid = new CollisionAvoidance<Vector2>(ship, proximity){
            @Override
            public boolean reportNeighbor(Steerable<Vector2> neighbor){//Temporary code until I can get the behaviour working
                return neighbor == ball;
            }
        };
        if(avoid.reportNeighbor(ball)){
            System.out.println("ok"); //this runs fine
        }
        blendedSteering = new BlendedSteering<Vector2>(ship);
        blendedSteering.add(avoid, 2);//One behaviour until collisionAvoidance works
        ship.setBehavior(blendedSteering);
    }

    private void DefineShip(){
        //irrelevant code, createsb2body 
    }

    public void update(float dt){
        //dt in this case is GdxAI.getTimepiece().getDeltaTime();
        ship.update(dt);
    }

SteeringEntity类:

public SteeringEntity(Body body, float boundingRadius){
        this.body = body;
        this.boundingRadius = boundingRadius;

    }

    public void update(float dt){
        if(behavior != null){
            behavior.calculateSteering(steerOutput);
            applySteering(dt);
        }
    }

    private void applySteering(float dt){

        anyAccelerations = false;

        if(!steerOutput.linear.isZero()){
            force = steerOutput.linear.scl(dt);
            body.applyForceToCenter(force, true);
            anyAccelerations = true;
        }

        if(steerOutput.angular != 0){
            body.applyTorque(steerOutput.angular * dt, true);
            anyAccelerations = true;
        }

        if(anyAccelerations){
            velocity = body.getLinearVelocity();
            currentSpeedSquared = velocity.len2();
            if(currentSpeedSquared > maxLinearSpeed * maxLinearSpeed){
                body.setLinearVelocity(velocity.scl(maxLinearSpeed / (float) Math.sqrt(currentSpeedSquared)));
            }

            if(body.getAngularVelocity() > maxAngularSpeed){
                body.setAngularVelocity(maxAngularSpeed);
            }
        }
    }

    //Irrelevant overrides, most edited to be self explanatory i.e :
    @Override
    public Vector2 getLinearVelocity() {
        return body.getLinearVelocity();
    }

我尝试使用shaperederer来显示实际的接近度,但没有显示任何内容,使用打印语句,我可以确定接近度不为null且不为零,这很令人困惑。

0 个答案:

没有答案