Esempio n. 1
0
        private Projection projectOnAxis(ConvexShape shape, Vector2 axis)
        {
            float min = Vector2Utils.DotProduct(axis, shape.Points[0]);
            float max = min;

            for (int i = 1; i < shape.Points.Count; i++)
            {
                float p = Vector2Utils.DotProduct(axis, shape.Points[i]);

                if (p < min)
                {
                    min = p;
                }
                else if (p > max)
                {
                    max = p;
                }
            }

            return(new Projection(min, max));
        }
Esempio n. 2
0
        public void resolve(GameTime gameTime)
        {
            if (this.Callback != null)
            {
                this.Callback(Node1, Node2);
            }

            //System.Console.WriteLine (_penetration);

            /*
             * if(entity1Collision->trigger && entity2Collision->trigger)
             *      return;
             *
             * if(entity2Collision->solid && entity1Collision->solid)
             *      return;
             */

            // TODO add triggers, check if both nodes are triggers, if not, do code here

            Vector2 delta = Node1.Position - Node2.Position;

            if (Vector2Utils.DotProduct(_normal, delta) > 0)
            {
                _normal *= -1.0f;
            }

            if (!Node1.Immovable && Node2.Immovable)
            {
                Node1.Position += _normal * (Penetration);
            }

            if (!Node2.Immovable && Node1.Immovable)
            {
                Node2.Position += _normal * (Penetration);
            }

            if (!Node1.Immovable && !Node2.Immovable)
            {
                Node1.Position -= _normal * (Penetration / 2);
                Node2.Position += _normal * (Penetration / 2);
            }

            float   x1  = Vector2Utils.DotProduct(_normal, Node1.Velocity);
            Vector2 v1x = _normal * x1;
            Vector2 v1y = Node1.Velocity - v1x;

            float   x2  = Vector2Utils.DotProduct(_normal, Node2.Velocity);
            Vector2 v2x = _normal * x2;
            Vector2 v2y = Node2.Velocity - v2x;

            // 1 is mass
            float node1mass = 1;
            float node2mass = 1;

            float massFormula1 = (node1mass - node2mass) / (node1mass + node2mass);
            float massFormula2 = (node2mass - node1mass) / (node2mass + node1mass);

            if (!Node1.Immovable)
            {
                Node1.Velocity = v1x * massFormula1 + v2x * massFormula2 + v1y;
            }

            if (!Node2.Immovable)
            {
                Node2.Velocity = v1x * massFormula2 + v2x * massFormula1 + v2y;
            }
        }