public static bool Collide(BoundingCapsule Capsule2, AABB Box1) { Box1.Half = (Box1.MaxExtent - Box1.MinExtent) * 0.5f;// + Box1.MinExtent; //Get Direction Between the two boxes + edges Box1.Centre = Box1.Half + Box1.MinExtent; AABB Box2 = new AABB(Capsule2.A, Capsule2.B); Box2.Half = (Box2.MaxExtent - Box2.MinExtent) * 0.5f; Box2.Centre = Box2.Half + Box2.MinExtent; if (Mathf.Abs(Box1.Centre.x - Box2.Centre.x) > (Box1.Half.x + Box2.Half.x)) { return(false); } if (Mathf.Abs(Box1.Centre.y - Box2.Centre.y) > (Box1.Half.y + Box2.Half.y)) { return(false); } if (Mathf.Abs(Box1.Centre.z - Box2.Centre.z) > (Box1.Half.z + Box2.Half.z)) { return(false); } return(true); }
public static void Resolve(BoundingCapsule Capsule2, AABB Box1, out MyVector3 Normal, out float Penetration) { Normal = new MyVector3(0, 0, 0); Penetration = float.MaxValue; Box1.Half = (Box1.MaxExtent - Box1.MinExtent) * 0.5f;// + Box1.MinExtent; //Get Direction Between the two boxes + edges Box1.Centre = Box1.Half + Box1.MinExtent; AABB Box2 = new AABB(new MyVector3(Capsule2.A.x, Capsule2.A.y, Capsule2.A.z), new MyVector3(Capsule2.B.x, Capsule2.B.y, Capsule2.B.z)); Box2.Half = (Box2.MaxExtent - Box2.MinExtent) * 0.5f; Box2.Centre = Box2.Half + Box2.MinExtent; float CurPenetration = (Box1.Half.x + Box2.Half.x) - Mathf.Abs(Box1.Centre.x - Box2.Centre.x); if (CurPenetration < Penetration) { Penetration = CurPenetration; if (Box1.Centre.x > Box2.Centre.x) { Normal = new MyVector3(-1, 0, 0); } if (Box1.Centre.x < Box2.Centre.x) { Normal = new MyVector3(1, 0, 0); } } CurPenetration = (Box1.Half.y + Box2.Half.y) - Mathf.Abs(Box1.Centre.y - Box2.Centre.y); if (CurPenetration < Penetration) { Penetration = CurPenetration; if (Box1.Centre.y > Box2.Centre.y) { Normal = new MyVector3(0, -1, 0); } if (Box1.Centre.y < Box2.Centre.y) { Normal = new MyVector3(0, 1, 0); } } CurPenetration = (Box1.Half.z + Box2.Half.z) - Mathf.Abs(Box1.Centre.z - Box2.Centre.z); if (CurPenetration < Penetration) { Penetration = CurPenetration; if (Box1.Centre.z > Box2.Centre.z) { Normal = new MyVector3(0, 0, -1); } if (Box1.Centre.z < Box2.Centre.z) { Normal = new MyVector3(0, 0, 1); } } }
public static bool Collide(BoundingCapsule capsule, BoundingCircle otherCircle) { float CombinedRadius = capsule.Radius + otherCircle.Radius; float DistanceFromFloat = Mathf.Sqrt(VectorMaths.SqDistanceFromFloat(capsule.A, capsule.B, otherCircle.CentrePoint)); return(DistanceFromFloat <= CombinedRadius); }
// Update is called once per frame void Update() { myTransformation Transformation = GetComponent <myTransformation>(); float CurRadius = float.MinValue; float MinCurRadius = float.MaxValue; if (Transformation.ModelMaxExtent.x > CurRadius) { CurRadius = Transformation.ModelMaxExtent.x; } if (Transformation.ModelMaxExtent.z > CurRadius) { CurRadius = Transformation.ModelMaxExtent.z; } if (Transformation.ModelMinExtent.x < MinCurRadius) { MinCurRadius = Transformation.ModelMinExtent.x; } if (Transformation.ModelMaxExtent.z < MinCurRadius) { MinCurRadius = Transformation.ModelMinExtent.z; } float BiggestScale = float.MinValue; if (Transformation.Scale.x > BiggestScale) { BiggestScale = Transformation.Scale.x; } if (Transformation.Scale.z > BiggestScale) { BiggestScale = Transformation.Scale.z; } Radius = CurRadius - MinCurRadius; Radius *= BiggestScale; Transformation.BoundObject = new BoundingCapsule(Transformation.Translation + (Transformation.Scale * Transformation.ModelMinExtent), Transformation.Translation + (Transformation.Scale * Transformation.ModelMaxExtent), Radius); BoundingCapsule Capsule2 = Transformation.BoundObject as BoundingCapsule; Min = Capsule2.A; Max = Capsule2.B; }
public override bool Intersects(BoundingObject RHS) { if (RHS is AABB) { AABB Box1 = RHS as AABB; return(Collide(this, Box1)); } if (RHS is BoundingCircle) { BoundingCircle Sphere1 = RHS as BoundingCircle; return(Collide(this, Sphere1)); } if (RHS is BoundingCapsule) { BoundingCapsule Capsule1 = RHS as BoundingCapsule; return(Collide(this, Capsule1)); } return(false); }
public static bool Collide(BoundingCapsule capsule, BoundingCapsule otherCapsule) { float CombinedRadiusSq = (capsule.Radius + otherCapsule.Radius) * (capsule.Radius + otherCapsule.Radius); MyVector3 LengthA = capsule.A - otherCapsule.A; float DistanceA = LengthA.Length(); if (DistanceA <= CombinedRadiusSq) { return(true); } MyVector3 LengthB = capsule.B - otherCapsule.B; float DistanceB = LengthB.Length(); if (DistanceB <= CombinedRadiusSq) { return(true); } return(false); }
public override void CollisionResolution(BoundingObject RHS, out MyVector3 Norm, out float Penetration) { Norm = new MyVector3(0, 0, 0); Penetration = float.MaxValue; if (RHS is AABB) { AABB Box2 = RHS as AABB; Resolve(this, Box2, out Norm, out Penetration); } if (RHS is BoundingCircle) { BoundingCircle Circle2 = RHS as BoundingCircle; Resolve(this, Circle2, out Norm, out Penetration); } if (RHS is BoundingCapsule) { BoundingCapsule Capsule2 = RHS as BoundingCapsule; Resolve(this, Capsule2, out Norm, out Penetration); } }
public static void Resolve(BoundingCapsule Capsule2, BoundingCircle Circle1, out MyVector3 Normal, out float Penetration) { Normal = new MyVector3(0, 0, 0); Penetration = float.MaxValue; AABB Box1 = new AABB(new MyVector3(Capsule2.A.x, Capsule2.A.y, Capsule2.A.z), new MyVector3(Capsule2.B.x, Capsule2.B.y, Capsule2.B.z)); Box1.Half = (Box1.MaxExtent - Box1.MinExtent) * 0.5f; Box1.Centre = Box1.Half + Box1.MinExtent; float Radius = Circle1.Radius; Radius *= 0.5f; MyVector3 Extent = new MyVector3(Radius, Radius, Radius); AABB Box2; Box2 = new AABB(Circle1.CentrePoint - Extent, Circle1.CentrePoint + Extent); Box2.Half = (Box2.MaxExtent - Box2.MinExtent) * 0.5f; Box2.Centre = Box2.Half + Box2.MinExtent; float CurPenetration = (Box1.Half.x + Box2.Half.x) - Mathf.Abs(Box1.Centre.x - Box2.Centre.x); if (CurPenetration < Penetration) { Penetration = CurPenetration; if (Box1.Centre.x > Box2.Centre.x) { Normal = new MyVector3(-1, 0, 0); } if (Box1.Centre.x < Box2.Centre.x) { Normal = new MyVector3(1, 0, 0); } } CurPenetration = (Box1.Half.y + Box2.Half.y) - Mathf.Abs(Box1.Centre.y - Box2.Centre.y); if (CurPenetration < Penetration) { Penetration = CurPenetration; if (Box1.Centre.y > Box2.Centre.y) { Normal = new MyVector3(0, -1, 0); } if (Box1.Centre.y < Box2.Centre.y) { Normal = new MyVector3(0, 1, 0); } } CurPenetration = (Box1.Half.z + Box2.Half.z) - Mathf.Abs(Box1.Centre.z - Box2.Centre.z); if (CurPenetration < Penetration) { Penetration = CurPenetration; if (Box1.Centre.z > Box2.Centre.z) { Normal = new MyVector3(0, 0, -1); } if (Box1.Centre.z < Box2.Centre.z) { Normal = new MyVector3(0, 0, 1); } } Penetration *= 0.001f; }