Hi,
On my current game, I got the following situation (displayed in the
attached drawing):
1 - On instant t0 an ideally circular spell C (displayed as a blue
circle in the image) moves toward a line segment AB in a straight and
predictable collision path with speed (dX, dY)/frame.
2 - On instant t1 the spell is intersecting with the line. The
intersection indicates that the circle has collided with the line.
3 - I am, however, unable to tell how to calculate the exact position
of the circle (displayed as a dotted circle in the image) when it
collided with the line, and there are no instants between t0 and t1.
Here's the math code I have available for calculating the distance
between a circle's origin and a line segment.
------------------------
def dot_product(A, B, C):
'''Computes the dot product AB . BC'''
AB = (B[0]-A[0], B[1]-A[1])
BC = (C[0]-B[0], C[1]-B[1])
return AB[0]*BC[0] + AB[1]*BC[1]
def cross_product(A, B, C):
'''Computes the cross product AB x AC'''
AB = (B[0]-A[0], B[1]-A[1])
BC = (C[0]-B[0], C[1]-B[1])
return AB[0]*BC[1] - AB[1]*BC[0]
def line_point_distance(A, B, P):
'''Computes the distance from the line formed by AB to the point P'''
return fabs(cross_product(A, B, P) / hypot(A, B))
def line_segment_point_distance(A, B, P):
'''Computes the distance from the line segment AB to the point P'''
if dot_product(A, B, P) > 0:
return point_point_distance(B, P)
if dot_product(B, A, P) > 0:
return point_point_distance(A, P)
return fabs(cross_product(A, B, P) / point_point_distance(A, B))
-----------------------
And here's how the test is done:
----------------------
pos0 = C.origin
pos1 = C.get_next_position()
distance = line_segment_point_distance(A, B, pos1)
if distance < C.radius:
print "A collision was detected!"
#MOVE CIRCLE TO COLLISION POINT
else:
C.pos = pos1
----------------------
If I know the circle's position on both t0 and t1 (let's call them
pos0 and pos1, respectively), how do I calculate the exact collision
point?
Thanks,
-Thiago
Attachment:
collision.png
Description: PNG image