Java 2'之间的碰撞检测;区域';

Java 2'之间的碰撞检测;区域';,java,swing,collision,area,boids,Java,Swing,Collision,Area,Boids,我一直在使用用户Andrew Thompson提供的以下示例-> 我曾试图在我的程序(下面的代码)中以“绘制”方法实现这一点,但由于某种原因,将正方形颜色更改为“红色”的语句永远不会被认为是真的,我无法理解为什么;在这一点上,我已经尝试了几乎所有的方法,但是由于缺少Andrew的进一步帮助,我似乎无法在正确实现该代码方面取得进展,因此当三角形群与正方形接触时,正方形的颜色将变为红色 非常感谢所有帮助(代码如下): import java.awt.*; 导入java.awt.event.Actio

我一直在使用用户Andrew Thompson提供的以下示例->

我曾试图在我的程序(下面的代码)中以“绘制”方法实现这一点,但由于某种原因,将正方形颜色更改为“红色”的语句永远不会被认为是真的,我无法理解为什么;在这一点上,我已经尝试了几乎所有的方法,但是由于缺少Andrew的进一步帮助,我似乎无法在正确实现该代码方面取得进展,因此当三角形群与正方形接触时,正方形的颜色将变为红色

非常感谢所有帮助(代码如下):

import java.awt.*;
导入java.awt.event.ActionEvent;
导入java.awt.geom.*;
导入静态java.lang.Math.*;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Random;
导入javax.swing.*;
导入javax.swing.Timer;
公共类Boids扩展了JPanel{
羊群;
羊群2;
最终整数w,h;
公共图书馆{
w=1200;
h=600;
设置首选尺寸(新尺寸(w,h));
挫折背景(颜色:黑色);
产卵群();
新计时器(17,(动作事件e)->{
如果(羊群已离开建筑物(w))
产卵群();
重新油漆();
}).start();
}
公众假期{
Random rand=新的Random();
int n=兰特·奈克斯汀(599)+1;
群=群。产卵(100,h-n,20);
}
@凌驾
公共组件(图形gg){
超级油漆组件(gg);
图形2d g=(图形2d)gg;
g、 setRenderingHint(RenderingHints.KEY_抗锯齿,
RenderingHints.VALUE_ANTIALIAS_ON);
群跑(g,w,h);
}
公共静态void main(字符串[]args){
SwingUtilities.invokeLater(()->{
JFrame f=新的JFrame();
f、 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f、 setTitle(“模拟器v0.6”);
f、 可设置大小(假);
f、 添加(新的Boids(),BorderLayout.CENTER);
f、 包装();
f、 setLocationRelativeTo(空);
f、 setVisible(真);
});
}
}
类Boid{
静态最终随机r=新随机();
静态最终Vec迁移=新Vec(0.02,0);
静态最终整数大小=3;
最终双maxForce,maxSpeed;
Vec位置、速度、加速度;
包含私有布尔值=真;
Boid(双x,双y){
加速度=新的向量();
速度=新向量(r.nextInt(3)+1,r.nextInt(3)-1);
位置=新的Vec(x,y);
最大速度=3.0;
最大力=0.05;
}
无效更新(){
速度加(加速度);
速度极限(最大速度);
位置。添加(速度);
加速度mult(0);
}
无效应用力(Vec力){
加速度。加(力);
}
矢量搜索(矢量目标){
矢量转向=矢量子系统(目标、位置);
steer.normalize();
steer.mult(最大速度);
转向(速度);
转向极限(最大力);
返回转向;
}
无效群集(图形2D g,列表BOID){
视图(g,boids);
Vec规则1=分离(BOID);
Vec规则2=校准(boids);
Vec规则3=内聚力(BOID);
规则1.mult(2.5);
规则2.mult(1.5);
规则3.mult(1.3);
申请权(规则1);
申请权(规则2);
申请权(规则3);
应用力(迁移);
}
公共布尔doAreasCollide(区域1、区域2){
布尔碰撞=假;
区域1=新区域(区域1);
1.减去(区域2);
如果(!1.等于(区域1)){
碰撞=真;
}
区域2=新区域(区域2);
2.减去(区域1);
如果(!2.等于(区域2)){
碰撞=真;
}
返回碰撞;
}
无效视图(图形2D g,列表BOID){
双视距=100;
双周边杨乐=PI*0.85;
for(Boid b:Boid){
b、 包含=错误;
如果(b==这个)
继续;
双d=向量距离(位置,b位置);
如果(d视距)
继续;
Vec视线=Vec.sub(b.位置,位置);
双角度=矢量之间的角度(视线、速度);
if(角度<周边角度)
b、 包含=正确;
}
}
Vec分离(列表BOID){
双重期望分离=25;
Vec转向=新的Vec(0,0);
整数计数=0;
for(Boid b:Boid){
如果(!b.包括在内)
继续;
双d=向量距离(位置,b位置);
如果((d>0)和((d0){
驾驶室(计数);
}
如果(steer.mag()>0){
steer.normalize();
steer.mult(最大速度);
转向(速度);
转向极限(最大力);
返回转向;
}
返回新的Vec(0,0);
}
Vec对齐(列表BOID){
双优先列表=50;
Vec转向=新的Vec(0,0);
整数计数=0;
for(Boid b:Boid){
如果(!b.包括在内)
继续;
双d=向量距离(位置,b位置);
如果((d>0)和&(d0){
驾驶室(
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.geom.*;
import static java.lang.Math.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.*;
import javax.swing.Timer;

public class Boids extends JPanel {


    Flock flock;
    Flock flock2;
    final int w, h;

    public Boids() {
        w = 1200;
        h = 600;

        setPreferredSize(new Dimension(w, h));
        setBackground(Color.black);

        spawnFlock();

       new Timer(17, (ActionEvent e) -> {
            if (flock.hasLeftTheBuilding(w)) 

                spawnFlock(); 
            repaint();
       }).start(); 

    }

    public void spawnFlock() {

        Random rand = new Random(); 
        int n = rand.nextInt(599) + 1;
        flock = Flock.spawn(100, h - n, 20);

        }

    @Override
    public void paintComponent(Graphics gg) {
        super.paintComponent(gg);
        Graphics2D g = (Graphics2D) gg;
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        flock.run(g, w, h);


     }


    public static void main(String[] args) {

        SwingUtilities.invokeLater(() -> {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setTitle("Simulator v0.6");
            f.setResizable(false);
            f.add(new Boids(), BorderLayout.CENTER);
            f.pack(); 
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        });

    }
    }

class Boid {


    static final Random r = new Random();
    static final Vec migrate = new Vec(0.02, 0);
    static final int size = 3;

    final double maxForce, maxSpeed;

    Vec location, velocity, acceleration;
    private boolean included = true;

    Boid(double x, double y) {
        acceleration = new Vec();
        velocity = new Vec(r.nextInt(3) + 1, r.nextInt(3) - 1);
        location = new Vec(x, y);
        maxSpeed = 3.0;
        maxForce = 0.05;
    }
    void update() {
          velocity.add(acceleration);
            velocity.limit(maxSpeed);
            location.add(velocity);
            acceleration.mult(0);

    }

    void applyForce(Vec force) {
        acceleration.add(force);
    }

    Vec seek(Vec target) {
        Vec steer = Vec.sub(target, location);
        steer.normalize();
        steer.mult(maxSpeed);
        steer.sub(velocity);
        steer.limit(maxForce);
        return steer;
    }

    void flock(Graphics2D g, List<Boid> boids) {
        view(g, boids);

        Vec rule1 = separation(boids);
        Vec rule2 = alignment(boids);
        Vec rule3 = cohesion(boids);

        rule1.mult(2.5);
        rule2.mult(1.5);
        rule3.mult(1.3);

        applyForce(rule1);
        applyForce(rule2);
        applyForce(rule3);
        applyForce(migrate);


    }
    public boolean doAreasCollide(Area area1, Area area2) {
        boolean collide = false;

        Area collide1 = new Area(area1);
        collide1.subtract(area2);
        if (!collide1.equals(area1)) {
            collide = true;
        }

        Area collide2 = new Area(area2);
        collide2.subtract(area1);
        if (!collide2.equals(area2)) {
            collide = true;
        }

        return collide;
    }



    void view(Graphics2D g, List<Boid> boids) {
        double sightDistance = 100;
        double peripheryAngle = PI * 0.85;

        for (Boid b : boids) {
            b.included = false;

            if (b == this)
                continue;

            double d = Vec.dist(location, b.location);
            if (d <= 0 || d > sightDistance)
                continue;

            Vec lineOfSight = Vec.sub(b.location, location);

            double angle = Vec.angleBetween(lineOfSight, velocity);
            if (angle < peripheryAngle)
                b.included = true;
        }
    }

    Vec separation(List<Boid> boids) {
           double desiredSeparation = 25;

            Vec steer = new Vec(0, 0);
            int count = 0;
            for (Boid b : boids) {
                if (!b.included)
                    continue;

                double d = Vec.dist(location, b.location);
                if ((d > 0) && (d < desiredSeparation)) {
                    Vec diff = Vec.sub(location, b.location);
                    diff.normalize();
                    diff.div(d);        // weight by distance
                    steer.add(diff);
                    count++;
                }
            }
            if (count > 0) {
                steer.div(count);
            }

            if (steer.mag() > 0) {
                steer.normalize();
                steer.mult(maxSpeed);
                steer.sub(velocity);
                steer.limit(maxForce);
                return steer;
            }
            return new Vec(0, 0);
        }


    Vec alignment(List<Boid> boids) {
          double preferredDist = 50;

            Vec steer = new Vec(0, 0);
            int count = 0;

            for (Boid b : boids) {
                if (!b.included)
                    continue;

                double d = Vec.dist(location, b.location);
                if ((d > 0) && (d < preferredDist)) {
                    steer.add(b.velocity);
                    count++;
                }
            }

            if (count > 0) {
                steer.div(count);
                steer.normalize();
                steer.mult(maxSpeed);
                steer.sub(velocity);
                steer.limit(maxForce);
            }
            return steer;
        }

    Vec cohesion(List<Boid> boids) {
          double preferredDist = 50;

            Vec target = new Vec(0, 0);
            int count = 0;

            for (Boid b : boids) {
                if (!b.included)
                    continue;

                double d = Vec.dist(location, b.location);
                if ((d > 0) && (d < preferredDist)) {
                    target.add(b.location);
                    count++;
                }
            }
            if (count > 0) {
                target.div(count);
                return seek(target);
            }
            return target;
        }

    void draw(Graphics2D g) {
        AffineTransform save = g.getTransform();
        int[] xTriangle = {33,36,34};
        int[] yTriangle = {30,30,20};
  Area   triangle = new Area(new Polygon(xTriangle, yTriangle, 3));
        g.translate(location.x, location.y);
        g.rotate(velocity.heading() + PI / 2);
        g.setColor(Color.green);
     //   g.fill(shape);
        g.setColor(Color.green);
        g.draw(triangle);
        g.fill(triangle);
        g.setTransform(save);
        Area square = new Area(new Rectangle(600,250,200,200));
        g.draw(square);

        if (doAreasCollide(square, triangle)) {
            g.setColor(Color.RED);
            System.out.println("Collision");
        } else {
            g.setColor(Color.GREEN);
            System.out.println("noCollision");
        }
        g.fill(square);
    }

    public void run(Graphics2D g, List<Boid> boids, int w, int h) { 
        flock(g, boids);
        update();
        draw(g);
    }


}

class Flock {
    List<Boid> boids;

    Flock() {
        boids = new ArrayList<>();
    }

    void run(Graphics2D g,  int w, int h) {
        for (Boid b : boids) {
            b.run(g, boids, w, h);

        }
    }

    boolean hasLeftTheBuilding(int w) {
        int count = 0;
        for (Boid b : boids) {
            if (b.location.x + Boid.size > w) 
                count++;

            }
        return boids.size() == count;
    }

    void addBoid(Boid b) {
        boids.add(b);
    }

    static Flock spawn(double w, double h, int numBoids) {
        Flock flock = new Flock();
        for (int i = 0; i < numBoids; i++)
            flock.addBoid(new Boid(w, h));
        return flock;
    }

}
class Vec {
    double x, y;

    Vec() {
    }

    Vec(double x, double y) {
        this.x = x;
        this.y = y;
    }

    void add(Vec v) {
        x += v.x;
        y += v.y;
    }

    void sub(Vec v) {
        x -= v.x;
        y -= v.y;
    }

    void div(double val) {
        x /= val;
        y /= val;
    }

    void mult(double val) {
        x *= val;
        y *= val;
    }

    double mag() {
        return sqrt(pow(x, 2) + pow(y, 2));
    }

    double dot(Vec v) {
        return x * v.x + y * v.y;
    }

    void normalize() {
        double mag = mag();
        if (mag != 0) {
            x /= mag;
            y /= mag;
        }
    }

    void limit(double lim) {
        double mag = mag();
        if (mag != 0 && mag > lim) {
            x *= lim / mag;
            y *= lim / mag;
        }
    }

    double heading() {
        return atan2(y, x);
    }

    static Vec sub(Vec v, Vec v2) {
        return new Vec(v.x - v2.x, v.y - v2.y);
    }

    static double dist(Vec v, Vec v2) {
        return sqrt(pow(v.x - v2.x, 2) + pow(v.y - v2.y, 2));
    }

    static double angleBetween(Vec v, Vec v2) {
        return acos(v.dot(v2) / (v.mag() * v2.mag()));
    }
}