Advertisement
bero1985

Bilateral advancement

May 28th, 2015
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 17.28 KB | None | 0 0
  1. // Bilateral advancement
  2. function TEnginePhysics.GetTimeOfImpactForContinuousCollisionDetection(const ShapeA:TEnginePhysicsShape;const SweepA:TEnginePhysicsSweep;const ShapeB:TEnginePhysicsShape;const ShapeBTriangleIndex:longint;const SweepB:TEnginePhysicsSweep;const DeltaTime:single;const ThreadIndex:longint;var Beta:single):boolean;
  3. const sfmNONE=0;
  4.       sfmVERTICES=1;
  5.       sfmEDGEA=2;
  6.       sfmEDGEB=3;
  7.       sfmFACEA=4;
  8.       sfmFACEB=5;
  9.       sfmEDGES=6;
  10. var Iteration,TryIteration,RootIteration,Index,SubIndex,SeparationFunctionMode,
  11.     ia0,ia1,ia2,ib0,ib1,ib2,ic0,ic1,ic2,a0i,a1i,b0i,b1i:longint;
  12.     t0,t1,s0,s1,a0,a1,t,s,tS,tT0,tT1,TotalRadius,Target,Tolerance,Distance,CurrentDistance,L:single;
  13.     Unprocessed,Overlapping:boolean;
  14.     SegmentTriangle:TSegmentTriangle;
  15.     ShapeTriangle:TEnginePhysicsShapeTriangle;
  16.     MeshShape:TEnginePhysicsShapeMesh;
  17.     MeshTriangle:PEnginePhysicsMeshTriangle;
  18.     Shapes:array[0..1] of TEnginePhysicsShape;
  19.     Points,WitnessPoints:array[0..1] of TVector3;
  20.     TempPoints,Normals:array[0..1] of TVector3;
  21.     Transforms:array[0..1] of TMatrix4x4;
  22.     Axis,LocalVertex,va0,va1,va2,va,vb0,vb1,vb2,vb,eA,eB:TVector3;
  23.     Plane:TPlane;
  24.     FeatureGJKState:TEnginePhysicsFeatureGJKState;
  25.     VertexIndexRows:array[0..2] of PEnginePhysicsFeatureGJKStateVertexIndexSimplexRow;
  26. begin
  27.  
  28.  result:=false;
  29.  
  30.  Shapes[0]:=ShapeA;
  31.  
  32.  if (ShapeBTriangleIndex>=0) and (ShapeB is TEnginePhysicsShapeMesh) then begin
  33.   MeshShape:=TEnginePhysicsShapeMesh(ShapeB);
  34.   ShapeTriangle:=TEnginePhysicsShapeTriangle(TriangleShapes[ThreadIndex]);
  35.   Shapes[1]:=ShapeTriangle;
  36.   MeshTriangle:=@MeshShape.Mesh.Triangles[ShapeBTriangleIndex];
  37.   ShapeTriangle.LocalTransform:=MeshShape.LocalTransform;
  38.   ShapeTriangle.WorldTransform:=MeshShape.WorldTransform;
  39.   ShapeTriangle.ConvexHull.Vertices[0].Position:=MeshShape.Mesh.Vertices[MeshTriangle^.Vertices[0]];
  40.   ShapeTriangle.ConvexHull.Vertices[1].Position:=MeshShape.Mesh.Vertices[MeshTriangle^.Vertices[1]];
  41.   ShapeTriangle.ConvexHull.Vertices[2].Position:=MeshShape.Mesh.Vertices[MeshTriangle^.Vertices[2]];
  42.   ShapeTriangle.UpdateData;
  43.  end else begin
  44.   Shapes[1]:=ShapeB;
  45.  end;
  46.  
  47.  TotalRadius:=Shapes[0].FeatureRadius+Shapes[1].FeatureRadius;
  48.  
  49.  Target:=Max(LinearSlop,TotalRadius-(3.0*LinearSlop));
  50.  
  51.  Tolerance:=LinearSlop*0.25;
  52.  
  53.  ClearFeatureGJKState(FeatureGJKState);
  54.  
  55.  FeatureGJKState.Shapes[0]:=Shapes[0];
  56.  FeatureGJKState.Shapes[1]:=Shapes[1];
  57.  FeatureGJKState.Transforms[0]:=@Transforms[0];
  58.  FeatureGJKState.Transforms[1]:=@Transforms[1];
  59.  FeatureGJKState.UseRadii:=false;
  60.  
  61.  // Compute current closest features. Setup a separation function to evaluate
  62.  // overlap on the axis between the closest features. Terminate if closest
  63.  // features are repeated.
  64.  
  65.  t0:=0.0;
  66.  
  67.  Axis:=Vector3Origin;
  68.  
  69.  SeparationFunctionMode:=sfmNONE;
  70.  
  71.  for Iteration:=1 to TimeOfImpactMaximumIterations do begin
  72.  
  73.   Transforms[0]:=Matrix4x4TermMul(Shapes[0].LocalTransform,SweepTransform(SweepA,t0));
  74.   Transforms[1]:=Matrix4x4TermMul(Shapes[1].LocalTransform,SweepTransform(SweepB,t0));
  75.  
  76.   FeatureGJKState.SimplexSize:=0;
  77.   FeatureGJKState.Initialized:=false;
  78.   if not FeatureGJKRun(FeatureGJKState) then begin
  79.    exit;
  80.   end;
  81.  
  82.   // TOI is not defined if shapes began in an overlapping configuration
  83.   if FeatureGJKState.Distance<EPSILON then begin
  84.    Beta:=0.0;
  85.    result:=false;
  86.    exit;
  87.   end;
  88.  
  89.   // Check for initial convergent state
  90.   if FeatureGJKState.Distance<(Target+Tolerance) then begin
  91.    Beta:=t0;
  92.    result:=true;
  93.    exit;
  94.   end;
  95.  
  96.   case FeatureGJKState.SimplexSize of
  97.    1:begin
  98.     // Vertex-vertex
  99.     VertexIndexRows[0]:=@FeatureGJKState.VertexIndexSimplices[FeatureGJKState.Permutation[0]];
  100.     Axis:=Vector3NormEx(Vector3Sub(Shapes[1].GetLocalFeatureSupportVertex(VertexIndexRows[0]^[1]),
  101.                                    Shapes[0].GetLocalFeatureSupportVertex(VertexIndexRows[0]^[0])));
  102.     SeparationFunctionMode:=sfmVERTICES;
  103.    end;
  104.    2:begin
  105.     VertexIndexRows[0]:=@FeatureGJKState.VertexIndexSimplices[FeatureGJKState.Permutation[0]];
  106.     VertexIndexRows[1]:=@FeatureGJKState.VertexIndexSimplices[FeatureGJKState.Permutation[1]];
  107.     ia0:=VertexIndexRows[0]^[0];
  108.     ia1:=VertexIndexRows[1]^[0];
  109.     ib0:=VertexIndexRows[0]^[1];
  110.     ib1:=VertexIndexRows[1]^[1];
  111.     if ia0<>ia1 then begin
  112.      // Edge-vertex
  113.      va0:=Shapes[0].GetLocalFeatureSupportVertex(ia0);
  114.      va1:=Shapes[0].GetLocalFeatureSupportVertex(ia1);
  115.      vb0:=Vector3TermMatrixMulInverted(Vector3TermMatrixMul(Shapes[1].GetLocalFeatureSupportVertex(ib0),Transforms[1]),Transforms[0]);
  116.      Axis:=Vector3Sub(va1,va0);
  117.      Axis:=Vector3NormEx(Vector3Cross(Vector3Cross(Axis,Vector3Sub(vb0,va0)),Axis));
  118.      LocalVertex:=va0;
  119.      SeparationFunctionMode:=sfmEDGEA;
  120.     end else if ib0<>ib1 then begin
  121.      // Vertex-edge
  122.      va0:=Vector3TermMatrixMulInverted(Vector3TermMatrixMul(Shapes[0].GetLocalFeatureSupportVertex(ia0),Transforms[0]),Transforms[1]);
  123.      vb0:=Shapes[1].GetLocalFeatureSupportVertex(ib0);
  124.      vb1:=Shapes[1].GetLocalFeatureSupportVertex(ib1);
  125.      Axis:=Vector3Sub(vb1,vb0);
  126.      Axis:=Vector3NormEx(Vector3Cross(Vector3Cross(Axis,Vector3Sub(va0,vb0)),Axis));
  127.      LocalVertex:=vb0;
  128.      SeparationFunctionMode:=sfmEDGEB;
  129.     end else begin
  130.      // Vertex-vertex
  131.      Axis:=Vector3NormEx(Vector3Sub(Shapes[1].GetLocalFeatureSupportVertex(ib0),Shapes[0].GetLocalFeatureSupportVertex(ia0)));
  132.      SeparationFunctionMode:=sfmVERTICES;
  133.     end;
  134.    end;
  135.    3:begin
  136.     VertexIndexRows[0]:=@FeatureGJKState.VertexIndexSimplices[FeatureGJKState.Permutation[0]];
  137.     VertexIndexRows[1]:=@FeatureGJKState.VertexIndexSimplices[FeatureGJKState.Permutation[1]];
  138.     VertexIndexRows[2]:=@FeatureGJKState.VertexIndexSimplices[FeatureGJKState.Permutation[2]];
  139.     ia0:=VertexIndexRows[0]^[0];
  140.     ia1:=VertexIndexRows[1]^[0];
  141.     ia2:=VertexIndexRows[2]^[0];
  142.     ib0:=VertexIndexRows[0]^[1];
  143.     ib1:=VertexIndexRows[1]^[1];
  144.     ib2:=VertexIndexRows[2]^[1];
  145.     if (ia0<>ia1) and (ia0<>ia2) and (ia1<>ia2) then begin
  146.      // Face-vertex or face-edge
  147.      if (ib0<>ib1) and ((ib0=ib2) or (ib1=ib2)) then begin
  148.       // TO-DO: Do correct face-edge handling
  149.       va0:=Shapes[0].GetLocalFeatureSupportVertex(ia0);
  150.       va1:=Shapes[0].GetLocalFeatureSupportVertex(ia1);
  151.       va2:=Shapes[0].GetLocalFeatureSupportVertex(ia2);
  152.       vb0:=Vector3TermMatrixMulInverted(Vector3TermMatrixMul(Shapes[1].GetLocalFeatureSupportVertex(ib0),Transforms[1]),Transforms[0]);
  153.       Axis:=Vector3NormEx(Vector3Cross(Vector3Sub(va0,va1),Vector3Sub(va2,va1)));
  154.       if Vector3Dot(vb0,Axis)<0.0 then begin
  155.        Axis:=Vector3Neg(Axis);
  156.       end;
  157.       LocalVertex:=va0;
  158.       SeparationFunctionMode:=sfmFACEA;
  159.      end else if (ib0<>ib2) and ((ib0=ib1) or (ib1=ib2)) then begin
  160.       // TO-DO: Do correct face-edge handling
  161.       va0:=Shapes[0].GetLocalFeatureSupportVertex(ia0);
  162.       va1:=Shapes[0].GetLocalFeatureSupportVertex(ia1);
  163.       va2:=Shapes[0].GetLocalFeatureSupportVertex(ia2);
  164.       vb0:=Vector3TermMatrixMulInverted(Vector3TermMatrixMul(Shapes[1].GetLocalFeatureSupportVertex(ib0),Transforms[1]),Transforms[0]);
  165.       Axis:=Vector3NormEx(Vector3Cross(Vector3Sub(va0,va1),Vector3Sub(va2,va1)));
  166.       if Vector3Dot(vb0,Axis)<0.0 then begin
  167.        Axis:=Vector3Neg(Axis);
  168.       end;
  169.       LocalVertex:=va0;
  170.       SeparationFunctionMode:=sfmFACEA;
  171.      end else begin
  172.       // Face-vertex
  173.       va0:=Shapes[0].GetLocalFeatureSupportVertex(ia0);
  174.       va1:=Shapes[0].GetLocalFeatureSupportVertex(ia1);
  175.       va2:=Shapes[0].GetLocalFeatureSupportVertex(ia2);
  176.       vb0:=Vector3TermMatrixMulInverted(Vector3TermMatrixMul(Shapes[1].GetLocalFeatureSupportVertex(ib0),Transforms[1]),Transforms[0]);
  177.       Axis:=Vector3NormEx(Vector3Cross(Vector3Sub(va0,va1),Vector3Sub(va2,va1)));
  178.       if Vector3Dot(vb0,Axis)<0.0 then begin
  179.        Axis:=Vector3Neg(Axis);
  180.       end;
  181.       LocalVertex:=va0;
  182.       SeparationFunctionMode:=sfmFACEA;
  183.      end;
  184.     end else if (ib0<>ib1) and (ib0<>ib2) and (ib1<>ib2) then begin
  185.      // Vertex-face or edge-face
  186.      if (ia0<>ia1) and ((ia0=ia2) or (ia1=ia2)) then begin
  187.       // TO-DO: Do correct face-edge handling
  188.       va0:=Vector3TermMatrixMulInverted(Vector3TermMatrixMul(Shapes[0].GetLocalFeatureSupportVertex(ia0),Transforms[0]),Transforms[1]);
  189.       vb0:=Shapes[1].GetLocalFeatureSupportVertex(ib0);
  190.       vb1:=Shapes[1].GetLocalFeatureSupportVertex(ib1);
  191.       vb2:=Shapes[1].GetLocalFeatureSupportVertex(ib2);
  192.       Axis:=Vector3NormEx(Vector3Cross(Vector3Sub(vb0,vb1),Vector3Sub(vb2,vb1)));
  193.       if Vector3Dot(va0,Axis)<0 then begin
  194.        Axis:=Vector3Neg(Axis);
  195.       end;
  196.       LocalVertex:=vb0;
  197.       SeparationFunctionMode:=sfmFACEB;
  198.      end else if (ia0<>ia2) and ((ia0=ia1) or (ia1=ia2)) then begin
  199.       // TO-DO: Do correct face-edge handling
  200.       va0:=Vector3TermMatrixMulInverted(Vector3TermMatrixMul(Shapes[0].GetLocalFeatureSupportVertex(ia0),Transforms[0]),Transforms[1]);
  201.       vb0:=Shapes[1].GetLocalFeatureSupportVertex(ib0);
  202.       vb1:=Shapes[1].GetLocalFeatureSupportVertex(ib1);
  203.       vb2:=Shapes[1].GetLocalFeatureSupportVertex(ib2);
  204.       Axis:=Vector3NormEx(Vector3Cross(Vector3Sub(vb0,vb1),Vector3Sub(vb2,vb1)));
  205.       if Vector3Dot(va0,Axis)<0.0 then begin
  206.        Axis:=Vector3Neg(Axis);
  207.       end;
  208.       LocalVertex:=vb0;
  209.       SeparationFunctionMode:=sfmFACEB;
  210.      end else begin
  211.       // Face-vertex
  212.       va0:=Vector3TermMatrixMulInverted(Vector3TermMatrixMul(Shapes[0].GetLocalFeatureSupportVertex(ia0),Transforms[0]),Transforms[1]);
  213.       vb0:=Shapes[1].GetLocalFeatureSupportVertex(ib0);
  214.       vb1:=Shapes[1].GetLocalFeatureSupportVertex(ib1);
  215.       vb2:=Shapes[1].GetLocalFeatureSupportVertex(ib2);
  216.       Axis:=Vector3NormEx(Vector3Cross(Vector3Sub(vb0,vb1),Vector3Sub(vb2,vb1)));
  217.       if Vector3Dot(va0,Axis)<0.0 then begin
  218.        Axis:=Vector3Neg(Axis);
  219.       end;
  220.       LocalVertex:=vb0;
  221.       SeparationFunctionMode:=sfmFACEB;
  222.      end;
  223.     end else begin
  224.      // Vertex-vertex or edge-vertex or vertex-edge or edge-edge
  225.      if (ia0<>ia1) and ((ia0=ia2) or (ia1=ia2)) then begin
  226.       a0i:=ia0;
  227.       a1i:=ia1;
  228.      end else if (ia0<>ia2) and ((ia0=ia1) or (ia1=ia2)) then begin
  229.       a0i:=ia0;
  230.       a1i:=ia2;
  231.      end else begin
  232.       a0i:=ia0;
  233.       a1i:=ia0;
  234.      end;
  235.      if (ib0<>ib1) and ((ib0=ib2) or (ib1=ib2)) then begin
  236.       b0i:=ib0;
  237.       b1i:=ib1;
  238.      end else if (ib0<>ib2) and ((ib0=ib1) or (ib1=ib2)) then begin
  239.       b0i:=ib0;
  240.       b1i:=ib2;
  241.      end else begin
  242.       b0i:=ib0;
  243.       b1i:=ib0;
  244.      end;
  245.      if a0i<>a1i then begin
  246.       if b0i<>b1i then begin
  247.        // Edge-edge
  248.        va0:=Shapes[0].GetLocalFeatureSupportVertex(a0i);
  249.        va1:=Shapes[0].GetLocalFeatureSupportVertex(a1i);
  250.        vb0:=Vector3TermMatrixMulInverted(Vector3TermMatrixMul(Shapes[1].GetLocalFeatureSupportVertex(ib0),Transforms[1]),Transforms[0]);
  251.        vb1:=Vector3TermMatrixMulInverted(Vector3TermMatrixMul(Shapes[1].GetLocalFeatureSupportVertex(ib1),Transforms[1]),Transforms[0]);
  252.        eA:=Vector3Sub(va1,va0);
  253.        eB:=Vector3Sub(vb1,vb0);
  254.        Axis:=Vector3NormEx(Vector3Cross(eA,eB));
  255.        if Vector3Dot(Vector3Sub(eB,eA),Axis)<0.0 then begin
  256.         Axis:=Vector3Neg(Axis);
  257.        end;
  258.        LocalVertex:=eA;
  259.        SeparationFunctionMode:=sfmEDGES;
  260.       end else begin
  261.        // Edge-vertex
  262.        va0:=Shapes[0].GetLocalFeatureSupportVertex(a0i);
  263.        va1:=Shapes[0].GetLocalFeatureSupportVertex(a1i);
  264.        vb0:=Vector3TermMatrixMulInverted(Vector3TermMatrixMul(Shapes[1].GetLocalFeatureSupportVertex(b0i),Transforms[1]),Transforms[0]);
  265.        Axis:=Vector3Sub(va1,va0);
  266.        Axis:=Vector3NormEx(Vector3Cross(Vector3Cross(Axis,Vector3Sub(vb0,va0)),Axis));
  267.        LocalVertex:=va0;
  268.        SeparationFunctionMode:=sfmEDGEA;
  269.       end;
  270.      end else begin
  271.       if b0i<>b1i then begin
  272.        // Vertex-edge
  273.        va0:=Vector3TermMatrixMulInverted(Vector3TermMatrixMul(Shapes[0].GetLocalFeatureSupportVertex(a0i),Transforms[0]),Transforms[1]);
  274.        vb0:=Shapes[1].GetLocalFeatureSupportVertex(b0i);
  275.        vb1:=Shapes[1].GetLocalFeatureSupportVertex(b1i);
  276.        Axis:=Vector3Sub(vb1,vb0);
  277.        Axis:=Vector3NormEx(Vector3Cross(Vector3Cross(Axis,Vector3Sub(va0,vb0)),Axis));
  278.        LocalVertex:=vb0;
  279.        SeparationFunctionMode:=sfmEDGEB;
  280.       end else begin
  281.        // Vertex-vertex
  282.        Axis:=Vector3NormEx(Vector3Sub(Shapes[1].GetLocalFeatureSupportVertex(b0i),Shapes[0].GetLocalFeatureSupportVertex(a0i)));
  283.        SeparationFunctionMode:=sfmVERTICES;
  284.       end;
  285.      end;
  286.     end;
  287.    end;
  288.   end;
  289.  
  290.   // Successively resolve the deepest point to compute the TOI, loop is bounded by the number of vertices to be resolved.
  291.   t1:=1.0;
  292.   for TryIteration:=1 to 64 do begin
  293.  
  294.    // Compute deepest witness points at t1
  295.    Transforms[0]:=Matrix4x4TermMul(Shapes[0].LocalTransform,SweepTransform(SweepA,t1));
  296.    Transforms[1]:=Matrix4x4TermMul(Shapes[1].LocalTransform,SweepTransform(SweepB,t1));
  297.    case SeparationFunctionMode of
  298.     sfmVERTICES:begin
  299.      WitnessPoints[0]:=Shapes[0].GetLocalFeatureSupportVertex(Shapes[0].GetLocalFeatureSupportIndex(Vector3TermMatrixMulTransposedBasis(Axis,Transforms[0])));
  300.      WitnessPoints[1]:=Shapes[1].GetLocalFeatureSupportVertex(Shapes[1].GetLocalFeatureSupportIndex(Vector3TermMatrixMulTransposedBasis(Vector3Neg(Axis),Transforms[1])));
  301.      s1:=Vector3Dot(Axis,Vector3Sub(Vector3TermMatrixMul(WitnessPoints[1],Transforms[1]),Vector3TermMatrixMul(WitnessPoints[0],Transforms[0])));
  302.     end;
  303.     sfmEDGEA,sfmFACEA,sfmEDGES:begin
  304.      WitnessPoints[1]:=Shapes[1].GetLocalFeatureSupportVertex(Shapes[1].GetLocalFeatureSupportIndex(Vector3Neg(Vector3TermMatrixMulTransposedBasis(Vector3TermMatrixMulBasis(Axis,Transforms[0]),Transforms[1]))));
  305.      s1:=Vector3Dot(Axis,Vector3Sub(Vector3TermMatrixMulInverted(Vector3TermMatrixMul(WitnessPoints[1],Transforms[1]),Transforms[0]),LocalVertex));
  306.     end;
  307.     sfmEDGEB,sfmFACEB:begin
  308.      WitnessPoints[0]:=Shapes[0].GetLocalFeatureSupportVertex(Shapes[01].GetLocalFeatureSupportIndex(Vector3Neg(Vector3TermMatrixMulTransposedBasis(Vector3TermMatrixMulBasis(Axis,Transforms[1]),Transforms[0]))));
  309.      s1:=Vector3Dot(Axis,Vector3Sub(Vector3TermMatrixMulInverted(Vector3TermMatrixMul(WitnessPoints[0],Transforms[0]),Transforms[1]),LocalVertex));
  310.     end;
  311.     else begin                                                        
  312.      s1:=0.0;
  313.      Assert(false);
  314.     end;
  315.    end;
  316.  
  317.    // Is the final configuration separated?
  318.    if s1>(Target+Tolerance) then begin
  319.     exit;
  320.    end;
  321.  
  322.    // Has the separation reached tolerance?
  323.    if s1>(Target-Tolerance) then begin
  324.     // Advance the sweeps
  325.     t0:=t1;
  326.     if t0>=(1.0-EPSILON) then begin
  327.      exit;
  328.     end else begin
  329.      break;
  330.     end;
  331.    end;
  332.  
  333.    // Compute the initial separation of the witness points
  334.    Transforms[0]:=Matrix4x4TermMul(Shapes[0].LocalTransform,SweepTransform(SweepA,t0));
  335.    Transforms[1]:=Matrix4x4TermMul(Shapes[1].LocalTransform,SweepTransform(SweepB,t0));
  336.    case SeparationFunctionMode of
  337.     sfmVERTICES:begin
  338.      s0:=Vector3Dot(Axis,Vector3Sub(Vector3TermMatrixMul(WitnessPoints[1],Transforms[1]),Vector3TermMatrixMul(WitnessPoints[0],Transforms[0])));
  339.     end;
  340.     sfmEDGEA,sfmFACEA,sfmEDGES:begin
  341.      s0:=Vector3Dot(Axis,Vector3Sub(Vector3TermMatrixMulInverted(Vector3TermMatrixMul(WitnessPoints[1],Transforms[1]),Transforms[0]),LocalVertex));
  342.     end;
  343.     sfmEDGEB,sfmFACEB:begin
  344.      s0:=Vector3Dot(Axis,Vector3Sub(Vector3TermMatrixMulInverted(Vector3TermMatrixMul(WitnessPoints[0],Transforms[0]),Transforms[1]),LocalVertex));
  345.     end;
  346.     else begin
  347.      s0:=0.0;
  348.      Assert(false);
  349.     end;
  350.    end;
  351.  
  352.    // Check for initial overlap
  353.    if s0<(Target-Tolerance) then begin
  354.     // This might happen if the root finder runs out of iterations. Also more likely to happen with a poor separation function.
  355.     Beta:=t0;
  356.     result:=false;
  357.     exit;
  358.    end;
  359.  
  360.    // Check for touching, t0 should hold the TOI (could be 0.0)
  361.    if s0<=(Target+Tolerance) then begin
  362.     Beta:=t0;
  363.     result:=true;
  364.     exit;
  365.    end;
  366.  
  367.    // Compute the 1D root of: f(x)-Target=0
  368.    a0:=t0;
  369.    a1:=t1;
  370.    for RootIteration:=0 to 63 do begin
  371.  
  372.     if (RootIteration and 1)<>0 then begin
  373.      t:=a1+((Target-s0)*((a1-a0)/(s1-s0)));
  374.     end else begin
  375.      t:=(a0+a1)*0.5;
  376.     end;
  377.  
  378.     Transforms[0]:=Matrix4x4TermMul(Shapes[0].LocalTransform,SweepTransform(SweepA,t));
  379.     Transforms[1]:=Matrix4x4TermMul(Shapes[1].LocalTransform,SweepTransform(SweepB,t));
  380.     case SeparationFunctionMode of
  381.      sfmVERTICES:begin
  382.       s:=Vector3Dot(Axis,Vector3Sub(Vector3TermMatrixMul(WitnessPoints[1],Transforms[1]),Vector3TermMatrixMul(WitnessPoints[0],Transforms[0])));
  383.      end;
  384.      sfmEDGEA,sfmFACEA,sfmEDGES:begin
  385.       s:=Vector3Dot(Axis,Vector3Sub(Vector3TermMatrixMulInverted(Vector3TermMatrixMul(WitnessPoints[1],Transforms[1]),Transforms[0]),LocalVertex));
  386.      end;
  387.      sfmEDGEB,sfmFACEB:begin
  388.       s:=Vector3Dot(Axis,Vector3Sub(Vector3TermMatrixMulInverted(Vector3TermMatrixMul(WitnessPoints[0],Transforms[0]),Transforms[1]),LocalVertex));
  389.      end;
  390.      else begin
  391.       s:=0.0;
  392.       Assert(false);
  393.      end;
  394.     end;
  395.  
  396.     if abs(s-Target)<Tolerance then begin
  397.      // t1 holds a tentative value for t0
  398.      t1:=t;
  399.      break;
  400.     end else if s>Target then begin
  401.      a0:=t;
  402.      s0:=s;
  403.     end else begin
  404.      a1:=t;
  405.      s1:=s;
  406.     end;
  407.  
  408.    end;
  409.  
  410.   end;
  411.  
  412.  end;
  413.  
  414. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement