简体   繁体   English

碰撞和矩形/直线相交

[英]Collision and rectangles/line intersection

I'm doing collision detection for a game, The levels are randomly generated from blocks each with its own bounding box. 我正在为游戏进行碰撞检测,这些关卡是从带有各自边界框的块中随机生成的。 My collision detection is ok except sometimes, the player ship can collide with 2 or more blocks at the same time and I need to work out which one it hit 1st. 我的碰撞检测是可以的,除了有时,玩家飞船可以同时与2个或更多块碰撞,我需要确定它撞到了第一个。

Ive already worked out that If I find the corner of the ship bounding box thats in its movement direction, and then draw a line in the opposite direction of the ships movement vector from that corner (red line on diagram), the intersection point thats furthest away on that line will be on the 1st box hit. 我已经得出结论,如果我找到了船舶边界框的沿其运动方向的角,然后从该角沿与船舶运动向量相反的方向绘制了一条线(图中的红色线),那是最远的交点在那条线上的距离将在第一个方框中。 I just don't know how to program that logic! 我只是不知道如何编程该逻辑!

What I have to work with 我必须与之合作

  • List of the bounding boxes (Rectangles) collided with 边界框列表(矩形)与
  • ships movement (Vector2) 船舶运动(Vector2)
  • Ships bounding box(Reactangle) 船舶包围盒(Reactangle)

在此处输入图片说明

My approach would be: Add another set of bounding boxes to the ship that are smaller and more acurate. 我的方法是:在船上添加另一组更小,更精确的边界框。 If collision occurs, you should then start checking into the scale of smaller bounding boxes. 如果发生碰撞,则应开始检查较小边框的比例。 From what I understand, many games do that to avoid unneccasary collision checks, but have a way to determin collisions more acurately when they occur. 据我了解,许多游戏都是这样做,以避免不必要的碰撞检查,但是有一种方法可以在碰撞发生时更准确地确定碰撞。

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

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