简体   繁体   中英

C# maths, collision detection, xna

Im doing collision detection in a game. All the surfaces are orthagonal so I only need to determine which face of an obstacle my moving object has collided with. I have the rectangle defining the intersection of the two objects and the vector representing the moving objects speed and direction of movement.

I reckon I need to translate the intersection rectangle along my moving objects vector until the intersection becomes a line, then I'll know which face was collided with 1st.

I have no idea how to do this mathematically of programatically however 在此处输入图片说明

在此处输入图片说明

Calculate the vector from (the corner of the obstacle internal to your object) to (the corner of your object internal to the obstacle). Whichever "side" of this vector your movement vector is on gives the "side" that touched first. If they have the same angle, the corner touched first.

Eg here your movement vector is at about 260 degrees and then calculated vector is at about 240 degrees. Anticlockwise from calculated to movement is 20 degrees, clockwise is 340 degrees. Thus the anticlockwise side (bottom) collided first.

You need able to tell which two sides are in question, and which is "clockwise" and "anticlockwise" - I hope this is trivial.

You can compare the aspect ratio of the velocity vector to the aspect ratio of the intersection rectangle.

For this particular example, if the velocity vector is steeper than the intersection rectangle (ie defines a taller and skinnier rectangle), then the collision was on the bottom face.

If the velocity vector is shallower, then the collision was on the left face of the moving rectangle.

If the velocity vector is the same aspect ratio as the intersection rectangle (ie the velocity lays on the diagonal of the intersection rectangle), then they collided on the corners.

Actually I may have figured it out...

  • Find the point on the intersection rectangle that isnt on the objects rectangle

  • draw a line from there in the vectors direction

  • whatever side it intersects with is the side that collided 1st

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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