Esempio n. 1
0
 public void RemoveWaterObject(WaterRigidBody waterBody)
 {
     if (waterObjects != null && waterObjects.Count > 0)
     {
         waterObjects.Remove(waterBody);
     }
 }
Esempio n. 2
0
    private void OnCollisionEnter(Collision collision)
    {
        WaterRigidBody waterBody = collision.gameObject.GetComponent <WaterRigidBody>();

        if (waterBody != null)
        {
            if (waterBody.Density <= 1 && !collided.Contains(collision.gameObject))
            {
                Vector3 firstContactPoint = collision.GetContact(0).point;
                Vector3 contactDirection  = firstContactPoint - waterBody.transform.position;
                firstContactPoint.y = 0;

                Vector3 travelDirection = waterBody.transform.TransformDirection(waterBody.Velocity.v);
                travelDirection.y = 0;

                float collisionAngle = Vector3.Angle(contactDirection, travelDirection);

                waterBody.Velocity = NewVelocity(waterBody);
                float damage = CrashImpact(waterBody) * sharpness * Mathf.Cos(collisionAngle / 360 * Mathf.PI) * waterBody.Mass;
                waterBody.Mass += damage;
                Debug.Log("Angle: " + collisionAngle);
                Debug.Log("Damage: " + damage);
                collided.Add(collision.gameObject);
            }
        }
    }
Esempio n. 3
0
    protected override Vector3 NewVelocity(WaterRigidBody w)
    {
        Vector3 vel = base.NewVelocity(w);

        Vector3 crashDirection = (w.GetPosition() - Direction.GetPosition()).normalized * Direction.GetDirection().magnitude;

        vel += crashDirection;

        return(vel);
    }
Esempio n. 4
0
    protected virtual Vector3 NewVelocity(WaterRigidBody w)
    {
        //Vector3 reflectVel = (m.GetPosition() - transform.position).normalized * m.GetDirection().magnitude / 2;
        //float bounce = -bouncePower / 2;
        //Vector3 bounceVel = m.GetDirection() * bounce;
        //return reflectVel + bounceVel;

        Vector3 crashDir = transform.position - w.transform.position;
        Vector3 moveDir  = w.transform.TransformDirection(w.Velocity.v);

        Vector3 result = w.Velocity.v;
        float   angle  = Vector3.Angle(crashDir, moveDir);

        Debug.Log(angle);
        if (angle < 105)
        {
            result = w.Velocity.v * -1 * bouncePower;
            if (result.magnitude < minKnockbackPower)
            {
                result = result.normalized * minKnockbackPower;
            }
        }
        return(result);
    }
Esempio n. 5
0
    public WaterFloatInfo GetObjectFloatInfo(WaterRigidBody t)
    {
        WaterFloatInfo result = new WaterFloatInfo();

        try
        {
            Vector3 targetPosition = t.CurrentPosition;

            Vector3 localPosition = TransformToAnkorPosition(targetPosition);

            Vector3 objectForward = t.transform.forward;

            Vector3 objectRight = t.transform.right;

            float stackedHeight = 0;

            int xLength = (int)(t.maxLength * t.floatAccuracy + 1);

            ///move object in rotate direction
            Vector3 startPosition = localPosition + (objectForward * -1 * (t.maxLength / 2) + objectRight * -1 * (t.maxWidth / 2));

            float[][] heightGraph = new float[(int)(t.maxWidth * t.floatAccuracy + 1)][];

            Vector3 currentPosition = startPosition;

            Vector3 current2DPosition;

            int waveHeightCounter = 0;

            int   totalSize       = heightGraph.Length * XSize;
            float pointPercentage = 1f / totalSize;

            float objectStackedHeight = 0;
            float pointsInWater       = 0;
            float maxPointsInWater    = 0;

            for (int x = 0; x < heightGraph.Length; x++)
            {
                heightGraph[x]  = new float[xLength];
                currentPosition = startPosition + objectRight * x;
                for (int z = 0; z < heightGraph[x].Length; z++)
                {
                    current2DPosition = new Vector2(currentPosition.x, currentPosition.z);
                    Vector2 progress = LocalPosToProgress(current2DPosition);
                    if (progress.x > 0 || progress.x < 1 || progress.y > 0 || progress.y < 1)
                    {
                        float waveHeight;
                        switch (t.floatPrecision)
                        {
                        case (WaterRigidBody.FloatHeightPrecision.Approximately):
                        {
                            int index = GetNearestIndex(progress);
                            if (index >= 0)
                            {
                                waveHeight = vertices[index].y + transform.position.y;
                            }
                            else
                            {
                                waveHeight = 0;
                                //abseits von wasser
                            }
                            break;
                        }

                        case (WaterRigidBody.FloatHeightPrecision.Exact):
                        {
                            waveHeight = GetAccurateLocalHeightOnProgress(progress);
                            break;
                        }

                        default:
                        {
                            waveHeight = 0;
                            Debug.LogWarning("Unkown float precision: " + t.floatPrecision);
                            break;
                        }
                        }
                        if (waveHeight /* + transform.position.y*/ > currentPosition.y || t.floatBehaviour == WaterRigidBody.FloatBehaviour.OnPoint)
                        {
                            stackedHeight       += waveHeight;
                            objectStackedHeight += currentPosition.y;
                            heightGraph[x][z]    = waveHeight;
                            waveHeightCounter++;
                            pointsInWater++;
                        }
                        maxPointsInWater++;
                    }

                    currentPosition += objectForward;
                }
            }
            result.sizePercentageInWater = pointsInWater / maxPointsInWater;
            if (result.sizePercentageInWater > 0)
            {
                stackedHeight /= waveHeightCounter;

                objectStackedHeight /= waveHeightCounter;

                float densityDifference = (t.Mass / t.volume) / WATER_DENSITY;
                result.densityDifferenceFactor = densityDifference;

                float currentHeightPercentageInWater = Mathf.Clamp((stackedHeight - objectStackedHeight) / t.maxHeight, 0, 1);
                if (currentHeightPercentageInWater == 0)
                {
                    result.densityDifferenceFactor = 0;
                }
                else if (currentHeightPercentageInWater == 1)
                {
                    result.densityDifferenceFactor = densityDifference;
                }
                else
                {
                    float currentVolumeInWater = t.volumeDistribution.Integrate(0, currentHeightPercentageInWater, t.integrationPrecision);
                    result.densityDifferenceFactor = (t.Mass / (currentVolumeInWater * t.volume)) / WATER_DENSITY;
                }

                float heightPercentageInWater = t.volumeDistribution.IntegrateUntil(0, densityDifference);

                result.heightPercentageInWater = heightPercentageInWater;

                result.height = stackedHeight + (((t.maxHeight / 2) - t.maxHeight * heightPercentageInWater) + t.offset.y);

                float frequenzy = Frequenzy(ToZProgress(localPosition.z));

                float degreeRotation = Mathf.Cos(frequenzy) * waveAmplitude * (waveFrequenzy / (1 / 3f));

                float objectArea = t.maxWidth * t.maxLength;

                objectForward.y = 0;
                objectForward.Normalize();

                ///calculate the rotation for x and z axis. Use z direction axis as x rotation axis (both ways)
                result.newEulerAngle = new Vector3(-degreeRotation * objectForward.z, t.transform.eulerAngles.y, -degreeRotation * objectForward.x);
            }
        }
        catch
        {
        }
        return(result);
    }
Esempio n. 6
0
 public void AddWaterObject(WaterRigidBody waterBody)
 {
     waterObjects.Add(waterBody);
 }
Esempio n. 7
0
 protected virtual float CrashImpact(WaterRigidBody w)
 {
     return(w.Velocity.v.magnitude);
 }