javascript - Calculating the point where two circles touch based on circle1's velocity -


we have 2 circles, circle1 & circle2, circle2 static , circle1 moving.

circle1 = c1, circle2 = c2.

c1 has velocity , direction cause touch c2 @ 1 point.

v vector describing c1's velocity. d distance center point of c1 center point of c2.

we know radius of both circles.

frame 1:

c1 has not yet collided c2, can see in next frame so.

frame 2:

c1 intersecting c2.

frame 2 (after calculation):

c1 has been positioned @ point first touched c2.

so question is, how can calculate (preferably in js) point along v c1 should stop?

current code:

//    x^2 + b * x + c = 0 //    x= v' //    b = 2 * (d.x * v.x + d.y*v.y)/math.sqrt(v.x*v.x + v.y*v.y) //    c = (d.length()^2 - rs^2)  // distance ri ri2 vector. var d = new vector(ri2.x - ri.x, ri2.y - ri.y); // sum of radiuses. var rs = ri.r + ri2.r;  var = 1; var b = 2 * (d.x * v.x + d.y*v.y)/math.sqrt(v.x*v.x + v.y*v.y); var c = (d.length()^2 - rs^2);  var x1 = (-b + math.sqrt(math.pow(b, 2) - 4 * * c)) / 2 * a; var x1 = (-b - math.sqrt(math.pow(b, 2) - 4 * * c)) / 2 * a;  // , lowest positive of x1 & x2. 

center-center distance in touch moment is

r12 = r1 + r2 

so using cosine theorem:

r122 = v'2 + d2 - 2*d*v'*cos(dv)

solve quadratic equation against v' , smaller positive solution value (if 2 cases exist).

you can find cos(dv) through scalar product of vectors d , v

cos(dv) = d * v / (|d||v|)

so final quadratic equation is

v'2 - v' * 2 * (d * v) / |v| + (d2 - r122) = 0

for standart form

x^2 + b * x + c = 0     x= v' b =  -2 * (d.x * v.x + d.y*v.y)/sqrt(v.x*v.x + v.y*v.y) c = (d^2 - r12^2)  

check simple case: circle radius 2 centered @ (0,0), moving right (v = (10,0)); static circle radius 3 centered (6,3). result should v'=2

b = -2*(6*10+3*0)/10= -12 c=45-(2+3)^2=20 determinant = b*b - 4*c = 144-80 = 64 v'= (12 +- 8)/2 smaller value v'=2 

enter image description here


Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -