如何在 java 中检测与省略号的交集

问题描述 投票:0回答:0

我正在尝试制作像游戏一样的粉碎兄弟,我希望 hitboxes 是省略号,因为我可以看到它们在哪里更好地碰撞并将它们移动到更准确的方向,我正在使用公式 ((x-h)^2)/ ^2) + ((y-k)^2)/a^2) = 1 来做到这一点,但我无法让它工作

我试着让它遍历第一个椭圆的域中的每个 x 值,并将它与第二个椭圆的每个域值进行比较,如果任何 x 值相等,我让它检查它们是否共享一个 y 值并且它在某些点上工作但不是其他人,我想知道是否有人知道为什么

public double[] intersects(Hitbox h1)
    {
        double d[] = {-1, -1};
        for(double q = h-(a/2.0); q <= h+(a/2.0); q += 10)
        {
            for(double w = h1.getH()-(h1.getA()/2.0); w <= h1.getH()+(h1.getA()/2.0); w += 10)
              {
                int ha = h1.getA();
                int hb = h1.getB();
                int hh = h1.getH();
                int hk = h1.getK();
                
                 if(Math.abs(q-w) < 10)
                 {
                     long ab = (a*a)*(b*b);
                    System.out.println( q + " \t" + w);
                    System.out.println("ab\t" + ab);
                    long bb = (int)((b*b)*((q-h)*(q-h)));
                    System.out.println("bb\t" + bb);
                    double y1 = k + Math.sqrt((ab-bb)/(a*a));
                    System.out.println("y1\t" + y1);
                    double negy1 = k - Math.sqrt((ab-bb)/(a*a));
                    System.out.println("negy1\t" + negy1);
        
                    ab = (ha*ha)*(hb*hb);
                    System.out.println("ab2\t" + ab);
                    bb = (int)((hb*hb)*((w-hh)*(w-hh)));
                    System.out.println("bb2\t" + bb);
                    double y2 = hk + Math.sqrt((ab-bb)/(ha*ha));
                    System.out.println("y2\t" + y2);
                    double negy2 = hk - Math.sqrt((ab-bb)/(ha*ha));
                    System.out.println("negy2\t" + negy2);
                    
                    
                    if(Math.abs(y2-y1) < 30 ||Math.abs(negy2-y1) < 30)
                    {
                      d[0] = q;
                      d[1] = y1;
                      latestIntersection = d;
                      System.out.println("fadonbhfabdfbnaodf");
                      return d;
                    }
                    if(Math.abs(y2-negy1) < 30 ||Math.abs(negy2-negy1) < 30)
                    {
                      d[0] = q;
                      d[1] = negy1;
                      latestIntersection = d;
                      System.out.println("fadonbhfabdfbnaodf");
                      return d;
                    }
                 }
              }
        }
        latestIntersection = d;
        return d;
    }
java math collision-detection
© www.soinside.com 2019 - 2024. All rights reserved.