Beispiel #1
0
 private void OnPhysicsCollisionExit(PhysicsCollider other)
 {
     if (PlayerOn && other.GetComponent <PlayerMovement>())
     {
         Debug.Log("Player leaving planet");
         PlayerOn = null;
     }
 }
Beispiel #2
0
 void OnPhysicsCollisionExit(PhysicsCollider other)
 {
     if (other.CompareTag("Terrain"))
     {
         Debug.Log("Left planet");
         physics.attractionActive = true;
         onPlanet = null;
     }
 }
Beispiel #3
0
        public static PhysicsBody Reflect(PhysicsBody body, PhysicsCollider other)
        {
            //Get vector from other to body (collision normal)
            Vector2 norm = other.Center - body.Collider.Center;

            //Reflect body velocity across normal

            return(body);
        }
Beispiel #4
0
 public override bool Overlapping(PhysicsCollider other)
 {
     if (other is CirclePhysicsCollider)
     {
         Vector2 btwn = other.Center - Center;
         float   r    = radius + ((CirclePhysicsCollider)other).radius;
         return(Vector2.SqrMagnitude(btwn) <= r * r);
     }
     else
     {
         throw new System.NotImplementedException();
     }
 }
Beispiel #5
0
        void Update()
        {
            //TODO: make more efficient (don't test EVERY single collider pair every frame)
            //ideas: use a different data structure?  (HashSet lookup is O(1) though, perhaps worse in practice)
            //find a smarter way to choose comparisons?  Recursive Sectoring?

            //check each collider pair for collisions
            for (int l = 0; l < cols.Count; l++)
            {
                for (int r = l + 1; r < cols.Count; r++)
                {
                    PhysicsCollider lcol = cols[l];
                    PhysicsCollider rcol = cols[r];

                    CollisionInfo info = new CollisionInfo(lcol, rcol);
                    if (lcol.Overlapping(rcol))
                    {
                        if (atlas.Contains(info))
                        {
                            //continuous collision
                            lcol.SendMessage("OnPhysicsCollisionStay", rcol,
                                             SendMessageOptions.DontRequireReceiver);
                            rcol.SendMessage("OnPhysicsCollisionStay", lcol,
                                             SendMessageOptions.DontRequireReceiver);
                        }
                        else
                        {
                            //new collision
                            //Debug.Log("Adding collision");
                            atlas.Add(info);
                            lcol.SendMessage("OnPhysicsCollisionEnter", rcol,
                                             SendMessageOptions.DontRequireReceiver);
                            rcol.SendMessage("OnPhysicsCollisionEnter", lcol,
                                             SendMessageOptions.DontRequireReceiver);
                        }
                        ResolveCollision(info);
                    }
                    else if (atlas.Contains(info))
                    {
                        //collision ended
                        atlas.Remove(info);
                        lcol.SendMessage("OnPhysicsCollisionExit", rcol,
                                         SendMessageOptions.DontRequireReceiver);
                        rcol.SendMessage("OnPhysicsCollisionExit", lcol,
                                         SendMessageOptions.DontRequireReceiver);
                    }
                }
            }
        }
Beispiel #6
0
 void OnPhysicsCollisionEnter(PhysicsCollider other)
 {
     if (other.CompareTag("Terrain"))               //hit planet
     {
         if (onPlanet == null)
         {
             Debug.Log("Hit planet");
             physics.attractionActive = false;
             physics.Stop();
             onPlanet = other.GetComponent <Planet>();
         }
         else                     //squished between planets
                                  //TODO: kill player (probably a different script tho)
         {
         }
     }
 }
Beispiel #7
0
 public override Vector2 GetOverlapVector(PhysicsCollider other)
 {
     //returns a vector pointing away from other collider
     if (other is CirclePhysicsCollider)
     {
         if (!Overlapping(other))
         {
             return(Vector2.zero);
         }
         Vector2 dist = Center - ((CirclePhysicsCollider)other).Center;
         return(dist * (radius + ((CirclePhysicsCollider)other).radius - dist.magnitude));
     }
     else
     {
         throw new System.NotImplementedException();
     }
 }
Beispiel #8
0
 public abstract Vector2 GetOverlapVector(PhysicsCollider other);
Beispiel #9
0
 public abstract bool Overlapping(PhysicsCollider other);
Beispiel #10
0
 private void OnPhysicsCollisionEnter(PhysicsCollider other)
 {
     Debug.Log("Player entering planet");
     PlayerOn = other.GetComponent <PlayerMovement>();
 }
Beispiel #11
0
 void Start()
 {
     velocity     = Vector2.zero;
     acceleration = Vector2.zero;
     Collider     = GetComponent <PhysicsCollider>();
 }
Beispiel #12
0
 public void RemoveCollider(PhysicsCollider collider)
 {
     cols.Remove(collider);
 }
Beispiel #13
0
 public void RegisterCollider(PhysicsCollider collider)
 {
     cols.Add(collider);
 }
Beispiel #14
0
 public CollisionInfo(PhysicsCollider l, PhysicsCollider r)
 {
     col1 = l;
     col2 = r;
 }