public bool Intersects(Box2d box) { // there are eight possible separating axis between two // boxes: // four from the first box, // four from the second box. // foreach such axis, we create a "ray" outgoing from the // corresponding edge, in the normal direction. // projecting the other boxes four corners onto that ray, // and finding out that all projections are positive, // means we have found a separating axis and can return // 'false', no intersection. // helpers: // project points on rays // creating rays from box // // what is a ray in our case? it is simply a start point, // and a direction vector. vector2 times two will be nice for this. // to compute the starting points of rays for one box, we // need to rotate around the anchor point and use the range information // available. ortho simply! // to compute the direction of the rays, is just a rotation of // either v(1,0) or v(0,1) vector bool one = SeparatingAxis(this, box); bool two = SeparatingAxis(box, this); bool foundSepAxis = one || two; return !foundSepAxis; }
static bool SeparatingAxis(Box2d b1, Box2d b2) { // If i can find a projection minima that is positive, // I've found a separating axis. for (int i = 0; i < 4; i++) { Vector2 rayStart, rayDir; b1.GetRay(i, out rayStart, out rayDir); double projectionMin = 1; for (int j = 0; j < 4; j++) { Vector2 corner; b2.GetCorner(j, out corner); double projection = Project(rayStart, rayDir, corner); if (projection <= projectionMin) projectionMin = projection; } if (projectionMin > 0) return true; } return false; }
public TopBox(Box2d box) { this.box = box; }
public SideBox(Box2d box, double planeOffset) { this.box = box; this.planeOffset = planeOffset; }
public BottomBox(Box2d box) { this.box = box; }
void RenderBox(IRenderer renderer, Box2d box) { renderer.PushMatrix(); renderer.Translate(Logic2World(box.X, box.Y)); renderer.Rotate(-Vector3.UnitY, box.Rotation); float amin = (float)box.Amin; float amax = (float)box.Amax; float bmin = (float)box.Bmin; float bmax = (float)box.Bmax; renderer.DrawLine(new Vector3(amin, 0, bmin), new Vector3(amin, 0, bmax)); renderer.DrawLine(new Vector3(amax, 0, bmin), new Vector3(amax, 0, bmax)); renderer.DrawLine(new Vector3(amin, 0, bmin), new Vector3(amax, 0, bmin)); renderer.DrawLine(new Vector3(amin, 0, bmax), new Vector3(amax, 0, bmax)); renderer.PopMatrix(); }