简体   繁体   English

碰撞检测图形2D

[英]Collision detection Graphics 2D

I'm building a simulator that is based on car collisions on the road. 我正在构建一个基于道路碰撞的模拟器。 The "cars" are basic rectangles drawn using fillRect and setting random x and y coordinates for each car. “汽车”是使用fillRect绘制的基本矩形,并为每辆汽车设置了随机的x和y坐标。 The kinematics portion of the simulator work perfect except when cars collide. 模拟器的运动学部分工作完美,除非汽车发生碰撞。 What I'm trying to do is figure out a way to detect collision without re-inventing the wheel. 我正在尝试做的是找到一种无需重新发明轮子就能检测到碰撞的方法。 In essence, is there such implementation in Java that helps with this type of situation? 本质上,Java中是否有这种实现方式可以帮助解决这种情况?

If not, I have an idea that consists of putting every single x and y point in the area of the square into an array for each car. 如果没有,我的想法是将正方形区域中的每个x和y点放入每辆汽车的阵列中。 Then if the other car's "area" overlaps a coordinate with the other, then a collision would occur. 然后,如果另一辆车的“区域”与另一辆车的坐标重叠,则会发生碰撞。 Could this be a solution, or is there a simpler way of doing this? 这可能是一个解决方案,还是有更简单的方法呢? Perhaps some advice would be great! 也许一些建议会很棒!

If not, I have an idea that consists of putting every single x and y point in the area of the square into an array for each car. 如果没有,我的想法是将正方形区域中的每个x和y点放入每辆汽车的阵列中。

No need to reinvent the wheel. 无需重新发明轮子。 Are you using Rectangle objects for your cars underneath? 您在下面的汽车中使用Rectangle对象吗? You can call methods such as contains and intersects which are part of the Rectangle api to achieve what you want. 您可以调用containsintersects方法(它们是Rectangle api的一部分)来实现所需的功能。 You need to make sure you check the next movement of the Rectangles , looking for collisions, before you move them. 您需要确保在移动Rectangles之前检查Rectangles的下一个运动,以查找碰撞。

Look here . 看这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM