检测运动物体与静止物体之间的碰撞 [英] Detect collision between a moving object and an immobile one

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

问题描述

首先,我的问题并不是真正针对C#或XNA,但我的代码示例将使用这些。

I目前正在尝试制作Pong克隆,而我遇到了碰撞检测问题。

I am currently trying to make a Pong clone and I've run into a problem with collision-detection.

每个对象基本上都有特定的速度(即Vector2),Position(也为Vector2)和Speed(只是浮点数)。在对象的每个Update()调用上,位置都是这样更改的:

Each object basicly has a specific Velocity(which is a Vector2), Position(Vector2, also) and Speed(just a float). On every Update() call of the object, the position is changed this way:

Velocity.Normalize();
Position += Velocity * Speed;

首先,我仅检查两个对象之间是否存在使用简单Intersects()的碰撞从对象的矩形调用。我很快意识到,我不仅要检查对象是否正在当前与其他对象碰撞,而且要检查对象是否已与碰撞。仅检查速度是否过高时,当前是否有两个物体在碰撞,使球穿过球拍。

At first, I only checked if there currently was a collision between two objects with a simple Intersects() call from the rectangles of the objects. I quickly realized that I couldn't only check if the object was currently colliding with another, but rather if the object collided with an object on its way. Only checking if two objects were currently colliding made the ball go through the paddle when the speed was too high.

我尝试了其他方法来解决问题,但没有一个似乎有效。我只需要一种方法来检查两个物体是否在途中发生碰撞,如果发生碰撞,是从水平方向,垂直方向还是从两者都发生(以相应地更改球的速度)。

I tried different things to fix the problem, but none of them seemed to work. I only need a way to check if two objects collided on their way, and if they did, if it was from the horizontal, vertical or both(to change the ball's velocity accordingly).

我不一定要立即解决该问题,也许只是如何实现此问题的基本思路,我自己编写。

I don't necessarily want the solution right away, maybe just the basic idea of how to implement this, and I'll code it myself.

谢谢

推荐答案

分隔轴定理是您的朋友:)

http://www.codeproject.com/KB/GDI-plus/PolygonCollision.aspx

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

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