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

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

问题描述

我想知道,判断一个图形对象相交另一种形状的最佳方式。 目前,我有碰撞检测我的游戏整理,只要它涉及到一个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.

推荐答案

没测试过,但为什么不:

Not tested, but why not:

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 implements Shape, but adds some potentially useful methods

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

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