static public CollisionInfo CircleOBB(CircleCollision3D circle, ObjectBoundingBoxCollision3D rect) { //transform circle into obb space transform.InverseTransformPoint(cirecle.postion); //then do circle AABB Vector3 halfExtend = (rect.posMax - rect.posMin) / 2; Vector3 circleInOBB = rect.GetComponent <Particle3D>().GetWorldToObject().MultiplyPoint(circle.center);//rect.transform.InverseTransformPoint(circle.center); Vector3 circleBox = new Vector3( Mathf.Max(-halfExtend.x, Mathf.Min(circleInOBB.x, halfExtend.x)), Mathf.Max(-halfExtend.y, Mathf.Min(circleInOBB.y, halfExtend.y)), Mathf.Max(-halfExtend.z, Mathf.Min(circleInOBB.z, halfExtend.z))); Vector3 distanceVec = circleInOBB - circleBox; float distanceSQ = Vector3.Dot(distanceVec, distanceVec); //Debug.DrawLine(circleBox, circle.center); if (distanceSQ <= (circle.radius * circle.radius)) { //return true; //Debug.Log("circ obb collision"); float distance = Mathf.Sqrt(distanceSQ); return(new CollisionInfo(circle, rect, rect.transform.TransformVector(-distanceVec).normalized, circle.radius - distance)); } return(null); }
public CollisionInfo(ObjectBoundingBoxCollision3D colA, ObjectBoundingBoxCollision3D colB, Vector3 normal, float penetration) { RigidBodyA = colA.GetComponent <Particle3D>(); RigidBodyB = colB.GetComponent <Particle3D>(); RelativeVelocity = RigidBodyB.velocity - RigidBodyA.velocity; contacts[0].normal = normal; contacts[0].penetration = penetration; contacts[0].restitution = Mathf.Min(RigidBodyA.restitution, RigidBodyB.restitution); }
static public CollisionInfo OBBOBB(ObjectBoundingBoxCollision3D rectA, ObjectBoundingBoxCollision3D rectB) { List <Vector3> allAxis = new List <Vector3>(); allAxis.AddRange(rectA.normAxis); allAxis.AddRange(rectB.normAxis); foreach (var axis in allAxis) { float OBB1Min = float.MaxValue; float OBB1Max = float.MinValue; foreach (var vert in rectA.verticeCheck) { float dotValue = (vert.x * axis.x + vert.y * axis.y); if (dotValue < OBB1Min) { OBB1Min = dotValue; } if (dotValue > OBB1Max) { OBB1Max = dotValue; } } float OBB2Min = float.MaxValue; float OBB2Max = float.MinValue; foreach (var vert in rectB.verticeCheck) { float dotValue = (vert.x * axis.x + vert.y * axis.y); if (dotValue < OBB2Min) { OBB2Min = dotValue; } if (dotValue > OBB2Max) { OBB2Max = dotValue; } } if (!(OBB1Max < OBB2Min && OBB2Max < OBB1Min)) { Vector3 AtoB = rectB.center - rectA.center; float x_overlap = rectA.halfExtends.x + rectB.halfExtends.x - Mathf.Abs(AtoB.x); if (x_overlap > 0.0f) { float y_overlap = rectA.halfExtends.y + rectB.halfExtends.y - Mathf.Abs(AtoB.y); if (y_overlap > 0.0f) { if (x_overlap < y_overlap) { //Debug.Log("OBB OBB"); return(new CollisionInfo(rectA, rectB, AtoB.x < 0.0f ? -Vector3.right : Vector3.right, x_overlap)); } else { //Debug.Log("OBB OBB"); return(new CollisionInfo(rectA, rectB, AtoB.y < 0.0f ? -Vector3.up : Vector3.up, y_overlap)); } } } } } return(null); }
static public CollisionInfo AABBOBB(AxisAllignedBoundingBoxCollision3D colA, ObjectBoundingBoxCollision3D colB) { List <Vector3> allAxis = new List <Vector3>(); allAxis.AddRange(colA.normAxis); allAxis.AddRange(colB.normAxis); foreach (var axis in allAxis) { float AABBMin = float.MaxValue; float AABBMax = float.MinValue; foreach (var vert in colA.verticeCheck) { float dotValue = (vert.x * axis.x + vert.y * axis.y); if (dotValue < AABBMin) { AABBMin = dotValue; } if (dotValue > AABBMax) { AABBMax = dotValue; } } float OBBMin = float.MaxValue; float OBBMax = float.MinValue; foreach (var vert in colB.verticeCheck) { float dotValue = (vert.x * axis.x + vert.y * axis.y); if (dotValue < OBBMin) { OBBMin = dotValue; } if (dotValue > OBBMax) { OBBMax = dotValue; } } if (!(AABBMax < OBBMin && OBBMax < AABBMin)) { Vector3 AtoB = colB.center - colA.center; float x_overlap = colA.halfExtends.x + colB.halfExtends.x - Mathf.Abs(AtoB.x); if (x_overlap > 0.0f) { float y_overlap = colA.halfExtends.y + colB.halfExtends.y - Mathf.Abs(AtoB.y); if (y_overlap > 0.0f) { if (x_overlap < y_overlap) { //Debug.Log("ABB OBB"); return(new CollisionInfo(colA, colB, AtoB.x < 0.0f ? -Vector3.right : Vector3.right, x_overlap)); } else { //Debug.Log("ABB OBB"); return(new CollisionInfo(colA, colB, AtoB.y < 0.0f ? -Vector3.up : Vector3.up, y_overlap)); } } } } } return(null); }