예제 #1
0
        public void add(InPlay obj)
        {
            bool haveAdded = false;

            if (!AdditionalMath.contains(Area, obj.getRekt()) && parent != null)
            {
                parent.add(obj);
                return;
            }
            for (int x = 0; x < 2; x++)
            {
                for (int y = 0; y < 2; y++)
                {
                    RectangleF rect = obj.getRekt();
                    if (AdditionalMath.contains(innerQuads[x, y], rect))
                    {
                        haveAdded = true;
                        if (children[x, y] == null && Area.Height * Area.Width * .25 > minimum)
                        {
                            children[x, y] = new QuadNode(this, innerQuads[x, y], minimum, quadLevel + 1);
                        }

                        children[x, y].add(obj);
                    }
                }
            }
            if (!haveAdded)
            {
                dict.Add(obj);
            }
        }
예제 #2
0
 public void operateCollisions(GameObject obj, int x)
 {
     for (int y = x + 1; y < objects.Count; y++)
     {
         GameObject obj2  = objects.ElementAt(y);
         RectangleF rekt1 = obj.getRekt();
         RectangleF rekt2 = obj2.getRekt();
         if (AdditionalMath.intersects(rekt1, rekt2))
         {
             obj.collision(obj2);
         }
     }
 }
예제 #3
0
        public void collision(InPlay other)
        {
            GameObject obj  = (GameObject)other;
            string     nome = "player";

            if (obj.getName().GetHashCode() == nome.GetHashCode())
            {
                inPlatCounter++;
                //Console.WriteLine("Collision between " + name + " and " + obj.getName() + " " + inPlatCounter);
            }
            RectangleF rect       = obj.getRekt();
            RectangleF overside   = gravityZone(rect.Height, rect.Width);
            RectangleF under_side = underside(rect.Height, rect.Width);
            bool       inPlat     = AdditionalMath.intersects(overside, rect); /*&& platform.Contains(rect)*/;
            bool       underPlat  = AdditionalMath.intersects(under_side, rect);

            if (underPlat)
            {
                obj.removeForce("speed up");
                RectangleF newRect = new RectangleF(rect.X, under_side.Y - under_side.Height, rect.Width, rect.Height);
                obj.setRekt(newRect);
            }
            if (inPlat)
            {
                //Console.WriteLine("Actually in Plat");
                obj.InPlat = true;
                rect       = new RectangleF(rect.X, hitbox.Y + rect.Height, rect.Width, rect.Height);
                obj.setRekt(rect);
                obj.removeForce("gravity");
                Vector   v     = obj.getVelocity();
                double[] comps = v.getComponent();
                comps[1] = 0;
                v.setComponent(comps);
                if (Universe.keysDown[0] && obj.getName().Equals("player"))
                {
                    obj.addForce(new Vector(1000, Math.PI / 2, VectorType.FORCE, "jump up"));
                }
                //Console.WriteLine(obj.getVelocityMagnitude() + "-speed");
                if (Math.Abs(obj.getVelocityMagnitude()) > .3)
                {
                    //Console.WriteLine(obj.getName() + " friction direction " + (obj.getVelocityDirection() + Math.PI));
                    obj.addForce(new Vector(kineticFriction * Universe.g * obj.getMass(), obj.getVelocityDirection() + Math.PI, VectorType.FRICTION, "friction with " + name));
                }
                else
                {
                    obj.removeForce("friction with " + name);
                }
            }
        }
예제 #4
0
        public void collisions()
        {
            List <InPlay> allPossibleCollisions = new List <InPlay>();

            queueUpInPlays(allPossibleCollisions);
            for (int x = 0; x < dict.Count; x++)
            {
                for (int y = x + 1; y < allPossibleCollisions.Count; y++)
                {
                    InPlay obj1 = dict.ElementAt(x);
                    InPlay obj2 = allPossibleCollisions.ElementAt(y);
                    if (obj1 is GameObject && obj2 is GameObject)
                    {
                        if (AdditionalMath.intersects(obj1.getRekt(), obj2.getRekt()))
                        {
                            obj1.collision(obj2);
                        }
                    }
                    else
                    {
                        //Console.WriteLine("Comparison between {0} and {1}", obj1.toString(), obj2.toString());
                        if (obj1 is Platform)
                        {
                            Platform   plat      = (Platform)(obj1);
                            RectangleF rect      = obj2.getRekt();
                            RectangleF overside  = plat.gravityZone(rect.Height, rect.Width);
                            RectangleF underside = plat.gravityZone(rect.Height, rect.Width);
                            if (AdditionalMath.intersects(rect, overside))
                            {
                                plat.collision(obj2);
                            }
                        }
                        if (obj2 is Platform)
                        {
                            Platform   plat      = (Platform)(obj2);
                            RectangleF rect      = obj1.getRekt();
                            RectangleF overside  = plat.gravityZone(rect.Height, rect.Width);
                            RectangleF underside = plat.gravityZone(rect.Height, rect.Width);
                            if (AdditionalMath.intersects(rect, overside))
                            {
                                plat.collision(obj1);
                            }
                        }
                    }
                }
            }
        }
예제 #5
0
        public bool operatePlats(GameObject obj)
        {
            bool inAnyPlat = false;

            foreach (Platform plat in platforms)
            {
                RectangleF rect        = obj.getRekt();
                RectangleF platform    = plat.getRekt();
                RectangleF gravityZone = plat.gravityZone(rect.Height, rect.Width);
                RectangleF underside   = plat.underside(rect.Height, rect.Width);
                bool       inPlat      = AdditionalMath.contains(gravityZone, rect); /*&& platform.Contains(rect)*/;
                bool       underPlat   = AdditionalMath.contains(underside, rect);
                if (underPlat)
                {
                    obj.removeForce("speed up");
                    RectangleF newRect = new RectangleF(rect.X, underside.Y - underside.Height, rect.Width, rect.Height);
                    obj.setRekt(newRect);
                }
                if (inPlat)
                {
                    inAnyPlat = true;
                    rect      = new RectangleF(rect.X, platform.Y + rect.Height, rect.Width, rect.Height);
                    obj.setRekt(rect);
                    obj.removeForce("gravity");
                    Vector   v     = obj.getVelocity();
                    double[] comps = v.getComponent();
                    comps[1] = 0;
                    v.setComponent(comps);
                    if (keysDown[0] && obj.getName().Equals("player"))
                    {
                        obj.addForce(new Vector(1000, Math.PI / 2, VectorType.FORCE, "jump up"));
                    }
                    if (Math.Abs(obj.getVelocityMagnitude()) > .3)
                    {
                        //Console.WriteLine(obj.getName() + " friction direction " + (obj.getVelocityDirection() + Math.PI));
                        obj.addForce(new Vector(plat.getKineticFriction() * g * obj.getMass(), obj.getVelocityDirection() + Math.PI, VectorType.FRICTION, "friction with " + plat.getName()));
                    }
                    else
                    {
                        obj.removeForce("friction with " + plat.getName());
                    }
                }
            }
            return(inAnyPlat);
        }
예제 #6
0
 public void traverse(ObjectDelegate del, List <QuadNode> queue, bool movementChange)
 {
     for (int a = 0; a < dict.Count; a++)
     {
         InPlay obj = dict.ElementAt(a);
         del(obj);
         if (movementChange && !AdditionalMath.contains(Area, obj.getRekt()))
         {
             if (parent != null)
             {
                 dict.Remove(obj);
                 parent.add(obj);
             }
         }
         for (int x = 0; x < 2; x++)
         {
             for (int y = 0; y < 2; y++)
             {
                 if (movementChange && AdditionalMath.contains(innerQuads[x, y], obj.getRekt()))
                 {
                     if (children[x, y] == null)
                     {
                         children[x, y] = new QuadNode(this, innerQuads[x, y], minimum, quadLevel + 1);
                     }
                     children[x, y].add(obj);
                     dict.Remove(obj);
                 }
             }
         }
     }
     for (int x = 0; x < 2; x++)
     {
         for (int y = 0; y < 2; y++)
         {
             if (children[x, y] != null)
             {
                 queue.Add(children[x, y]);
             }
         }
     }
 }
예제 #7
0
 public void collision(InPlay otherOne)
 {
     if (otherOne is GameObject)
     {
         GameObject other = (GameObject)otherOne;
         double[]   components = velocity.getComponent();
         double[]   theirComponents = other.velocity.getComponent();
         double     momentumX = components[0] * mass + theirComponents[0] * other.mass;
         double     momentumY = components[1] * mass + theirComponents[1] * other.mass;
         double     ourXVelocity, ourYVelocity, theirXVelocity, theirYVelocity;
         if (other.elastic || elastic)
         {
             //kinetic energy when only taking into account velocity in the x direction
             double xJoules = .5 * Math.Pow(components[0], 2) * mass + .5 * Math.Pow(theirComponents[0], 2) * other.mass;
             //formula I figured out
             double   a = .5 * (other.mass + other.mass * other.mass / mass);
             double   b = -momentumX * other.mass / mass;
             double   c = .5 * momentumX * momentumX / mass - xJoules;
             double[] theirPossibleVxs = AdditionalMath.quadraticFormula(a, b, c);
             if (Math.Round(theirPossibleVxs[0], 5) == Math.Round(theirComponents[0], 5))
             {
                 theirXVelocity = theirPossibleVxs[1];
             }
             else
             {
                 theirXVelocity = theirPossibleVxs[0];
                 if (Double.IsNaN(theirXVelocity))
                 {
                     theirXVelocity = 0;
                 }
             }
             double yJoules = .5 * Math.Pow(components[1], 2) * mass + .5 * Math.Pow(theirComponents[1], 2) * other.mass;
             double aa      = .5 * (other.mass + other.mass * other.mass / mass);
             double bb      = -momentumY * other.mass / mass;
             double cc      = .5 * momentumY * momentumY / mass - yJoules;
             //formula I figured out don't question it
             double[] theirPossibleVys = AdditionalMath.quadraticFormula(aa, bb, cc);
             if (Math.Round(theirPossibleVys[0], 5) == Math.Round(theirComponents[1], 5))
             {
                 theirYVelocity = theirPossibleVys[1];
             }
             else
             {
                 theirYVelocity = theirPossibleVys[0];
                 if (Double.IsNaN(theirYVelocity))
                 {
                     theirYVelocity = 0;
                 }
             }
             ourYVelocity = (momentumY - theirYVelocity * other.mass) / mass;
             ourXVelocity = (momentumX - theirXVelocity * other.mass) / mass;
         }
         else
         {
             ourXVelocity = theirXVelocity = momentumX / (mass + other.mass);
             ourYVelocity = theirYVelocity = momentumY / (mass + other.mass);
         }
         double velocityMag      = Math.Sqrt(Math.Pow(ourXVelocity, 2) + Math.Pow(ourYVelocity, 2));
         double otherVelocityMag = Math.Sqrt(Math.Pow(theirXVelocity, 2) + Math.Pow(theirYVelocity, 2));
         double otherDirection   = Math.Atan2(theirYVelocity, theirXVelocity);
         double direction        = Math.Atan2(ourYVelocity, ourXVelocity);
         velocity       = new Vector(velocityMag, direction, VectorType.VELOCITY, "velocity");
         other.velocity = new Vector(otherVelocityMag, otherDirection, VectorType.VELOCITY, "velocity");
         if (momentumX > 0)
         {
             if (rekt.X < other.rekt.X)
             {
                 float change = ((rekt.X + rekt.Width) - other.rekt.X) / 2;
                 rekt       = new RectangleF(rekt.X - change, rekt.Y, rekt.Width, rekt.Height);
                 other.rekt = new RectangleF(other.rekt.X + change, other.rekt.Y, other.rekt.Width, other.rekt.Height);
             }
             if (rekt.X > other.rekt.X)
             {
                 float change = ((other.rekt.X + other.rekt.Width) - rekt.X) / 2;
                 other.rekt = new RectangleF(other.rekt.X - change, other.rekt.Y, other.rekt.Width, other.rekt.Height);
                 rekt       = new RectangleF(rekt.X + change, rekt.Y, rekt.Width, rekt.Height);
             }
         }
         if (momentumY > 0)
         {
             if (rekt.Y < other.rekt.Y)
             {
                 float change = (rekt.Y - (other.rekt.Y - other.rekt.Height)) / 2;
                 other.rekt = new RectangleF(other.rekt.X, other.rekt.Y + change, other.rekt.Width, other.rekt.Height);
                 rekt       = new RectangleF(rekt.X + change, rekt.Y - change, rekt.Width, rekt.Height);
             }
             if (rekt.Y > other.rekt.Y)
             {
                 float change = (other.rekt.Y - (rekt.Y - rekt.Height)) / 2;
                 other.rekt = new RectangleF(other.rekt.X, other.rekt.Y - change, other.rekt.Width, other.rekt.Height);
                 rekt       = new RectangleF(rekt.X + change, rekt.Y + change, rekt.Width, rekt.Height);
             }
         }
     }
 }