public static bool OverlapSphereBox(MySphereCollider sph, MyBoxCollider box) { Vector3 closestPointInAABB = Vector3.Min(Vector3.Max(sph.transform.position, box.Min), box.Max); float distance = Vector3.Distance(closestPointInAABB, sph.transform.position); return(distance < sph.Radius); }
/// <summary> /// Check if the input body has corrected with a sphere /// </summary> /// <param name="body"></param> /// <param name="sphere"></param> /// <returns></returns> private MyCollision CollidedWithSphere(MyRigidBody body, MySphereCollider sphere) { // default to no collision MyCollision collision = null; // combined radius of both spheres float combinedRadius = this.distanceToSurface + ((MySphereCollider)sphere).radius; // the vector between the centres of the spheres Vector3 direction = sphere.transform.position - transform.position; // distance between centres float centreDistance = direction.magnitude; // normalise the direction direction /= centreDistance; // check interection if (centreDistance < combinedRadius) { // create a new collision object, containing the vector of the normal and the distance from the sphere collision = new MyCollision(this, sphere, new Vector3(transform.position.x, transform.position.y - ((MySphereCollider)body).radius, transform.position.z), direction, centreDistance); } return collision; }
public override bool isColliding(MyCollider _other) { if (_other == null) { return(false); } if (_other is MySphereCollider) { MySphereCollider other = (MySphereCollider)_other; float distance = (this.Position - other.Position).sqrMagnitude; float radii = this.m_radius * transform.lossyScale.x + other.m_radius * other.transform.lossyScale.x; return(distance < (radii * radii)); } else if (_other is MyBoxCollider) { MyBoxCollider other = (MyBoxCollider)_other; other.CalculateCubeCorners(); Vector3 potentialAxis = this.Position - other.Position; potentialAxis = potentialAxis.normalized; float min1, max1, min2, max2; min1 = float.PositiveInfinity; max1 = float.NegativeInfinity; float tmp; for (int i = 0; i < other.CubeCorners.Length; i++) { tmp = Vector3.Dot(potentialAxis, other.cubeCorners[i]); if (tmp < min1) { tmp = min1; } if (tmp > max1) { max1 = tmp; } } min2 = float.PositiveInfinity; max2 = float.NegativeInfinity; tmp = Vector3.Dot(potentialAxis, other.Position); min2 = tmp - this.ActualRadius; max2 = tmp + this.ActualRadius; if (min1 > min2 && min1 < max2) { return(true); } if (max1 > min2 && max1 < max2) { return(true); } return(false); } return(base.isColliding(_other)); }
/// <summary> /// Check for Sphere and Sphere /// </summary> /// <param name="_coll1"></param> /// <param name="_coll2"></param> /// <returns></returns> private MySphereCollider CheckForSph_Sph(MySphereCollider _coll1, MySphereCollider _coll2) { if (Collisions.SphereInSphere((Sphere)_coll1.Get(), (Sphere)_coll2.Get())) { return(_coll1); } else { return(null); } }
private bool CheckForIntersectionBetweenSpherers(MySphereCollider s1, MySphereCollider s2) { float distance = (s2.Center - s1.Center).magnitude; float radiusSum = s1.Radius + s2.Radius; if (distance < radiusSum) { return(true); } return(false); }
void Start() { hasWon = false; hasDied = false; isInBlackHole = false; planetSphere = planetGO.GetComponentInChildren <MySphereCollider>().Sphere; collider = GetComponent <MySphereCollider>(); scale = Helper.ConvertUnityVectorToMyVector(transform.localScale); rotation = Helper.ConvertUnityVectorToMyVector(transform.rotation.eulerAngles); center = Helper.ConvertUnityVectorToMyVector(transform.position); }
private void ResolveCollisionForSphereres(MySphereCollider c1, MySphereCollider c2) { MyRigidbody r1 = c1.Rigidbody; MyRigidbody r2 = c2.Rigidbody; if (r1 != null && r2 != null) { Vector3 difference = c2.Center - c1.Center; Vector3 normal = difference.normalized; Vector3 relativeVelocity = r2.Velocity - r1.Velocity; Vector3 normalVelocity = normal * Vector3.Dot(normal, relativeVelocity); r1.Velocity += normalVelocity; //adapt force r2.Velocity -= normalVelocity; c2.Center = c1.Center + normal * (c1.Radius + c2.Radius); //separate by force, push c2 away from c1 } else { throw new System.NotImplementedException("Resolution without 2 Rigidbodies is not implemented"); } }
private void Awake() { sphereCollider = GetComponent <MySphereCollider>(); }
public override bool isColliding(MyCollider _other) { if (_other == null) { return(false); } if (_other is MyBoxCollider) { MyBoxCollider other = (MyBoxCollider)_other; Vector3[] mySegment = new Vector3[] { m_cubeCorners[4] - m_cubeCorners[0], m_cubeCorners[3] - m_cubeCorners[0], m_cubeCorners[1] - m_cubeCorners[0], }; Vector3[] otherSegments = new Vector3[] { other.m_cubeCorners[4] - other.m_cubeCorners[0], other.m_cubeCorners[3] - other.m_cubeCorners[0], other.m_cubeCorners[1] - other.m_cubeCorners[0], }; Vector3[] potentialSeperatingAxis = new Vector3[] { Vector3.Cross(mySegment[0], mySegment[1]), Vector3.Cross(mySegment[0], mySegment[2]), Vector3.Cross(mySegment[1], mySegment[2]), Vector3.Cross(mySegment[0], mySegment[1]), Vector3.Cross(mySegment[0], mySegment[2]), Vector3.Cross(mySegment[1], mySegment[2]), Vector3.Cross(mySegment[0], mySegment[0]), Vector3.Cross(mySegment[0], mySegment[1]), Vector3.Cross(mySegment[0], mySegment[2]), Vector3.Cross(mySegment[1], mySegment[0]), Vector3.Cross(mySegment[1], mySegment[1]), Vector3.Cross(mySegment[1], mySegment[2]), Vector3.Cross(mySegment[1], mySegment[0]), Vector3.Cross(mySegment[1], mySegment[1]), Vector3.Cross(mySegment[1], mySegment[2]), }; Vector3 normalizedAxis; float min1, max1, min2, max2; float tmp; foreach (Vector3 axis in potentialSeperatingAxis) { if (axis.sqrMagnitude == 0) { continue; } normalizedAxis = axis.normalized; min1 = float.PositiveInfinity; max1 = float.NegativeInfinity; for (int i = 0; i < this.m_cubeCorners.Length; i++) { tmp = Vector3.Dot(normalizedAxis, this.m_cubeCorners[i]); if (tmp < min1) { min1 = tmp; } if (tmp > max1) { max1 = tmp; } } min2 = float.PositiveInfinity; max2 = float.NegativeInfinity; for (int i = 0; i < other.m_cubeCorners.Length; i++) { tmp = Vector3.Dot(normalizedAxis, this.m_cubeCorners[i]); if (tmp < min2) { min2 = tmp; } if (tmp > max2) { max2 = tmp; } } if (min1 > min2 && min1 < max2) { continue; } if (max1 > min1 && max1 < max2) { continue; } return(false); } return(true); } else if (_other is MySphereCollider) { this.CalculateCubeCorners(); MySphereCollider other = (MySphereCollider)_other; Vector3 potentialAxis = this.Position - other.Position; potentialAxis = potentialAxis.normalized; float min1, max1, min2, max2; min1 = float.PositiveInfinity; max1 = float.NegativeInfinity; float tmp; for (int i = 0; i < cubeCorners.Length; i++) { tmp = Vector3.Dot(potentialAxis, this.m_cubeCorners[i]); if (tmp < min1) { tmp = min1; } if (tmp > max1) { max1 = tmp; } } min2 = float.PositiveInfinity; max2 = float.NegativeInfinity; tmp = Vector3.Dot(potentialAxis, other.Position); min2 = tmp - other.ActualRadius; max2 = tmp + other.ActualRadius; if (min1 > min2 && min1 < max2) { return(true); } if (max1 > min2 && max1 < max2) { return(true); } return(false); } return(base.isColliding(_other)); }
public static bool OverlapSphereSphere(MySphereCollider sphA, MySphereCollider sphB) { float distance = Vector3.Distance(sphA.transform.position, sphB.transform.position); return(distance < sphA.Radius + sphB.Radius); }
public void AddSphere(MySphereCollider sphere) { spheres.Add(sphere); }
public MyCollision(MySphereCollider colliderA, MySphereCollider colliderB, float distance) { this.colliderA = colliderA; this.colliderB = colliderB; this.distance = distance; }