Posts: 704
Threads: 0
Joined: Dec 2002
'
' Consider two circular objects moving with velocity vectors (V1X,V1Y)
' and (V2X,V2Y). At time = 0, their centers are at (X10,Y10) and
' (X20,Y20). This function calculates the "collision geometry." It
' returns directly the clockwise angle (radians, measured from the x-axis)
' about the center of the second circle the point on the perimeter of the
' two circles at which they meet. TCOL is output via the parameter list.
' It is the time at which the two circles collide. Before using the
' result returned via ANGLE, check the value of TCOL. If it is negative,
' the indication is that a collision was not possible, either because they
' are on parallel paths too far apart for a collision to occur or because
' they are moving away from each other.
'
' If you want to know where the circle centers are at TCOL:
'
' X1 = X10 + V1X * TCOL,
'
' Y1 = Y10 + V1Y * TCOL,
'
' X2 = X20 + V2X * TCOL,
'
' and
'
' Y2 = Y20 + V2Y * TCOL.
'
' Once having those, if you want to know the coordinates of the actual
' point of collision (where the two circles touch), it's
'
' XC = X2 + A2 * COS(ANGLE)
'
' and
'
' YC = Y2 + A2 * SIN(ANGLE).
'
' Your main routine needs the following DECLARE statement.
'
' DECLARE FUNCTION ANGLE(X10,Y10,X20,Y20,V1X,V1Y,V2X,V2Y,A1,A2,TCOL)
'
DEFSNG A-Z
FUNCTION ANGLE(X10,Y10,X20,Y20,V1X,V1Y,V2X,V2Y,A1,A2,TCOL)
PI=4*ATN(1)
'
' Define dummy angle and collision time in case there is no collision.
'
TEMPANGLE=0.
TCOL=-1.
'
' Get coefficients in quadratic equation.
'
A=(V1X-V2X)^2+(V1Y-V2Y)^2
B=2*((X10-X20)*(V1X-V2X)+(Y10-Y20)*(V1Y-V2Y))
C=(X10-X20)^2+(Y10-Y20)^2-(A1+A2)^2
'
' Get expression under "quadratic radical" and test it to make sure
' collision can actually occur, or did occur, before trying to solve for
' collision time.
'
Q=B^2-4*A*C
IF Q>=0 THEN
'
' Q isn't negative. Collision is physically possible. Get two times at
' which balls are just touching.
'
IF A<>0 THEN
T1=(SQR(Q)-B)/2/A : T2=-(B+SQR(Q))/2/A
'
' If neither T1 nor T2 is positive (or zero), there is no collision after
' the balls were at (X10,Y10) and (X20,Y20). If only one of them is non-
' negative, *that's* the one that means something. If they're both non-
' negative, use the minimum of the two.
'
IF T1>=0 OR T2>=0 THEN
IF T1>=0 AND T2>=0 THEN
TCOL=T1
IF T2<T1 THEN TCOL=T2
ELSEIF T1>=0 THEN
TCOL=T1
ELSE
TCOL=T2
END IF
END IF
ELSE
IF B<>0 THEN TCOL=-C/B
END IF
END IF
IF TCOL>=0 THEN
'
' Okay, get angle on circle 2 at which collision occurs. Circles will be
' at (X1,Y1) and (X2,Y2).
'
X1=X10+V1X*TCOL : Y1=Y10+V1Y*TCOL : X2=X20+V2X*TCOL : Y2=Y20+V2Y*TCOL
TEMPANGLE=(PI/2)*SGN(Y1-Y2)
IF X1<>X2 THEN TEMPANGLE=ATN((Y1-Y2)/(X1-X2))
IF X1<X2 THEN TEMPANGLE=TEMPANGLE+PI
END IF
ANGLE=TEMPANGLE
END FUNCTION
ravelling Curmudgeon
(geocities sites require copying and pasting URLs.)
I liked spam better when it was something that came in a can.
Windows should be defenestrated.