开发者

Detect collision between a moving object and an immobile one

开发者 https://www.devze.com 2023-01-06 04:34 出处:网络
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.

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 w开发者_开发问答ay 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.


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.


The Separating Axis Theorem is your friend :)

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


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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消