// two objects collided at time t. stop them at that time private static void ProcessOverlap(cGameObject objA, cGameObject objB, Vector2f xMTD) { Vector2f mtd = xMTD; float time = (1.0f / 200.0f); // 0.0166f; // 1.0f / 120.0f; //cAppMath.Vec2Truncate(ref mtd, 0.1f); //mtd.X = Math.Clamp<float>(mtd.X, 3.0f, -3.0f); //mtd.Y = Math.Clamp<float>(mtd.Y, 3.0f, -3.0f); if (objA.Unmovable) { objB.MoveBy(mtd * -time); /* * Vector2f p = objB.Position; * p.X -= mtd.X * time; // *0.5f; * p.Y -= mtd.Y * time; // *0.5f; * objB.Position = p; */ } else if (objB.Unmovable) { objA.MoveBy(mtd * time); //objA.Position.X += mtd.X * 0.0166f;// * 0.5f; //objA.Position.Y += mtd.Y * 0.0166f;// * 0.5f; //return; } else { objA.MoveBy(mtd * time); objB.MoveBy(mtd * -time); /* * objA.Position.X += mtd.X * 0.5f; * objA.Position.Y += mtd.Y * 0.5f; * * objB.Position.X -= mtd.X * 0.5f; * objB.Position.Y -= mtd.Y * 0.5f; */ } Vector2f N = mtd; AppMath.Vec2Normalize(ref N); ProcessCollision(objA, objB, N, 0.0f); }
private static bool FindMTD(Vector2f[] xAxis, float[] taxis, int iNumAxes, ref Vector2f N, ref float t) { // find collision first int mini = -1; t = 0.0f; for (int i = 0; i < iNumAxes; i++) { if (taxis[i] > 0) { if (taxis[i] > t) { mini = i; t = taxis[i]; N = xAxis[i]; AppMath.Vec2Normalize(ref N); //.Normalise(); } } } // found one if (mini != -1) { return(true); } // nope, find overlaps mini = -1; for (int i = 0; i < iNumAxes; i++) { float n = (float)AppMath.Vec2Length(AppMath.Vec2NormalizeReturn(xAxis[i])); //xAxis[i].Normalise(); taxis[i] /= n; if (taxis[i] > t || mini == -1) { mini = i; t = taxis[i]; N = xAxis[i]; } } if (mini == -1) { throw new Exception("Collision exception."); } return(mini != -1); }