Delphi custom animation - collision detection

后端 未结 4 377
半阙折子戏
半阙折子戏 2021-01-12 00:35

I\'m working with custom drawing / 2D animation and I\'m trying to figure out how to detect when the moving object collides with a wall in the map. User holds arrow keys on

4条回答
  •  广开言路
    2021-01-12 01:23

    Every time the key is pressed, you compute the new coordinate of the object after the move would be executed. Then you can test for intersections between the object trajectory and the line in the map.

    Since your map can be considered a set of line segments, and given that your object path is linear, you can find all the possible collisions by finding intersections between the object path and the lines on which the segments of your map lie. You will only have two slopes for the object path: zero and infinity. So for each map segment:

    1. Compute its slope. If the map segment slope is the same as object path slope, they will not intersect.
    2. Compute the intersection between the lines that the map segment and the object path are one (see here for instance)
    3. Check if the map segment ends before the collision point: if yes, then no collision
    4. Check if the object path ends before the collision point: if yes, then no collision

提交回复
热议问题