public BBAA Intersect(BBAA o) { // validate intersection //if (!Overlaps(o)) return new BBAA(v2f(0, 0), v2f(0, 0)); bool lapX = true, lapY = true; float ox = 0, dx = 0; if (o.UL.X >= UL.X && o.LR.X <= LR.X) { // we're outside ox = o.UL.X; dx = o.LR.X - o.UL.X; } else if (UL.X >= o.UL.X && LR.X <= o.LR.X) { // we're inside ox = UL.X; dx = LR.X - UL.X; } else if (LR.X > o.UL.X && LR.X < o.LR.X) { // we're overlapping from the left ox = o.UL.X; dx = LR.X - o.UL.X; } else if (UL.X < o.LR.X && UL.X > o.UL.X) { // we're overlapping from the right ox = UL.X; dx = o.LR.X - UL.X; } else { lapX = false; } float oy = 0, dy = 0; if (o.UL.Y >= UL.Y && o.LR.Y <= LR.Y) { // we're outside oy = o.UL.Y; dy = o.LR.Y - o.UL.Y; } else if (UL.Y >= o.UL.Y && LR.Y <= o.LR.Y) { // we're inside oy = UL.Y; dy = LR.Y - UL.Y; } else if (LR.Y > o.UL.Y && LR.Y < o.LR.Y) { // we're overlapping from above oy = o.UL.Y; dy = LR.Y - o.UL.Y; } else if (UL.Y < o.LR.Y && UL.Y > o.UL.Y) { // we're overlapping from below oy = UL.Y; dy = o.LR.Y - UL.Y; } else { lapY = false; } if (lapX && lapY) return new BBAA(v2f(ox, oy), v2f(ox + dx, oy + dy)).Check(); else return new BBAA(v2f(0, 0), v2f(0, 0)); }
public Player(Assets assets) { Box = new BBAA(v2f(0, 0), v2f(0.8F, 0.8F)); anim = assets.Animation("cat"); anim.Scale = Box.Size; }
public BBAA ResolveCollision(BBAA o, out CollidedWith flags) { flags = 0; o.Check(); // mutation of a BBAA without explicit copy is acceptable, it's a struct. // iterate tiles that overlap with "o" // TODO: Check less tiles. s/2/1/ or s/2/0/ might do. int ULy = (int)(o.UL.Y - 2).Clamp(0, Height); int ULx = (int)(o.UL.X - 2).Clamp(0, Width); int LRy = (int)(o.LR.Y + 2).Clamp(0, Height); int LRx = (int)(o.LR.X + 2).Clamp(0, Width); for (int y = ULy; y < LRy; y++) { for (int x = ULx; x < LRx; x++) { var tile = this[x, y]; if (tile.Type != null && tile.Type.Collider) { // TODO: less checks var intersection = o.Check().Intersect(new BBAA(v2f(x, y), v2f(x + 1, y + 1))).Check(); Vector2f iSize = intersection.Size, oSize = o.Size; Vector2f iCenter = intersection.Center, oCenter = o.Center; if (iSize.Len() > 0) { // only respond to force on the shallow axis if (iSize.X < iSize.Y) { if (iCenter.X < oCenter.X) { flags |= CollidedWith.Left; o.Position = v2f(intersection.LR.X, o.UL.Y); } else { flags |= CollidedWith.Right; o.Position = v2f(intersection.UL.X - o.Size.X, o.UL.Y); } } else { if (iCenter.Y < oCenter.Y) { flags |= CollidedWith.Top; o.Position = v2f(o.UL.X, intersection.LR.Y); } else { flags |= CollidedWith.Bottom; o.Position = v2f(o.UL.X, intersection.UL.Y - o.Size.Y); } } } } } } return o; }
public bool Overlaps(BBAA o) { return Physics.BBAAOverlap(UL.X, UL.Y, LR.X - UL.X, LR.Y - UL.Y, o.UL.X, o.UL.Y, o.LR.X - o.UL.X, o.LR.Y - o.LR.X); }
public static void TestIntersect() { var a = new BBAA(v2f(0, 0), v2f(1, 1)); var b = new BBAA(v2f(0.25F, 0.25F), v2f(0.75F, 0.75F)); AssumeEq(a.Intersect(b), b.Intersect(a)); var c = new BBAA(v2f(0.5F, 0.5F), v2f(1.5F, 1.5F)); AssumeEq(a.Intersect(c), c.Intersect(a)); L.I(a.Intersect(c).Size.ToString()); }