public static bool Distance(ConvexShape shape0,ref Matrix wtrs0,ConvexShape shape1,ref Matrix wtrs1,ref Vector3 guess,GjkEpaSolver2Results results) { GjkEpaSolver2MinkowskiDiff shape = new GjkEpaSolver2MinkowskiDiff(); Initialize(shape0,ref wtrs0,shape1,ref wtrs1,results,shape,false); GJK gjk = new GJK(); GJKStatus gjk_status= gjk.Evaluate(shape,ref guess); if(gjk_status == GJKStatus.Valid) { Vector3 w0 = Vector3.Zero; Vector3 w1 = Vector3.Zero; for(uint i=0;i<gjk.m_simplex.rank;++i) { float p=gjk.m_simplex.p[i]; w0+=shape.Support(ref gjk.m_simplex.c[i].d,0)*p; Vector3 temp = -gjk.m_simplex.c[i].d; w1+=shape.Support(ref temp,1)*p; } results.witnesses0 = Vector3.Transform(w0,wtrs0); results.witnesses1 = Vector3.Transform(w1,wtrs0); results.normal = w0-w1; results.distance = results.normal.Length(); results.normal /= results.distance>GJK_MIN_DISTANCE?results.distance:1; return(true); } else { //GjkEpaSolver2Status results.status = (gjk_status==GJKStatus.Inside)?GjkEpaSolver2Status.Penetrating :GjkEpaSolver2Status.GJK_Failed ; return(false); } }
public static void Initialize(ConvexShape shape0,ref Matrix wtrs0, ConvexShape shape1,ref Matrix wtrs1, GjkEpaSolver2Results results, GjkEpaSolver2MinkowskiDiff shapeR, bool withmargins) { /* Results */ results.witnesses0 = Vector3.Zero; results.witnesses1 = Vector3.Zero; results.status = GjkEpaSolver2Status.Separated; /* Shape */ shapeR.m_shapes[0] = shape0; shapeR.m_shapes[1] = shape1; shapeR.m_toshape1 = MathUtil.TransposeTimesBasis(ref wtrs1,ref wtrs0); shapeR.m_toshape0 = MathUtil.InverseTimes(ref wtrs0, ref wtrs1); if (BulletGlobals.g_streamWriter != null && debugGJK) { MathUtil.PrintMatrix(BulletGlobals.g_streamWriter, "gjksolver2::init::shape0", shapeR.m_toshape0); MathUtil.PrintMatrix(BulletGlobals.g_streamWriter, "gjksolver2::init::shape1", shapeR.m_toshape1); } shapeR.EnableMargin(withmargins); }
public virtual bool CalcPenDepth(ISimplexSolverInterface simplexSolver, ConvexShape convexA, ConvexShape convexB, ref Matrix transA, ref Matrix transB, ref Vector3 v, ref Vector3 wWitnessOnA, ref Vector3 wWitnessOnB, IDebugDraw debugDraw) { //float radialmargin = 0f; Vector3 guessVector = (transA.Translation - transB.Translation); GjkEpaSolver2Results results = new GjkEpaSolver2Results(); if (GjkEpaSolver2.Penetration(convexA, ref transA, convexB, ref transB, ref guessVector, results)) { // debugDraw->drawLine(results.witnesses[1],results.witnesses[1]+results.normal,btVector3(255,0,0)); //resultOut->addContactPoint(results.normal,results.witnesses[1],-results.depth); wWitnessOnA = results.witnesses0; wWitnessOnB = results.witnesses1; v = results.normal; return true; } else { if (GjkEpaSolver2.Distance(convexA, ref transA, convexB, ref transB, ref guessVector, results)) { wWitnessOnA = results.witnesses0; wWitnessOnB = results.witnesses1; v = results.normal; return false; } } return false; }
public static bool Penetration(ConvexShape shape0,ref Matrix wtrs0,ConvexShape shape1,ref Matrix wtrs1,ref Vector3 guess,GjkEpaSolver2Results results,bool usemargins) { GjkEpaSolver2MinkowskiDiff shape = new GjkEpaSolver2MinkowskiDiff(); Initialize(shape0,ref wtrs0,shape1,ref wtrs1,results, shape,usemargins); GJK gjk = new GJK(); Vector3 minusGuess = -guess; GJKStatus gjk_status=gjk.Evaluate(shape,ref minusGuess); switch(gjk_status) { case GJKStatus.Inside: { EPA epa = new EPA(); eStatus epa_status=epa.Evaluate(gjk,ref minusGuess); if(epa_status!=eStatus.Failed) { Vector3 w0 = Vector3.Zero; for(uint i=0;i<epa.m_result.rank;++i) { // order of results here is 'different' , EPA.evaluate. w0+=shape.Support(ref epa.m_result.c[i].d,0)*epa.m_result.p[i]; } results.status = GjkEpaSolver2Status.Penetrating; results.witnesses0 = Vector3.Transform(w0,wtrs0); results.witnesses1 = Vector3.Transform((w0-epa.m_normal*epa.m_depth),wtrs0); results.normal = -epa.m_normal; results.distance = -epa.m_depth; return(true); } else results.status=GjkEpaSolver2Status.EPA_Failed; } break; case GJKStatus.Failed: results.status=GjkEpaSolver2Status.GJK_Failed; break; } return(false); }
public static bool Penetration(ConvexShape shape0, ref Matrix wtrs0, ConvexShape shape1, ref Matrix wtrs1, ref Vector3 guess, GjkEpaSolver2Results results) { return Penetration(shape0, ref wtrs0, shape1, ref wtrs1, ref guess, results, true); }
// public bool SignedDistance(ConvexShape shape0,ref Matrix wtrs0,ConvexShape shape1,ref Matrix wtrs1,ref Vector3 guess,GjkEpaSolver2Results results) { if(!Distance(shape0,ref wtrs0,shape1,ref wtrs1,ref guess,results)) return(Penetration(shape0,ref wtrs0,shape1,ref wtrs1,ref guess,results,false)); else return(true); }
// public float SignedDistance(ref Vector3 position, float margin, ConvexShape shape0, ref Matrix wtrs0, GjkEpaSolver2Results results) { GjkEpaSolver2MinkowskiDiff shape = new GjkEpaSolver2MinkowskiDiff(); SphereShape shape1 = new SphereShape(margin); Matrix wtrs1 = Matrix.CreateFromQuaternion(Quaternion.Identity); wtrs0.Translation = position; Initialize(shape0,ref wtrs0,shape1,ref wtrs1,results,shape,false); GJK gjk = new GJK(); Vector3 guess = new Vector3(1,1,1); GJKStatus gjk_status=gjk.Evaluate(shape,ref guess); if(gjk_status==GJKStatus.Valid) { Vector3 w0=Vector3.Zero; Vector3 w1=Vector3.Zero; for(int i=0;i<gjk.m_simplex.rank;++i) { float p=gjk.m_simplex.p[i]; w0+=shape.Support( ref gjk.m_simplex.c[i].d,0)*p; Vector3 temp = -gjk.m_simplex.c[i].d; w1+=shape.Support(ref temp,1)*p; } results.witnesses0 = Vector3.Transform(w0,wtrs0); results.witnesses1 = Vector3.Transform(w1,wtrs0); Vector3 delta= results.witnesses1-results.witnesses0; float margin2 = shape0.GetMarginNonVirtual()+shape1.GetMarginNonVirtual(); float length = delta.Length(); results.normal = delta/length; results.witnesses0 += results.normal*margin2; return(length-margin2); } else { if(gjk_status==GJKStatus.Inside) { if(Penetration(shape0,ref wtrs0,shape1,ref wtrs1,ref gjk.m_ray,results)) { Vector3 delta= results.witnesses0-results.witnesses1; float length= delta.Length(); if (length >= MathUtil.SIMD_EPSILON) results.normal = delta/length; return(-length); } } } return(MathUtil.SIMD_INFINITY); }