开发者

Simple Problem - Velocity and Collisions

开发者 https://www.devze.com 2022-12-13 15:51 出处:网络
Okay I\'m working on a Space sim and as most space sims I need to work out where the opponents ship will be (the 3d position) when my bul开发者_Python百科let reaches it. How do I calculate this from t

Okay I'm working on a Space sim and as most space sims I need to work out where the opponents ship will be (the 3d position) when my bul开发者_Python百科let reaches it. How do I calculate this from the velocity that bullets travel at and the velocity of the opponents ship?


Calculate the relative velocity vector between him and yourself: this could be considered his movement if you were standing still. Calculate his relative distance vector. Now you know that he is already D away and is moving V each time unit. You have V' to calculate, and you know it's length but not it's direction.

Now you are constructing a triangle with these two constraints, his V and your bullet's V'. In two dimensions it'd look like:

Dx+Vx*t = V'x*t

Dy+Vy*t = V'y*t

V'x^2 + V'y^2 = C^2

Which simplifies to:

(Dx/t+Vx)^2 + (Dy/t+Vx)^2 = C^2

And you can use the quadratic formula to solve that. You can apply this technique in three dimensions similarly. There are other ways to solve this, but this is just simple algebra instead of vector calculus.


Collision Detection by Kurt Miller
http://www.gamespp.com/algorithms/collisionDetection.html


Add the negative velocity of the ship to the bullet, so that only the bullet moves. Then calculate the intersection of the ship's shape and the line along which the bullet travels (*pos --> pos + vel * dt*).


The question probably shouldn't be "where the ship will be when the bullet hits it," but IF the bullet hits it. Assuming linear trajectory and constant velocity, calculate the intersection of the two vectors, one representing the projectile path and another representing that of the ship. You can then determine the time that each object (ship and bullet) reach that point by dividing the distance from the original position to the intersection position by the velocity of each. If the times match, you have a collision and the location at which it occurs.

If you need more precise collision detection, you can use something like a simple BSP tree which will give you not only a fast way to determine collisions, but what surface the collision occurred on and, if handled correctly, the exact 3d location of the collision. However, it can be challenging to maintain such a tree in a dynamic environment.

0

精彩评论

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