椭圆与弧之间的碰撞检测 [英] Collision Detection between a Oval and Arc

查看:107
本文介绍了椭圆与弧之间的碰撞检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个简单的游戏,或者看起来.我创建了一个绘制Arc2D(半圆形状)的类,该类将随着鼠标移动来重新绘制拱形.

I am writing a simple game, or so it seemed. I created a class that draws a Arc2D (half a circle shape), that same class will repaint the arch as the mouse move.

然后,我创建了一个绘制椭圆的新类.该课程有一些简单的数学方法可以在屏幕上移动椭圆形.椭圆的运动不是很重要.现在,完成此操作后,我想检测椭圆形是否在任意点与圆弧(半圆,Only the arc line)碰撞.

Then I created a new class that draws ovals. This class has some simple mathematics to move the ovals on the screen. The movement of the ovals are not very important. So now that this is done I want to detect if the Oval collides with the arc(half a circle, Only the arc line) at any point.

我试图将椭圆形做成矩形并使用相交方法.该代码位于圆弧的draw方法中.

What I have attempted is making the oval a Rectangle and use the intersect method. This code is in the draw method for the arc.

Arc2D temp= new Arc2D.Double(200, 200, 100, 100, angle, 180, Arc2D.OPEN);
MasterOval m = new MasterOval();
Rectangle r1 = m.bounds();//This gets the bounds of the oval
if(r1.intersects(temp.getBounds()))
    System.out.println("hit");//display if intersects

由于某种原因,我无法弄清为什么它与弧碰撞时不会显示单词hit的原因.有没有办法查看它们是否相交?由于隐私权政策,这就是我可以提供的所有代码.请帮忙.

For some reason I cant figure out why it will not display the word hit when it collides with the arc. Is there a way to see if they intercect? This is all code I can provide due to privacy policies. Please help.

推荐答案

好吧,我不确定您的MasterOval类是否实现Shape接口,但是不能实现(如果没有实现,考虑使用Ellipse2D.Double或类似的东西),检查Shape实例之间的冲突的最简单方法(标准也许是?)是使用Area:

Well, I'm not sure if your MasterOval class implements the Shape interface or not, but if it does (if it doesn't, consider using Ellipse2D.Double or something of that sort), the easiest way (standard perhaps ?) of checking for collision between Shape instances is using Area:

Shape1 shape1 = new Arc2D.Double(...);
Shape2 shape2 = new Ellipse2D.Double(...);

Area area1 = new Area(shape1);
Area area2 = new Area(shape2);

if (area1.intersect(area2)) {
    ...
}

这篇关于椭圆与弧之间的碰撞检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆