c# - At what delta time will two objects collide? -


my apologies if has been posted before. can't seem find on topic, via stackoverflow, nor of other google searches.

i have 2 objects, both containing position (x , y), linear velocity (x , y). these objects moving through 2d plane. need detect when these objects collide, if collide @ all. big problem situation radius of objects, effect when touch.

i've tried various solutions problem on period of month, i'm not hacking it. i've managed do, calculate 2 straight line formulas object, , end b , c both (y=mx + c). i'm trying compare these 2 formulas each other, x=x , y=y, end complex equation, , no idea how translate c#.

this function needs fire in <50ms well, complicate matters. advice go long way.

you can formulate problem system of 2 vector equations

p1 = u1 + v1 t p2 = u2 + v2 t 

where p1 position of first object given initial position u1, velocity v1, , scalar time t. want solve moment objects touch, described by

r1 + r2 = |p1 - p2| 

where r1 , r2 radii of objects. let's define variables our derivation little cleaner:

x  = u1.x - u2.x y  = u1.y - u2.y x' = v1.x - v2.x y' = v1.y - v2.y 

then can proceed solve follows:

    r1 + r2    = |p1 - p2|                = sqrt((p1.x - p2.x)^2 + (p1.y - p2.y)^2)                 definition of vector magnitude  => (r1 + r2)^2 = (p1.x - p2.x)^2 + (p1.y - p2.y)^2                       square both sides                = (x + x' t)^2 + (y + y' t)^2                             substitute variable definitions                = x^2 + 2 x x' t + x'^2 t^2 + y^2 + 2 y y' t + y'^2 t^2   expand squares of sums                = (x'^2 + y'^2) t^2 + 2 (x x' + y y') t + x^2 + y^2       rearrange terms  => (x'^2 + y'^2) t^2 + 2 (x x' + y y') t + x^2 + y^2 - (r1 + r2)^2 = 0   rearrange terms 

this quadratic equation, can find t using quadratic formula with

a = x'^2 + y'^2 b = 2 (x x' + y y') c = x^2 + y^2 - (r1 + r2)^2 

if objects don't collide, discriminant b^2 - 4 c negative. if objects collide in past, values t may negative.

speaking of which, how interpret fact obtain 2 roots, or 2 values of t? consider following image:

enter image description here

as can see, obtain solutions first contact, "entry", when objects intersect , part, "exit". can useful depending on needs, if need contact time, take smaller t.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -