Java Boid旋转限制

问题描述

我正在创建一个在java swing中实现boid的程序。为避免辫子立即捕捉到所需位置,我将角速度和加速度限制为静态Constants类中定义的值。 public static final double maxTurningSpeed = 1d; // radians per second public static final double maxAngularacceleration = 1d; // radians per second squared

Image of boids

问题是,当我使用此代码时,某些boid具有非常不寻常的行为,例如旋转,而不是被goalAngle所吸引并且没有遵循平滑的路径。当goalAngle == angle时,辫子似乎会随机地开始和停止旋转,直到它们达到一个新的目标为止,此时,大多数都是无限循环的。

    public void alterCourse(double timePassed) {
        // if the distance (rad) to the goal Angle > max turning speed,angle += turningspeed,else ange = goal angle
        
        double newAngularVeLocity =  Math.abs(goalAngle-angle);
        
        if (Math.abs(newAngularVeLocity-angularVeLocity) > Constants.maxAngularacceleration) {
            // if Planned acceleration > the max acceleration
            this.angle += (angularVeLocity+Constants.maxAngularacceleration)
                    * (goalAngle-angle < 0 ? 1 : -1); // negative if  the difference between the angle > 0
            this.angle %= (2 * Math.PI);
        }
        
        if (newAngularVeLocity > Constants.maxTurningSpeed*timePassed) {
            this.angle = this.goalAngle % (2 * Math.PI);
        } else if ((angle+goalAngle) % (Math.PI*2) > Math.PI) { // Left/Right turn
            this.angle += Constants.maxTurningSpeed*timePassed;
        } else {
            this.angle -= Constants.maxTurningSpeed*timePassed;
        }

        angularVeLocity = newAngularVeLocity;
    }

任何指导表示赞赏

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)