void OnParticleCollision(GameObject other)
    {
        if (other != transform.root.gameObject)
        {
            //Debug.Log(other.name);
            Rigidbody2D otherBody = other.GetComponent <Rigidbody2D>();
            if (otherBody)
            {
                mainThruster.GetCollisionEvents(other, collisionEvents);
                for (int i = 0; i < collisionEvents.Count; i++)
                {
                    Vector3 incidentVelocity = collisionEvents[i].velocity;
                    Vector3 intersection     = collisionEvents[i].intersection;
                    Vector3 intersectionDir  = intersection - transform.position;
                    if (otherBody != vehicleController.vehicleBody && Vector3.Dot(transform.forward, intersectionDir.normalized) > 0.9f)
                    {
                        //Vector3 colliderSize = otherBody.GetComponentInChildren<Collider2D>().bounds.size;
                        //float largestSide = Mathf.Max(colliderSize.x, colliderSize.y, colliderSize.z);

                        //float totalParticles = mainThruster.particleCount + secondaryThruster.particleCount;
                        //float percentVelocity = incidentVelocity.magnitude / (mainThruster.startSpeed + secondaryThruster.startSpeed) / 2f;
                        //if (percentVelocity < 0) percentVelocity = 0;
                        //if (percentVelocity > 1) percentVelocity = 1;

                        float incidentDistance = intersectionDir.magnitude, flameLength = mainThruster.startSpeed * mainThruster.startLifetime;
                        //float torqueSign =
                        //float torqueSign = (Vector3.Dot(otherBody.transform.right, intersectionDir.normalized) * -1) * (Vector3.Dot(otherBody.transform.forward, intersectionDir.normalized));
                        float torqueSign = Mathf.Sin(2 * VectorHelpers.AngleSigned(otherBody.transform.forward, intersectionDir.normalized, Vector3.forward)) > 0 ? -1 : 1;
                        //float percentTorque = Mathf.Clamp01(incidentDistance / largestSide);
                        float percentForce = 1 - (incidentDistance / flameLength);
                        if (percentForce < 0)
                        {
                            percentForce = 0;
                        }
                        if (percentForce > 1)
                        {
                            percentForce = 1;
                        }

                        float   forceOnPoint = (vehicleController.thrustForce * percentForce / vehicleController.thrusters.Length) / Mathf.Clamp(mainThruster.particleCount / 5, 1, 1000);
                        Vector3 thrustForce  = incidentVelocity.normalized * forceOnPoint;
                        otherBody.AddForce(thrustForce);
                        otherBody.AddTorque(forceOnPoint * incidentDistance * torqueSign);
                        Debug.DrawRay(intersection, thrustForce, Color.black);
                    }
                }
            }
        }
    }
Example #2
0
    private void RotationLogic()
    {
        float rotationAngle = vehicle.inputLeftStick.x;

        if (inDanger)
        {
            VehicleController otherShip = imminentDanger.transform.GetComponent <VehicleController>();

            if (otherShip)
            {
                Vector2 otherShipTrajectory = otherShip.velocity * dangerRayTimeFrame;
                float   trajectoryAngle     = -VectorHelpers.AngleSigned(otherShip.transform.position - vehicle.transform.position, (Vector2)otherShip.transform.position + otherShipTrajectory - (Vector2)vehicle.transform.position, transform.up);
                rotationAngle = trajectoryAngle + (trajectoryAngle > 0 ? 5 : -5);
                //Debug.Log("Trajectory Angle: " + trajectoryAngle);
            }
            else
            {
                float percentVelocityInDanger = Vector2.Dot(vehicle.velocity.normalized, imminentDanger.normal);
                percentVelocityInDanger = Mathf.Clamp(percentVelocityInDanger, 0.6f, 1);

                Vector2 reflection = Vector2.Reflect((imminentDanger.point - (Vector2)transform.position), imminentDanger.normal).normalized;
                dangerAvoidanceVector = Vector2.Lerp(reflection, imminentDanger.normal, percentVelocityInDanger);

                rotationAngle = VectorHelpers.AngleSigned(transform.forward, dangerAvoidanceVector, transform.up);
            }
        }
        else
        {
            rotationAngle = VectorHelpers.AngleSigned(transform.forward, (destination - transform.position).normalized, transform.up);
        }

        if ((inDanger && Mathf.Abs(rotationAngle) <= minDangerAngle) || (!inDanger && Mathf.Abs(rotationAngle) <= minTargetAngle))
        {
            rotationAngle = 0;
        }

        //float maxRotationAngle = 360 * vehicle.revolutionsPerSecond * Time.fixedDeltaTime;
        float maxRotationAngle = 360 * vehicle.cappedRotSpeed * Time.fixedDeltaTime;
        float steerPercent     = Mathf.Clamp(rotationAngle / maxRotationAngle, -1, 1);

        if (steerPercent != 0)
        {
            vehicle.inputLeftStick = new Vector2(Mathf.Lerp(vehicle.inputLeftStick.x, steerPercent, Time.deltaTime * steerLerp), 0); steering = true;
        }
        else if (steering)
        {
            vehicle.inputLeftStick = Vector2.zero; steering = false;
        }
    }
    private void CalculateTrajectory()
    {
        if (projectileBody && cappedRocketRotSpeed > 0)
        {
            thrustPercent = 1;

            if (lockTarget)
            {
                Vector3 displacement = lockTarget.position - transform.position;
                if (displacement.magnitude <= lockRange)
                {
                    float angle            = VectorHelpers.AngleSigned(transform.forward, displacement.normalized, Vector3.forward);
                    float maxRotationAngle = 360 * cappedRocketRotSpeed * Time.fixedDeltaTime;
                    percentRotation = Mathf.Clamp(angle / maxRotationAngle, -1, 1);
                    //projectileBody.angularDrag = torqueForce / ((cappedRocketRotSpeed * VehicleController.KI2ME * projectileBody.mass) + (Time.fixedDeltaTime / torqueForce));
                }
            }
        }
    }