//--------------------------------- // OBB vs Sphere //--------------------------------- private bool AreSphereOBBColliding(ColliderBox b, SphereCollider s, ColliderTypes collTypes = ColliderTypes.BothRigidBodies) { Tuple <Vector3, bool> infoClosestPoint = Util.FindClosestPoint(b, s); this.currentClosestPointOnObb = infoClosestPoint.Item1; bool sphereInsideObb = infoClosestPoint.Item2; float distSphereObbPoint = (this.currentClosestPointOnObb - s._center).sqrMagnitude; bool collision = distSphereObbPoint < s.SqrRadius; if (collision || sphereInsideObb) { if (collTypes == ColliderTypes.BothRigidBodies) { this.CollisionResolutionObbSphere(b, s, sphereInsideObb); } else { this.CollisionResolutionObbWithSingleParticle(b, s, sphereInsideObb, collTypes); } } return(collision || sphereInsideObb); }
/// <summary> /// Reads the collider type and creates a new Collider instance based on the type. /// </summary> /// <param name="collType"></param> /// <returns>A new collider instance based on the type.</returns> public Collider FromColliderTypeToColliderInstance(ColliderTypes collType) { switch (collType) { case ColliderTypes.Box: return(new BoxCollider()); case ColliderTypes.Capsule: return(new CapsuleCollider()); case ColliderTypes.Mesh: return(new MeshCollider()); case ColliderTypes.Sphere: return(new SphereCollider()); default: return(null); } }
//--------------------------------- // Sphere vs Sphere //--------------------------------- private bool AreSpheresColliding(SphereCollider sp1, SphereCollider sp2, ColliderTypes collTypes = ColliderTypes.BothRigidBodies) { // Compute distance vector Vector3 sp1_sp2 = sp1._center - sp2._center; float longSpan = sp1_sp2.magnitude; float overlapSpan = sp1.Radius + sp2.Radius; bool colliding = longSpan < overlapSpan; // Resolve Collision if (colliding) { if (collTypes == ColliderTypes.BothRigidBodies) { this.CollisionResolutionSphere(sp1, sp2, overlapSpan - longSpan); } else { this.CollisionResolutionSphereWithSingleParticle(sp1, sp2, overlapSpan - longSpan, collTypes); } } return(colliding); }
public void Start() { currentColliderType = ColliderTypes.Standing; }
private void CollisionResolutionObbWithSingleParticle(ColliderBox cb, SphereCollider s, bool sphereInsideObb, ColliderTypes collTypes) { Vector3 temp = this.currentClosestPointOnObb - s._center; float dirMult = sphereInsideObb ? -1 : 1; Vector3 dir = temp.normalized * dirMult; Vector3 currPoint = s._center + dir * s.Radius; Vector3 projPoint = this.currentClosestPointOnObb; this.SeparateParticleObjectWithSingleParticles(cb, s, projPoint, currPoint, collTypes); }
private void CollisionResolutionSphereWithSingleParticle(SphereCollider sp1, SphereCollider sp2, float distance, ColliderTypes collTypes) { Vector3 dir = sp2._center - sp1._center; dir.Normalize(); Vector3 currPoint = sp1._center + dir * sp1.Radius; Vector3 displace = dir * distance; Vector3 projPoint = currPoint - displace; this.SeparateParticleObjectWithSingleParticles(sp1, sp2, currPoint, projPoint, collTypes); }
//------------------------------------- // Collision Methods - Single Particle Colliders //------------------------------------- /// <summary> /// Separate 2 objects where at least one is a SingleParticle object (SphereCollider). /// </summary> private void SeparateParticleObjectWithSingleParticles(BaseCollider bc, SphereCollider sp, Vector3 currPoint, Vector3 projPoint, ColliderTypes collTypes) { if (collTypes == ColliderTypes.FirstRigidBody_SecondSingleParticle) { //ParticleObject po = bc.GetParticleObject(); //float[] c1 = this.ComputeParticlesCoefficients(po, currPoint); //float lambda1 = this.ComputeLambda(c1); Logger.Instance.DebugInfo("Updating FirstRigidBody_SecondSingleParticle object", "COLLISION"); // Update the dynamic object //for (int i = 0; i < 4; i++) //{ // po.particles[i].position = po.particles[i].position + (lambda1 * c1[i] * (projPoint - currPoint) * 0.5f) * po.particles[i].invMass; //} // Update also the single particle Vector3 newPos = sp.GetSingleParticlePosition() - ((projPoint - currPoint)) * 0.5f * sp.GetSingleParticleInvMass(); sp.ChangeSingleParticlePosition(newPos); } // if both are single particle objects else if (collTypes == ColliderTypes.BothSingleParticles) { // TODO: test SphereCollider sp1 = (SphereCollider)bc; Vector3 newPosSp1 = sp1.GetSingleParticlePosition() + ((projPoint - currPoint)) * sp1.GetSingleParticleInvMass(); sp1.ChangeSingleParticlePosition(newPosSp1); Vector3 newPosSp2 = sp.GetSingleParticlePosition() - ((projPoint - currPoint)) * sp.GetSingleParticleInvMass(); sp.ChangeSingleParticlePosition(newPosSp2); } }
bool RayCastFast(Vector3 origin, Vector3 direction, out VoxelHitInfo hitInfo, float maxDistance = 0, bool createChunksIfNeeded = false, byte minOpaque = 0, ColliderTypes colliderTypes = ColliderTypes.AnyCollider) { bool voxelHit = RayCastFastVoxel(origin, direction, out hitInfo, maxDistance, createChunksIfNeeded, minOpaque); if ((colliderTypes & ColliderTypes.OnlyVoxels) != 0) { return(voxelHit); } if (voxelHit) { maxDistance = hitInfo.distance - 0.1f; } // Cast a normal raycast to detect normal gameobjects within ray RaycastHit hit; if (Physics.Raycast(origin + 0.3f * direction, direction, out hit, maxDistance - 0.3f)) { hitInfo.distance = hit.distance; hitInfo.point = hit.point; hitInfo.normal = hit.normal; hitInfo.collider = hit.collider; // Check if gameobject is a dynamic voxel if (hit.collider != null) { if ((colliderTypes & ColliderTypes.IgnorePlayer) != 0 && characterController != null && characterController.name.Equals(hit.collider.name)) { return(false); } hitInfo.voxelIndex = -1; VoxelPlaceholder placeholder = hit.collider.GetComponentInParent <VoxelPlaceholder> (); if (placeholder != null) { hitInfo.chunk = placeholder.chunk; hitInfo.voxelIndex = placeholder.voxelIndex; hitInfo.voxel = placeholder.chunk.voxels [placeholder.voxelIndex]; hitInfo.voxelCenter = placeholder.transform.position; hitInfo.placeholder = placeholder; } } return(true); } else { return(voxelHit); } }
public Collider(ColliderTypes type) { _type = type; }