Detect collision between a moving object and an immobile one

一世执手 提交于 2019-12-22 05:10:09

问题


First of all, my question isn't really specific to C# or XNA, but my code examples will use these.

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

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;

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.

Thanks for your time.


回答1:


The Separating Axis Theorem is your friend :)

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




回答2:


As a starting point, have a look here.

http://www.flipcode.com/archives/Theory_Practice-Issue_01_Collision_Detection.shtml

This is a very good introduction about all the different collision ways. Maybe your case is explained here.




回答3:


I think this link: http://www.gamasutra.com/view/feature/3383/simple_intersection_tests_for_games.php might be what you're looking for. It describes the sphere-plane sweep test, useful for when you have fast moving objects that may pass through a plane within a one-frame interval.

It gives you the intersection point as well, which you can use to reflect your trajectory about the plane normal and continue the path of the object.




回答4:


You are having problem with the fact that if one object is too fast, it can pass the immobile object before an Update() is called with the detection (like it passes through the immobile object).

Extend the shape of the object along the move vector with the size of speed: Square [0,0][2,2] with velocity [1,0] and speed 10 will create a shape of Rectangle [0,0][12,2] => it is now positioned at coords [0,0] with size [12,2].

Now intersected the rectangle with the immobile object. Now you know if they collided.




回答5:


If two point objects have the same position, then they have collided.



来源:https://stackoverflow.com/questions/3195495/detect-collision-between-a-moving-object-and-an-immobile-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!