private void OnParticleCollision(GameObject other)
        {
            if (!Enabled || (!HighPriority && ImpactManagerInstance.HasReachedPhysicsInteractionsLimit()))
            {
                return;
            }

            ImpactManagerInstance.IncrementPhysicsInteractionsLimit();

            bool isParticleSystem   = _particles != null;
            int  numCollisionEvents = 0;

            //If this is a particle system, get the collision events from out particles
            if (isParticleSystem)
            {
                numCollisionEvents = _particles.GetCollisionEvents(other, collisionEvents);
            }
            //This is a non-particle system object, get collision events from the other object
            else
            {
                ParticleSystem p = other.GetComponent <ParticleSystem>();
                numCollisionEvents = p.GetCollisionEvents(this.gameObject, collisionEvents);
            }

            //Process collision for each particle collision event
            for (int i = 0; i < numCollisionEvents; i++)
            {
                processParticleCollision(collisionEvents[i], other, isParticleSystem);
            }
        }
        /// <summary>
        /// Creates a new DecalInteractionResult from the given IInteractionData.
        /// </summary>
        /// <param name="interactionData">The data to use to create the result.</param>
        /// <returns>A new DecalInteractionResult.</returns>
        public override IInteractionResult GetInteractionResult <T>(T interactionData)
        {
            //Immediately break out if intensity is less than the velocity minimum, since any result would be invalid anyways.
            float intensity = ImpactInteractionUtilities.GetCollisionIntensity(interactionData, CollisionNormalInfluence);

            if (intensity < MinimumVelocity)
            {
                return(null);
            }

            long key = 0;

            if (!ImpactInteractionUtilities.GetKeyAndValidate(interactionData, this, out key))
            {
                return(null);
            }

            DecalInteractionResult c;

            if (shouldEmit(interactionData.InteractionType) && ImpactManagerInstance.TryGetInteractionResultFromPool(interactionResultPoolKey, out c))
            {
                c.Key           = key;
                c.DecalTemplate = DecalPrefab;
                c.OriginalData  = InteractionDataUtilities.ToInteractionData(interactionData);

                c.CreationInterval     = CreationInterval;
                c.CreationIntervalType = CreationIntervalType;

                return(c);
            }

            return(null);
        }
 private static void triggerOnHitObject <T>(T interactionData, IImpactObject otherObject, int physicsMaterialId, bool useMaterialComposition) where T : IInteractionData
 {
     if (otherObject != null)
     {
         if (useMaterialComposition)
         {
             int count = otherObject.GetMaterialCompositionNonAlloc(interactionData.Point, ImpactManagerInstance.MaterialCompositionBuffer);
             for (int i = 0; i < count; i++)
             {
                 ImpactMaterialComposition comp = ImpactManagerInstance.MaterialCompositionBuffer[i];
                 if (comp.CompositionValue > 0)
                 {
                     IInteractionData newInteractionData = interactionData.Clone();
                     newInteractionData.CompositionValue = comp.CompositionValue;
                     ImpactManagerInstance.ProcessInteraction(newInteractionData, comp.Material, otherObject);
                 }
             }
         }
         else
         {
             ImpactManagerInstance.ProcessInteraction(interactionData, otherObject);
         }
     }
     else if (ImpactManagerInstance.UseMaterialMapping)
     {
         IImpactMaterial m;
         if (ImpactManagerInstance.TryGetImpactMaterialFromMapping(physicsMaterialId, out m))
         {
             ImpactManagerInstance.ProcessInteraction(interactionData, m, null);
         }
     }
 }
Exemplo n.º 4
0
        protected ImpactTagMask?getOtherObjectTagMask(IImpactObject impactObject, Vector3 point, int otherPhysicsMaterialID, bool hasOtherObject)
        {
            if (hasOtherObject)
            {
                IImpactMaterial m = impactObject.GetPrimaryMaterial(point);

                if (m != null)
                {
                    return(m.MaterialTagsMask);
                }
                else if (ImpactManagerInstance.UseMaterialMapping && ImpactManagerInstance.TryGetImpactMaterialFromMapping(otherPhysicsMaterialID, out m))
                {
                    return(m.MaterialTagsMask);
                }
            }
            else
            {
                IImpactMaterial m;
                if (ImpactManagerInstance.UseMaterialMapping && ImpactManagerInstance.TryGetImpactMaterialFromMapping(otherPhysicsMaterialID, out m))
                {
                    return(m.MaterialTagsMask);
                }
            }

            return(null);
        }
Exemplo n.º 5
0
        public static bool GetKeyAndValidate <T>(T interactionData, ImpactInteractionBase interaction, out long key) where T : IInteractionData
        {
            key = 0;

            if (interactionData.InteractionType != InteractionData.InteractionTypeCollision)
            {
                int tagMaskValue = 0;
                if (interactionData.TagMask.HasValue)
                {
                    tagMaskValue = interactionData.TagMask.Value.Value;
                }

                key = cantorPairing(interaction.GetInstanceID(),
                                    cantorPairing(tagMaskValue,
                                                  cantorPairing(interactionData.InteractionType,
                                                                cantorPairing(interactionData.ThisObject.GetInstanceID(), interactionData.OtherObject.GetInstanceID()))));

                bool containsKey = ImpactManagerInstance.HasActiveContinuousInteractionWithKey(key);

                if (!containsKey && ImpactManagerInstance.HasReachedContinuousInteractionLimit())
                {
                    return(false);
                }
            }

            return(true);
        }
Exemplo n.º 6
0
        private void OnCollisionStay2D(Collision2D collision)
        {
            if (!Enabled || (!HighPriority && ImpactManagerInstance.HasReachedPhysicsInteractionsLimit()))
            {
                return;
            }

            ImpactManagerInstance.IncrementPhysicsInteractionsLimit();

            ImpactCollisionWrapper c = new ImpactCollisionWrapper(collision);

            processCollision(c);
        }
        protected override void buildInteractionData(IImpactObject target, TCollision collision, TContact contactPoint,
                                                     VelocityData myVelocityData, VelocityData otherVelocityData, ImpactTagMask?tagMask, float compositionValue)
        {
            Vector3 relativeContactPointVelocity = myVelocityData.TotalPointVelocity - otherVelocityData.TotalPointVelocity;

            if (SlideMode != SlideMode.None)
            {
                InteractionData slideParameters = new InteractionData()
                {
                    TagMask          = tagMask,
                    Point            = contactPoint.Point,
                    Normal           = contactPoint.Normal,
                    Velocity         = relativeContactPointVelocity,
                    InteractionType  = InteractionData.InteractionTypeSlide,
                    ThisObject       = contactPoint.ThisObject,
                    OtherObject      = contactPoint.OtherObject,
                    CompositionValue = compositionValue
                };

                invokeTriggeredEvent(slideParameters, target);

                ImpactManagerInstance.ProcessContinuousInteraction(slideParameters, target);
            }

            if (RollMode != RollMode.None)
            {
                float roll = 1 - Mathf.Clamp01(relativeContactPointVelocity.magnitude * 0.1f);

                if (roll > 0)
                {
                    Vector3 rollVelocity = myVelocityData.TangentialVelocity * roll;

                    InteractionData rollParameters = new InteractionData()
                    {
                        TagMask          = tagMask,
                        Point            = contactPoint.Point,
                        Normal           = contactPoint.Normal,
                        Velocity         = rollVelocity,
                        InteractionType  = InteractionData.InteractionTypeRoll,
                        ThisObject       = contactPoint.ThisObject,
                        OtherObject      = contactPoint.OtherObject,
                        CompositionValue = compositionValue
                    };

                    invokeTriggeredEvent(rollParameters, target);

                    ImpactManagerInstance.ProcessContinuousInteraction(rollParameters, target);
                }
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Creates a new AudioInteractionResult from the given IInteractionData.
        /// </summary>
        /// <param name="interactionData">The data to use to create the result.</param>
        /// <returns>A new AudioInteractionResult.</returns>
        public override IInteractionResult GetInteractionResult <T>(T interactionData)
        {
            //Immediately break out if intensity is less than the velocity range minimum, since any result would be invalid anyways.
            float intensity = ImpactInteractionUtilities.GetCollisionIntensity(interactionData, CollisionNormalInfluence);

            if (intensity < VelocityRange.Min)
            {
                return(null);
            }

            long key = 0;

            if (!ImpactInteractionUtilities.GetKeyAndValidate(interactionData, this, out key))
            {
                return(null);
            }

            AudioInteractionResult c;

            if (ImpactManagerInstance.TryGetInteractionResultFromPool(interactionResultPoolKey, out c))
            {
                c.OriginalData        = InteractionDataUtilities.ToInteractionData(interactionData);
                c.LoopAudio           = interactionData.InteractionType != InteractionData.InteractionTypeCollision;
                c.Interaction         = this;
                c.AudioSourceTemplate = AudioSourceTemplate;

                if (interactionData.InteractionType == InteractionData.InteractionTypeSimple)
                {
                    c.AudioClip = getAudioClip(interactionData.InteractionType, 0);
                    c.Volume    = RandomVolumeRange.RandomInRange();
                }
                else
                {
                    float normalizedIntensity = VelocityRange.Normalize(intensity);

                    c.AudioClip = getAudioClip(interactionData.InteractionType, normalizedIntensity);
                    c.Volume    = getVolume(normalizedIntensity) * interactionData.CompositionValue;
                    c.Key       = key;
                }

                c.Pitch = RandomPitchRange.RandomInRange();

                return(c);
            }

            return(null);
        }
Exemplo n.º 9
0
        private void OnCollisionEnter2D()
        {
            if (!Enabled || (!HighPriority && ImpactManagerInstance.HasReachedPhysicsInteractionsLimit()))
            {
                return;
            }

            ImpactManagerInstance.IncrementPhysicsInteractionsLimit();

            InteractionData c = new InteractionData()
            {
                InteractionType = InteractionData.InteractionTypeSimple,
                Point           = transform.position
            };

            ImpactManagerInstance.ProcessInteraction(c, MainTarget);
        }
Exemplo n.º 10
0
        private static void triggerOnRaycastingObject <T>(T interactionData, IImpactObject impactObject, IImpactObject otherObject, int physicsMaterialId, bool useMaterialComposition) where T : IInteractionData
        {
            if (otherObject != null)
            {
                if (useMaterialComposition)
                {
                    int count = otherObject.GetMaterialCompositionNonAlloc(interactionData.Point, ImpactManagerInstance.MaterialCompositionBuffer);
                    for (int i = 0; i < count; i++)
                    {
                        ImpactMaterialComposition comp = ImpactManagerInstance.MaterialCompositionBuffer[i];
                        if (comp.CompositionValue > 0)
                        {
                            IInteractionData newInteractionData = interactionData.Clone();
                            newInteractionData.CompositionValue = comp.CompositionValue;
                            newInteractionData.TagMask          = comp.Material.MaterialTagsMask;
                            ImpactManagerInstance.ProcessInteraction(newInteractionData, impactObject);
                        }
                    }
                }
                else
                {
                    IImpactMaterial material = otherObject.GetPrimaryMaterial(interactionData.Point);
                    if (material != null || (ImpactManagerInstance.UseMaterialMapping && ImpactManagerInstance.TryGetImpactMaterialFromMapping(physicsMaterialId, out material)))
                    {
                        interactionData.TagMask = material.MaterialTagsMask;
                    }

                    ImpactManagerInstance.ProcessInteraction(interactionData, impactObject);
                }
            }
            else if (ImpactManagerInstance.UseMaterialMapping)
            {
                IImpactMaterial material;
                if (ImpactManagerInstance.TryGetImpactMaterialFromMapping(physicsMaterialId, out material))
                {
                    interactionData.TagMask = material.MaterialTagsMask;
                }

                ImpactManagerInstance.ProcessInteraction(interactionData, impactObject);
            }
        }
        protected override void buildInteractionData(IImpactObject target, TCollision collision, TContact contactPoint,
                                                     VelocityData myVelocityData, VelocityData otherVelocityData, ImpactTagMask?tagMask, float CompositionValue)
        {
            Vector3 relativeContactPointVelocity = myVelocityData.TotalPointVelocity - otherVelocityData.TotalPointVelocity;

            InteractionData interactionData = new InteractionData()
            {
                TagMask          = tagMask,
                Point            = contactPoint.Point,
                Normal           = contactPoint.Normal,
                Velocity         = relativeContactPointVelocity,
                InteractionType  = InteractionData.InteractionTypeCollision,
                ThisObject       = contactPoint.ThisObject,
                OtherObject      = contactPoint.OtherObject,
                CompositionValue = CompositionValue
            };

            invokeTriggeredEvent(interactionData, target);

            ImpactManagerInstance.ProcessInteraction(interactionData, target);
        }
        protected override void buildInteractionData(IImpactObject target, ImpactCollisionWrapper collision, ImpactContactPoint contactPoint,
                                                     VelocityData myVelocityData, VelocityData otherVelocityData, ImpactTagMask?tagMask, float CompositionValue)
        {
            VelocityData currentVelocityData = rigidbodyWrapper.GetCurrentVelocityData(contactPoint.Point);

            Vector3 velocityChange = myVelocityData.TotalPointVelocity - currentVelocityData.TotalPointVelocity;
            Vector3 relativeContactPointVelocity = myVelocityData.TotalPointVelocity - otherVelocityData.TotalPointVelocity;

            InteractionData interactionData = new InteractionData()
            {
                TagMask          = tagMask,
                Point            = contactPoint.Point,
                Normal           = contactPoint.Normal,
                Velocity         = Vector3.Lerp(relativeContactPointVelocity, velocityChange, _velocityChangeInfluence),
                InteractionType  = InteractionData.InteractionTypeCollision,
                ThisObject       = contactPoint.ThisObject,
                OtherObject      = contactPoint.OtherObject,
                CompositionValue = CompositionValue
            };

            invokeTriggeredEvent(interactionData, target);

            ImpactManagerInstance.ProcessInteraction(interactionData, target);
        }
 /// <summary>
 /// Creates an object pool instance for the decal prefab.
 /// </summary>
 public override void Preload()
 {
     ImpactDecalPool.PreloadPoolForDecal(DecalPrefab);
     ImpactManagerInstance.CreateInteractionResultPool <Interactions.Decals.DecalInteractionResult>(interactionResultPoolKey);
 }
Exemplo n.º 14
0
 /// <summary>
 /// Creates an object pool instance for the particle prefab.
 /// </summary>
 public override void Preload()
 {
     ImpactParticlePool.PreloadPoolForParticle(ParticlePrefab);
     ImpactManagerInstance.CreateInteractionResultPool <Interactions.Particles.ParticleInteractionResult>(interactionResultPoolKey);
 }
        private void processParticleCollision(ParticleCollisionEvent particleCollisionEvent, GameObject onParticleCollisionObject, bool isParticleSystem)
        {
            IImpactObject myObject;

            //Particle system always uses the main target
            if (isParticleSystem)
            {
                myObject = MainTarget;
            }
            //Non-particle system gets the object from the particle collision event's collider
            else
            {
                myObject = getImpactObject(particleCollisionEvent.colliderComponent.gameObject);
            }

            if (myObject != null)
            {
                IImpactObject otherObject = null;

                //Get other object based on whether or not this object is a particle system
                if (isParticleSystem)
                {
                    otherObject = particleCollisionEvent.colliderComponent.GetComponentInParent <IImpactObject>();
                }
                else
                {
                    otherObject = onParticleCollisionObject.GetComponentInParent <IImpactObject>();
                }

                bool hasOtherObject = otherObject != null;

                if (UseMaterialComposition && hasOtherObject)
                {
                    int count = otherObject.GetMaterialCompositionNonAlloc(particleCollisionEvent.intersection, ImpactManagerInstance.MaterialCompositionBuffer);

                    //Velocity data is dependent on if this is a particle system or a not
                    VelocityData myVelocityData;
                    VelocityData otherVelocityData;
                    if (isParticleSystem)
                    {
                        myVelocityData    = new VelocityData(particleCollisionEvent.velocity, Vector3.zero);
                        otherVelocityData = otherObject.GetVelocityDataAtPoint(particleCollisionEvent.intersection);
                    }
                    else
                    {
                        myVelocityData    = myObject.GetVelocityDataAtPoint(particleCollisionEvent.intersection);
                        otherVelocityData = new VelocityData(particleCollisionEvent.velocity, Vector3.zero);
                    }

                    Vector3 relativeContactPointVelocity = myVelocityData.TotalPointVelocity - otherVelocityData.TotalPointVelocity;

                    for (int i = 0; i < count; i++)
                    {
                        ImpactMaterialComposition comp = ImpactManagerInstance.MaterialCompositionBuffer[i];

                        if (comp.CompositionValue > 0)
                        {
                            InteractionData interactionData = new InteractionData()
                            {
                                TagMask          = comp.Material.MaterialTagsMask,
                                Point            = particleCollisionEvent.intersection,
                                Normal           = particleCollisionEvent.normal,
                                Velocity         = relativeContactPointVelocity,
                                InteractionType  = InteractionData.InteractionTypeCollision,
                                ThisObject       = this.gameObject,
                                OtherObject      = otherObject.GameObject,
                                CompositionValue = 1f
                            };

                            invokeTriggeredEvent(interactionData, myObject);

                            ImpactManagerInstance.ProcessInteraction(interactionData, myObject);
                        }
                    }
                }
                else
                {
                    //Velocity data is dependent on if this is a particle system or a not
                    VelocityData myVelocityData;
                    VelocityData otherVelocityData;
                    if (isParticleSystem)
                    {
                        myVelocityData    = new VelocityData(particleCollisionEvent.velocity, Vector3.zero);
                        otherVelocityData = hasOtherObject ? otherObject.GetVelocityDataAtPoint(particleCollisionEvent.intersection) : new VelocityData();
                    }
                    else
                    {
                        myVelocityData    = myObject.GetVelocityDataAtPoint(particleCollisionEvent.intersection);
                        otherVelocityData = new VelocityData(particleCollisionEvent.velocity, Vector3.zero);
                    }

                    Vector3 relativeContactPointVelocity = myVelocityData.TotalPointVelocity - otherVelocityData.TotalPointVelocity;

                    //Get physics material ID for material mapping, but only if this object is a particle system.
                    //Particles don't have a collider so no need to try and get the physics material if this is not a particle system
                    int           otherPhysicsMaterialID = isParticleSystem ? getPhysicsMaterialID(particleCollisionEvent.colliderComponent) : 0;
                    ImpactTagMask?tagMask = getOtherObjectTagMask(otherObject, particleCollisionEvent.intersection, otherPhysicsMaterialID, hasOtherObject);

                    InteractionData interactionData = new InteractionData()
                    {
                        TagMask          = tagMask,
                        Point            = particleCollisionEvent.intersection,
                        Normal           = particleCollisionEvent.normal,
                        Velocity         = relativeContactPointVelocity,
                        InteractionType  = InteractionData.InteractionTypeCollision,
                        ThisObject       = this.gameObject,
                        OtherObject      = particleCollisionEvent.colliderComponent.gameObject,
                        CompositionValue = 1f
                    };

                    invokeTriggeredEvent(interactionData, myObject);

                    ImpactManagerInstance.ProcessInteraction(interactionData, myObject);
                }
            }
            else
            {
                Debug.LogError("Unable to find Impact Object on GameObject " + gameObject.name);
            }
        }
Exemplo n.º 16
0
 /// <summary>
 /// Creates an instance of the ImpactAudioPool.
 /// </summary>
 public override void Preload()
 {
     ImpactAudioPool.PreloadPoolForAudioSource(AudioSourceTemplate);
     ImpactManagerInstance.CreateInteractionResultPool <AudioInteractionResult>(interactionResultPoolKey);
 }