Writing plan for the physical model of the billiard (rigid body)

xiaoxiao2021-03-06  71

Can only provide initial

1. Mutual collision detection between spheres (the radius and mass of the ball is equal)

Input Data: (2D vector representation) The speed of the ball 1 of the ball 1 speed ball 1 rate ball 1 speed = speed direction of the ball 1 × ball 1 Rate of ball 2 Ball 2 speed ball 2 speed ball 2 speed ball 2 speed = speed direction × ball 2 of the ball 2

Calculation process: Ball 1 and the relative speed of the ball 2 RelativeV = Ball 1 speed - the speed of the ball 2 by the ball of the ball 1, the spherical heart coordinates of the ball 1 constitutes a fixed vector, indicating a one-to-one ray The distance between the ball 2 and the distance of the rays If the resulting distance is greater than 2 times radius, the possibility of two balls does not have collisions If the resulting distance is less than or equal to 2 times radius, the two balls have a collision possible to calculate the current frame The collision possibilities In order to increase the calculation accuracy, use dual cycles, separation rendering layers, and logic layers, the rendering speed is 33FPS start time is 0 time intervals to cut each frame 30 time slice calculation T between 0 to 30 Whether there is a point of NewPosition and the ball coordinate of the ball 2 less than or equal to 2 times radius RV = RelativeV / 30; V1 '= V1 / 30; V2' = V2 / 30; While (t <= 30) {t = T; newPosition = position1 rv * t; if (newPosition, position2) <= 2 * radius) {// calculate the ball coordinates and ball 2 of the ball 2 POS1 = POSITION1 V1 ' * (T-1); POS2 = POSITION2 V2 '* (T-1); // Calculate two small balls after collision CollisionResponsebetWeensphere (); newv1; // Ball 1 After collision, NEWV2; // The speed of the ball 2 after collision, NV1 = Newv1 / 30; NV2 = newv2 / 30; POS1 '= POS1 NV1 * (30-T); POS2' = POS2 NV2 * (30-T);}}

2, collision detection of sphere and plane

In a 2D environment, the plane can be regarded as a straight-line input data: the ball's heart coordinate POSITION ball speed direction Direction ball rate Speed ​​ball speed = Ball speed direction × Ball Rate Velocity = Direction * Speed ​​line segment endpoint 1 The coordinate EP (End Point) of the endpoint 2 of the coordinate SP (START POINT) line segment

Calculation process: Method for determination of Normal Calculates the speed direction of the ball and the line segment method vector, determined whether the ball and line segment parallel If DOT is 0, it means that the two vectors are vertical, that is, the direction of movement of the ball is parallel. There is no possibility of collision if the DOT is not 0, indicating that the sphere and the line segment may calculate the possibility of calculating the collision of the current frame by the current coordinates of the ball and the coordinates of the ball after passing the ball. P1P2 STARTPOINT and EndPoint constitute a line segment SE calculation P1P2 and SE if not intersect, then in the current frame, the sphere and the line segment do not collide if intersect, the sphere is collided in the current frame.

P1 = position; p2 = position velocity; // step1 // line segment intersecting fast repellent experiment xmax = max (sp.x, ep.x); xmin = min (sp.x, ep.x); ymax = max (Sp.y, ep.y); ymin = min (sp.y, ep.y); if ((p1.x> = xmin && p1.x <= xmax) && (p1.y> = ymin && p1.y <= YMAX)) Flag1 = true; else flag1 = false; if ((p2.x> = xmin && p2.x <= xmax) && (p2.y> = ymin && p2.y <= ymax)) FLAG2 = true; else flag2 = false ; If (Flag1 || Flag2) // Rapid exclusion experiments set up step1 = true; Else Continue; // Rejection experiment failed, collisions do not occur in // Step2 // line segments intersecting Interchange Experiment IF ((P1-SP) × (EP-SP) * (EP-SP) × (P2-SP)> = 0) {// Collision happens, calculate the collision point m // division: // 1, the slope of the two line segments is present /// The collision point is both on the line segment P1P2 in the line segment SE, resulting in a equation group / / (m-p1) × (p2-p1) = 0 // (m-sp) × (EP-SP) = 0 mx = ((P1 × P2) * (EP.X-SP.X) - (SP × EP) * (P2.x-P1.x)) / (EP-SP) × (P2-P1)); MY = ((P1 × P2) * (EP.Y-SP.Y) - (SP × EP) * (P2.Y-P1.Y)) / ((EP-SP) × (P2-P1)); // 2, the line segment SE and the x-axis parallel // my is sp.y // (m-p1) × (P2-P1) = 0 my = sp.y; MX = ((p1 × p2) * (EP.X- SP.X) - (SP × EP) * (P2.x-P1.x)) / (EP-SP) × (P2-P1)); // 3, line segment SE and Y-axis parallel // mx SP.X / / (m-p1) × (P2-P1) = 0 mx = sp.x; my = ((p1 × p2) * (EP.Y-SP.Y) - (sp × EP) * (SP × EP) * P2.Y-P1.Y)) / (EP-SP) × (P2-P1)); // Calculate the speed of the ball after the collision Collisi OnResponsebetWeenedge (); newvelocity; newpos = m newvelocity * (Distance (M, P2) / DISTANCE (P1, P2));} else continue; 3, collision between spheres CollisionResponsebetWeensphere ();

Input Data: Ball 1 Spherical Coordinate Position Position1 Ball 1 Speed ​​Direction Direction1 Ball 1 Rate Speed1 Ball 1 Speed ​​= Ball 1 Speed ​​Rate = Rate Velocity1 = Direction1 × Speed1 Ball 2 Spherical Coordinate Position Position2 Ball 2 Speed ​​Direction Direction2 Ball 2 Rate Speed2 Ball 2 Speed ​​= Ball 2 Speed ​​Direction × Ball 2 Rate Velocity2 = Direction2 × SPEED2

Calculate: New speed after two pump collisions

Calculation process: Calculate the method of two spherical collisions Normal = (position2-position1) .unit () calculates the initial speed of the two spheroid collision to convert the speed in the XY coordinate system to the NT coordinate system on the N-axis Speed ​​V1n = Normal * (Normal Dot Velocity1) Ball 1 Speed ​​V1T = Velocity1-V1N Ball 2 on the T-axis V2N = Normal * (- Normal Dot Velocity2) Ball 2 on the T-axis V2T = Velocity2-V2N calculates the novel speed NEWV1N = ((V1N * m1) (V2N * m2) - (V1 N-V2N) * M2) / (M1 M2) NEWV2N = ((V1 ) * m1) (V2N * m2) - (V2N-V1N) * m1) / (m1 m2) assumes that M1 = M2 = 1, then: newv1n = (V1N V2N- (V1N-V2N)) / 2 = V2N newv2n = (V1N V2N- (V2N-V1N)) / 2 = V1N calculates two spheres after collision, the new speed new speed new speed new speed new speed new speed after calculation of two spherical collisions NEWVELOCITY1 = Newv1n newv1t newvelocity2 = Newv2n newv2t4, sphere and plane collision CollisionResponsebetWeenedge ();

Enter data: Spherical core coordinate position POSITION sphere speed direction Direction sphere rate SpeED spheroid speed = spherical speed direction × spherical rate Velocity = direction × speed plane Method Normal Normal Normal

Calculating process calculation of vector n = (-velocity * normal) * Normal calculates the new speed after the sphere collision newVelocity = 2 * n velocity

Mathematical Theory: Vector Fork P1 (X1, Y1) P2 (X2, Y2) P1 × P2 = X1 * Y2-X2 * Y1 Vector Distant P1 (X1, Y1) P2 (X2, Y2) P1 * P2 = x1 * X2 Y1 * Y2

转载请注明原文地址:https://www.9cbs.com/read-92393.html

New Post(0)