两个Shape对象之间的Java碰撞检测? [英] Java collision detection between two Shape objects?

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

问题描述

我想知道判断一个 Shape 对象是否与另一个形状相交的最佳方法.目前,只要涉及与矩形相交的形状,反之亦然,我的游戏中的碰撞检测就已经解决了.我遇到的问题是 Shape 类中的 intersects() 方法只能将一个 Rectangle 或一个 Point 作为参数,而不是另一个 Shape.有没有一种有效的方法来测试两个 Shape 对象是否以任何方式重叠?我尝试的一种方法是使用 for 循环生成一个点区域来测试它们是否在形状中,然后构建一个 Point 对象数组以发送到另一个形状进行测试,但这显着降低了我的帧率,因为所有不必要的比较.

I would like to know the best way to tell if a Shape object intersects another shape. Currently I have collision detection in my game sorted out as long as it involves a Shape intersecting a Rectangle or vice versa. The problem I'm having is that the intersects() method in the Shape class can only take a Rectangle or a Point as a parameter, not another Shape. Is there an efficient way to test if two Shape objects are overlapping in any way? One way I tried was using a for loop to generate an area of points to test if they were in the shape, and then building an array of Point objects to send to the other shape to test, but this significantly dropped my framerate because of all of the unnecessary comparisons.

我在这里寻找并寻找了类似的东西,但没有真正找到任何东西.如果重复,请提前抱歉.

I looked and looked for something similar on here but didn't find anything really. Sorry in advance if this is a repeat.

推荐答案

未测试,但为什么不:

import java.awt.geom.Area;

...

public static boolean testIntersection(Shape shapeA, Shape shapeB) {
   Area areaA = new Area(shapeA);
   areaA.intersect(new Area(shapeB));
   return !areaA.isEmpty();
}

Area 实现了 Shape,但添加了一些可能有用的方法

Area implements Shape, but adds some potentially useful methods

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

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