/// <summary> /// Checks whether the given circle intersects with this circle, calculating the /// collision data in that case. /// </summary> /// <param name="other">the circle to check</param> /// <param name="intersection">the collision data, <code>null</code> if no collision has been detected.</param> /// <returns>true if the colliders intersect, false otherwise.</returns> public override bool Intersects(DSphereCollider other, out Manifold intersection) { intersection = null; Fix32 rDistance = this.radius + other.radius; Fix32 sqrRadiusDistance = rDistance * rDistance; Vector3F centerDistance = other.center - this.center; if (centerDistance.SqrtMagnitude > sqrRadiusDistance) { return(false); } //check if one of them is a trigger if (this.IsTrigger || other.IsTrigger) { intersection = new Manifold(this.Body, other.Body); return(true); } Fix32 distance = centerDistance.Magnitude; //Debug.Log("distance:" + (double)distance); Vector3F normal; Fix32 penetration; if (distance > Fix32.Zero) { penetration = rDistance - distance; normal = centerDistance / distance; } else { penetration = this.radius; normal = new Vector3F((Fix32)1, (Fix32)0, (Fix32)0); } intersection = new Manifold(this.Body, other.Body, normal, penetration); return(true); }
protected virtual void Initialize() { gravityPoint = gameObject.AddComponent <GravityPoint>(); dCollider = gameObject.AddComponent <DSphereCollider>(); }
/// <summary> /// Abstract function for intersections with boxes. /// </summary> /// <param name="other">sphere collider.</param> /// <param name="intersection">collision data.</param> /// <returns></returns> public abstract bool Intersects(DSphereCollider other, out Manifold intersection);
/// <summary> /// Checks whether the given colliders intersect, generating an Intersection instance /// with the collision data in that case. /// Since this function represents a "box vs circle" collision, the symmetric function is called /// instead. See DCircleCollider for more info. /// </summary> /// <param name="other">the second collider</param> /// <param name="intersection">the collision data, <code>null</code> if no collision has been detected.</param> /// <returns></returns> public override bool Intersects(DSphereCollider other, out Manifold intersection) { intersection = null; Vector3F spherePos = other.GetPosition(); Vector3F boxPos = this.GetPosition(); spherePos = spherePos - boxPos; boxPos = Vector3F.Zero; //spherePos = rotatePointAroundPivot(spherePos, -euler); Vector3F h = scale / 2; Vector3F v = Vector3F.Abs(spherePos); Vector3F u = Vector3F.Max(v - h, Vector3F.Zero); Vector3F f = getFlag(spherePos); Fix32 penetration; Vector3F normal = Vector3F.Zero; bool ret = Vector3F.Dot(u, u) <= other.Radius * other.Radius; if (!ret) { return(false); } else { if (this.IsTrigger || other.IsTrigger) { intersection = new Manifold(this.Body, other.Body); return(true); } if (u.x > Fix32.Zero || u.y > Fix32.Zero || u.z > Fix32.Zero) { //real_u = rotatePointAroundPivot(real_u, euler); if (IsDebug) { Debug.Log("u.x:" + (float)u.x); Debug.Log("u.y:" + (float)u.y); Debug.Log("u.z:" + (float)u.z); Debug.Log("f.x:" + (float)f.x); Debug.Log("f.y:" + (float)f.y); Debug.Log("f.z:" + (float)f.z); Debug.Log("spherePos.x:" + (float)spherePos.x); Debug.Log("spherePos.y:" + (float)spherePos.y); Debug.Log("spherePos.z:" + (float)spherePos.z); } normal = u / u.Magnitude; normal = new Vector3F(normal.x * f.x, normal.y * f.y, normal.z * f.z); penetration = other.Radius - u.Magnitude; } else { Vector3F vh = v - h; if (vh.x < vh.y && vh.x < vh.z) { var n = new Vector3F(vh.x, Fix32.Zero, Fix32.Zero); normal = n / n.Magnitude; penetration = other.Radius - n.Magnitude; } else if (vh.y < vh.x && vh.y < vh.z) { var n = new Vector3F(Fix32.Zero, vh.y, Fix32.Zero); normal = n / n.Magnitude; penetration = other.Radius - n.Magnitude; } else { var n = new Vector3F(Fix32.Zero, Fix32.Zero, vh.z); normal = n / n.Magnitude; penetration = other.Radius - n.Magnitude; } } intersection = new Manifold(this.Body, other.Body, normal, penetration); } return(true); }