crazymage
crazymage

Reputation: 128

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

We have two circles, Circle1 & Circle2, Circle2 is static and Circle1 is moving.

Circle1 = C1, Circle2 = C2.

C1 has a velocity and direction which will cause it to touch C2 at one point.

v is a vector describing C1's velocity. d is the distance from the center point of C1 to the center point of C2.

We also know the radius of both circles.

Frame 1:

C1 has not yet collided with C2, but as we can see it will in the next frame do so.

Frame 2:

C1 is now intersecting with C2.

Frame 2 (after calculation):

C1 has now been positioned at the point where it first touched C2.

So the question is, how can we calculate (preferably in js) the point along v where 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)

// get distance from ri to ri2 as a vector.
var d = new Vector(ri2.x - ri.x, ri2.y - ri.y);
// get sum of radiuses.
var rs = ri.r + ri2.r;

var A = 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 * A * C)) / 2 * A;
var x1 = (-B - Math.sqrt(Math.pow(B, 2) - 4 * A * C)) / 2 * A;

// and then we get the lowest positive of x1 & x2.

Upvotes: 1

Views: 510

Answers (3)

MBo
MBo

Reputation: 80197

Center-center distance in the touch moment is

R12 = R1 + R2

So using cosine theorem:

R122 = v'2 + d2 - 2*d*v'*Cos(dv)

Solve this quadratic equation against v' and get smaller positive solution value (if 2 cases exist).

You can find Cos(dv) through scalar product of vectors d and 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 for simple case: circle radius 2 centered at (0,0), moving right (v = (10,0)); static circle radius 3 centered (6,3). Result should be 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

Upvotes: 1

dpmcmlxxvi
dpmcmlxxvi

Reputation: 1302

You need to solve a few steps

  1. Set up equation of motion and impose criteria that circles touch

    | P1 + v * t - P2 | = R1 + R1

    where P1 and P2 are the initial positions of the circle 1 and 2, respectively, and t is time.

  2. Square the equation and solve for the intersection time using the quadratic equation

  3. Use intersection time to solve for the intersection location of circle 1

  4. Once you have where circle 1 is at the moment of intersection, and since circle 2 is static, solve circle-circle intersection problem

Upvotes: 1

Gal Samuel
Gal Samuel

Reputation: 493

for each x,y on your V line calculate the distance between the centers of C1 and C2. it the distance equals to r1+r2 then the circles touch eternally.

P2(x2,y2) is center of C2 P1(x1,y1) is calculated center for C1 moving on V

P1P2 distance is:

SQuare Root of: (x2-x1)^2+(y2-y1)^2

once this equals r1+r2 you should stop

Distnace function for javascript is:

var d = Math.sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) );

or if you wish:

var dist = Math.sqrt( Math.pow((x1-x2), 2) + Math.pow((y1-y2), 2) );

Upvotes: 0

Related Questions