示例#1
0
    //VERIFY: is OnCollisionEnter the best function to call? There's also -Stay and -Exit
    void OnCollisionEnter2D(Collision2D collision)
    {
        Collider2D targetCollider = collision.collider;

        targetPhysicsBehaviour = targetCollider.gameObject.GetComponent <PhysicsBehaviour>();
        if (targetPhysicsBehaviour != null)
        {
            /*IMPLEMENT: calculate push-back direction
             *
             * To calculate the angle/direction, draw vector from pushback object centre to collision point
             *
             * Vector2.Angle? definitely not any object rotation: must work on static objects too, like a cactus
             *
             * QUESTION: game object transform centre OR collider centre?
             * imagine a sprite with complex coumpound colliders, collider centre seems to make more sense
             *
             */
            targetColliderCenter = CalculateColliderCenter(targetCollider);

            knockbackDirection = targetColliderCenter - localColliderCenter;
            knockbackDirection.Normalize();

            //physics push-back effect
            knockbackSuccessful = targetPhysicsBehaviour.Knockback(knockbackDirection, knockbackStrength);

            if (knockbackSuccessful)
            {
                //Debug.Log(gameObject.name + " pushed " + targetCollider.gameObject.name + ". Vector: " + knockbackDirection.ToString());
            }
            else
            {
                //Debug.Log(gameObject.name + " failed to push " + targetCollider.gameObject.name);
            }
        }
    }
示例#2
0
    private void MovePermission(bool p)
    {
        PhysicsBehaviour player = _player;

        player.Stop    = !p;
        _devourer.Stop = !p;
    }
    public static void CheckCollision(PhysicsBehaviour a, PhysicsBehaviour b)
    {
        if (HasIntersection(a, b))
        {
            if (!a.contacts.Contains(b))
            {
                a.contacts.Add(b);
                a.isColliding = true;
            }

            if (!b.contacts.Contains(a))
            {
                b.contacts.Add(a);
                b.isColliding = true;
            }
        }
        else
        {
            if (a.contacts.Contains(b))
            {
                a.contacts.Remove(b);
                a.isColliding = false;
            }

            if (b.contacts.Contains(a))
            {
                b.contacts.Remove(a);
                b.isColliding = false;
            }
        }
    }
 public static bool HasIntersection(PhysicsBehaviour a, PhysicsBehaviour b)
 {
     if (a.type == CollisionType.Cube)
     {
         if (b.type == CollisionType.Cube)
         {
             return(HasIntersection(a.aabb, b.aabb));
         }
         else if (b.type == CollisionType.Sphere)
         {
             return(HasIntersection(b.sphere, a.aabb));
         }
     }
     else if (a.type == CollisionType.Sphere)
     {
         if (b.type == CollisionType.Cube)
         {
             return(HasIntersection(a.sphere, b.aabb));
         }
         else if (b.type == CollisionType.Sphere)
         {
             return(HasIntersection(a.sphere, b.sphere));
         }
     }
     return(false);
 }
 public static void ResolveCollisions(PhysicsBehaviour obj)
 {
     foreach (PhysicsBehaviour collider in obj.contacts)
     {
         ResolveCollision(obj, collider);
     }
     obj.contacts.Clear();
 }
示例#6
0
 public Bullet Initialize()
 {
     rb = GetComponent<Rigidbody> ();
     pb = new PhysicsBehaviour (go);
     pb.setGravityFactor (1.2F);
     this.lifeSpan = 1.0F;
     this.spawnTime = Time.time;
     return this;
 }
示例#7
0
 public Bullet2 Initialize()
 {
     rb = GetComponent<Rigidbody>();
     pb = new PhysicsBehaviour(This);
     //pb.setGravityFactor(1.2F);
     pb.setGravityFactor(1.4F);
     this.spawnTime = Time.time;
     return this;
 }
示例#8
0
    private void Start()
    {
        state       = DoggoState.ROAMING;
        audioSource = GetComponent <AudioSource>();

        status          = GetComponent <DogStatus>();
        agent           = GetComponent <NavMeshAgent>();
        pb              = GetComponent <PhysicsBehaviour>();
        foodDetector    = GetComponentInChildren <FoodDetector>();
        ballDetector    = GetComponentInChildren <BallDetector>();
        myTransform     = transform;
        playerTransform = FindObjectOfType <Player>().transform;
        animator        = GetComponent <Animator>();

        audioSource.Play();
        spawnDespawnParticleSystem.Play();
        AudioSource.PlayClipAtPoint(spawnClip, transform.position);

        StartCoroutine(BarkRandomly());
    }
示例#9
0
    public CollisionManifold(PhysicsBehaviour a, PhysicsBehaviour b)
    {
        mNature   = Nature.Colliding;
        mA        = a;
        mB        = b;
        mVelocity = b.velocity - a.velocity;
        mNormal   = b.transform.position - a.transform.position;

        if (a.type == CollisionType.Cube && b.type == CollisionType.Cube)
        {
            mPenetration = 0;
            mContacts    = new Vector3[0];
        }
        else if (a.type == CollisionType.Cube && b.type == CollisionType.Sphere)
        {
            mPenetration = 0;
            mContacts    = new Vector3[0];
        }
        else if (a.type == CollisionType.Sphere && b.type == CollisionType.Cube)
        {
            mPenetration = 0;
            mContacts    = new Vector3[0];
        }
        else if (a.type == CollisionType.Sphere && b.type == CollisionType.Sphere)
        {
            mPenetration = -0.5f * (mNormal.magnitude - (a.sphere.mRadius + b.sphere.mRadius));

            mContacts = new Vector3[1];
            float h = (0.5f + a.sphere.mRadius * a.sphere.mRadius - b.sphere.mRadius * b.sphere.mRadius) / (2.0f * mNormal.sqrMagnitude);
            mContacts[0] = a.sphere.mCentre + h * (b.sphere.mCentre - a.sphere.mCentre);
        }
        else
        {
            mPenetration = 0;
            mContacts    = new Vector3[0];
        }
    }
    public static void ResolveCollision(PhysicsBehaviour a, PhysicsBehaviour b)
    {
        CollisionManifold manifold = new CollisionManifold(a, b); // The normal will point from a to b

        if (manifold.mPenetration > 0.0f)
        {
            // Linear displacement
            if (a.mobile)
            {
                a.transform.position -= manifold.mNormal.normalized * manifold.mPenetration;
            }
            if (b.mobile)
            {
                b.transform.position += manifold.mNormal.normalized * manifold.mPenetration;
            }

            // Linear Impulse
            Vector3 vr        = manifold.mVelocity;
            Vector3 n         = manifold.mNormal;
            float   vrn       = Vector3.Dot(vr, n);
            float   e         = Mathf.Min(a.bounciness, b.bounciness);
            float   InvMasses = (1.0f / a.mass) + (1.0f / b.mass);
            float   j         = (-(1.0f + e) * vrn) / InvMasses;

            // Friction
            Vector3 t        = vr - (vrn * n);
            float   jt       = (-(1.0f + e) * (Vector3.Dot(vr, t))) / InvMasses;
            float   friction = Mathf.Sqrt(a.friction * b.friction);
            jt = Mathf.Max(jt, -j * friction);
            jt = Mathf.Min(jt, j * friction);

            // Adjust velocities
            a.velocity = a.velocity - ((jt / a.mass) * n);
            b.velocity = b.velocity + ((jt / b.mass) * n);
        }
        b.contacts.Remove(a);
    }
示例#11
0
    public override void Fire()
    {
        if (!CanFire())
        {
            return;
        }

        if (!firing)
        {
            audioSource.Play();
        }

        firing = true;

        magazine.Fire();
        muzzleWaterDroplets.Play();

        lr.gameObject.SetActive(true);

        RaycastHit hit;
        Ray        ray = new Ray(muzzle.position, muzzle.forward);

        float totalDistance;

        if (Physics.BoxCast(
                ray.origin,
                lr.startWidth * muzzle.localScale,
                ray.direction,
                out hit,
                muzzle.rotation,
                maxDistance,
                layers,
                QueryTriggerInteraction.Ignore))
        {
            GameObject go = hit.collider.gameObject;
            Rigidbody  rb = go.GetComponentInParent <Rigidbody>();

            if (rb)
            {
                Doggo d = go.GetComponentInParent <Doggo>();
                if (d)
                {
                    PhysicsBehaviour pb = d.pb;
                    if (pb && pb.ControlledByAgent)
                    {
                        pb.DisableAgentAndSetupForPhysics();
                    }

                    d.status.AddBoost(Attributes.CLEANLINESS);
                }

                Vector3 force = waterStrengthDistanceCurve.Evaluate(hit.distance / maxDistance)
                                * maxWaterStrength * (-hit.normal + Vector3.up);

                rb.AddForceAtPosition(force, hit.point);
            }
            totalDistance = hit.distance;
        }
        else
        {
            onHitParticleSystem.Stop(true, ParticleSystemStopBehavior.StopEmitting);
            totalDistance = maxDistance;
        }

        Vector3 dir = ray.direction;

        lr.positionCount = numberOfSegments;

        for (int i = 0; i < numberOfSegments; i++)
        {
            float   perc     = i / (float)numberOfSegments;
            float   distance = perc * maxDistance;
            Vector3 pos      = ray.GetPoint(perc * maxDistance);

            Vector3 beamPos = pos +
                              Random.Range(0.01f, 0.015f) * Mathf.Sin(30 * (i % 2 == 0 ? 1 : -1) * Time.time) * Vector3.up;

            if (distance >= totalDistance)
            {
                lr.positionCount = i;

                break;
            }

            lr.SetPosition(i, beamPos);
        }

        onHitParticleSystem.transform.position = lr.GetPosition(lr.positionCount - 1);
        if (!onHitParticleSystem.isPlaying)
        {
            onHitParticleSystem.Play();
        }
    }
示例#12
0
    void Awake()
    {
        this.damageText = GameObject.Find("Player" + id + "_text/damage" + id).GetComponent<TextMesh> ();
        this.liveText = GameObject.Find ("Player" + id + "_text/lives" + id).GetComponent<TextMesh> ();

        weapon = GetComponent<Weapon2> ();
        weapon.setOwner (spine);
        currentWeapons = new WeaponType[2];
        game = FindObjectOfType<Game> ();
        rb = spine.GetComponent<Rigidbody> ();
        pb = new PhysicsBehaviour (spine);

        this.rb.mass = 5.0F;

        nextFire = new float[2];
        nextFire [0] = Time.time;
        nextFire [1] = Time.time;

        weaponFireSources = spine.GetComponents<AudioSource> ();

        weaponFireSources [0].clip = audioClips [0];
        weaponFireSources [1].clip = audioClips [1];

        //weaponFireSources [0].volume = 0.065F;
        //weaponFireSources [1].volume = 0.065F;
        weaponFireSources [0].volume = 0.007F;
        weaponFireSources [1].volume = 0.007F;

        currentWeapons [0] = WeaponType.MACHINEGUN;
        currentWeapons [1] = WeaponType.SHOTGUN;

        upperLeftArm = spine.transform.Find ("chest/shoulder.L/upper_arm.L").gameObject;
        upperRightArm = spine.transform.Find ("chest/shoulder.R/upper_arm.R").gameObject;

        leftHand = upperLeftArm.transform.Find ("forearm.L/hand.L").gameObject;
        rightHand = upperRightArm.transform.Find ("forearm.R/hand.R").gameObject;
    }