private Vector2D Seek()
        {
            Vector2D currentVelocity = MovingEntity.VVelocity;
            Vector2D desiredVelocity = _nextLocation.Clone().Substract(MovingEntity.VPos).Normalize();

            desiredVelocity.Multiply(MovingEntity.DMaxForce);
            var steering = desiredVelocity.Substract(currentVelocity);

            return(steering);
        }
Example #2
0
        static void Main(string[] args)
        {
            // Bai 1
            List <Student> studentList = new List <Student>();

            chooseAction(studentList);
            Console.ReadLine();

            // Bai 2
            Vector2D a = new Vector2D(2, 3);
            Vector2D b = new Vector2D(5, 6);

            a.WriteInfo("a");
            b.WriteInfo("b");
            Vector2D c = a.Add(b);

            Console.WriteLine("\nAfter adding b to a");
            c.WriteInfo("after adding");
            Vector2D d = a.Div(b);

            Console.WriteLine("\nAfter diving a by b");
            d.WriteInfo("after dividing");
            double e = a.Distance(b);

            Console.WriteLine("\nThe distance between a and b is {0}\n", e);
            a.Swap(ref b);
            a.WriteInfo("a");
            b.WriteInfo("b");
            Vector2D f = a.Clone();

            f.WriteInfo("f (clone of a)");
            Console.ReadLine();
        }
Example #3
0
        private Vector2D ToWorldSpace(Vector2D localPosition, double agentHeading)
        {
            Vector2D positionInLocalSpace           = localPosition.Clone().Add(MovingEntity.VPos);
            Vector2D positionAndHeadingInLocalSpace = positionInLocalSpace.Rotate(-agentHeading);

            return(positionAndHeadingInLocalSpace);
        }
Example #4
0
        private Vector2D ToLocalSpace(Vector2D worldPosition, double agentHeading)
        {
            Vector2D positionInLocalSpace           = worldPosition.Clone().Substract(MovingEntity.VPos);
            Vector2D positionAndHeadingInLocalSpace = positionInLocalSpace.Rotate(agentHeading);

            return(positionAndHeadingInLocalSpace);
        }
Example #5
0
        public void CloneTestSame()
        {
            var vector       = new Vector2D(-3.21, 2.76);
            var clonedVector = (Vector2D)vector.Clone();

            Assert.AreEqual(vector.X, clonedVector.X);
            Assert.AreEqual(vector.Y, clonedVector.Y);
        }
Example #6
0
        public override Vector2D Calculate()
        {
            // All obstacles and its position
            List <ObstacleEntity> obstacles = MovingEntity.World.Obstacles;

            // Current position of the entity.
            Vector2D currentPos = MovingEntity.VPos;

            // Direction the entity currently goes to with applied force.
            Vector2D direction = MovingEntity.VVelocity.Clone();

            // The direction the entity currently goes to, but normalized.
            Vector2D currentDirection = direction.Normalize();

            double speed    = MovingEntity.VVelocity.Length() * 10;
            double maxSpeed = MovingEntity.DMaxSpeed;

            double extra = ((speed / maxSpeed) * MinimumRectangleLength);

            RectangleDistance = MinimumRectangleLength + extra;

            // The square and its distance
            rDist = currentPos.Clone().Add((currentDirection.Clone().Multiply(RectangleDistance)));

            // Create rectangle
            Rectangle = GetRectangle();

            var agentHeading = Math.Atan2(MovingEntity.VVelocity.X, MovingEntity.VVelocity.Y);

            // Get all obstacles within range
            List <ObstacleEntity> obstaclesWithinRange = FindObstaclesWithinRange();

            Vector2D totalForce = new Vector2D();

            foreach (var obstacle in obstaclesWithinRange)
            {
                // All obstacles that are within range
                var objectToLocalSpace = ToLocalSpace(obstacle.VPos, agentHeading);

                // Once we have all obstacles within our range, translate them to the local space and check if they are in
                // front of the moving entity
                if (objectToLocalSpace.Y > 0)
                {
                    // All obstacles that are within range AND in front of the moving entity
                    var severity = RectangleDistance * (1 / Vector2D.DistanceSquared(new Vector2D(0, 0), objectToLocalSpace));

                    // Calculate the forces that should be added to change direction
                    var forceX = -objectToLocalSpace.X * severity;
                    var forceY = -objectToLocalSpace.Y * severity;

                    //And add it to the total force
                    totalForce.Add(ToWorldSpace(new Vector2D(forceX, forceY), agentHeading));
                }
            }
            return(totalForce);
        }
Example #7
0
        public void CloneTestDifferent()
        {
            var vector       = new Vector2D(-3.21, 2.76);
            var clonedVector = (Vector2D)vector.Clone();

            clonedVector.Y *= -2;

            Assert.AreEqual(vector.X, clonedVector.X);
            Assert.AreNotEqual(vector.Y, clonedVector.Y);
        }
Example #8
0
        public override Vector2D Calculate()
        {
            // Generate a random new angle to find a point on the circle. Do this with the TurningAngle steps.
            double currentAngle = LastAngle;
            int    r            = random.Next(0, 30);

            switch (r % 4)
            {
            case 0:
                currentAngle += TurningAngle * 2;
                break;

            case 1:
                currentAngle += TurningAngle;
                break;

            case 2:
                currentAngle -= TurningAngle;
                break;

            case 3:
                currentAngle -= TurningAngle * 2;
                break;
            }

            // Current position of the entity.
            Vector2D currentPos = MovingEntity.VPos;

            // Direction the entity currently goes to with applied force.
            Vector2D direction = MovingEntity.VVelocity.Clone();

            // The direction the entity currently goes to, but normalized.
            Vector2D currentDirection = direction.Normalize();

            // The middle of the circle.
            // This is calculated by adding up the direction (multiplied by the distance) to our current position.
            cDist = currentPos.Clone().Add((currentDirection.Clone().Multiply(CircleDistance)));

            // Calculate our X and Y for our randomly generated new angle.
            double X = Math.Cos(currentAngle) * CircleRadius;
            double Y = Math.Sin(currentAngle) * CircleRadius;

            // Our new destination we want to travel towards.
            vDest = cDist.Clone().Add(new Vector2D(X, Y));

            // One problem: This is the destination from 0, 0.
            // So, to get the dest from our entities location, we can just substract our entities location from it.
            steering = vDest.Clone().Substract(MovingEntity.VPos);

            // Update our last angle so we can get a random angle next time.
            LastAngle = currentAngle;

            // Make sure to limit the steering to the max force of the MovingEntity
            return(steering.Truncate(MovingEntity.DMaxForce));
        }
Example #9
0
        private List <Vector2D> GetRectangle()
        {
            Vector2D direction        = MovingEntity.VVelocity.Clone();
            Vector2D currentDirection = direction.Normalize();
            Vector2D currentPos       = MovingEntity.VPos;

            List <Vector2D> rectangle = new List <Vector2D>();

            Vector2D one   = currentDirection.Perpendicular().Normalize().Multiply(RectangleWidth);
            Vector2D two   = new Vector2D(-one.X, -one.Y);
            Vector2D three = two.Clone().Add(currentDirection.Clone().Multiply(RectangleDistance * 2));
            Vector2D four  = one.Clone().Add(currentDirection.Clone().Multiply(RectangleDistance * 2));

            rectangle.AddRange(new[] { one, two, three, four });

            foreach (Vector2D point in rectangle)
            {
                point.Add(currentPos);
            }

            return(rectangle);
        }
Example #10
0
        public Lion(Vector2D position, World world) : base(position, world)
        {
            HomePosition  = position.Clone();
            Color         = Color.Orange;
            Bradius       = 5;
            VVelocity     = new Vector2D(0, 0);
            DMass         = 15;
            DMaxSpeed     = 5;
            DDeceleration = 3;
            DMaxForce     = 10;

            HashTagLifeGoal = new GoalThinkLion(this);
        }
Example #11
0
        public void Simple()
        {
            var vector = new Vector2D(3, 7);

            var clone = (Vector2D)vector.Clone();

            Assert.AreEqual(3, vector.X);
            Assert.AreEqual(7, vector.Y);

            Assert.AreEqual(3, clone.X);
            Assert.AreEqual(7, clone.Y);

            Assert.AreNotSame(clone, vector);
        }
        public override Vector2D Calculate(Vector2D target)
        {
            Vector2D targetPos = target.Clone();
            Vector2D desired   = targetPos.Sub(movingEntity.Pos);

            desired.Normalize();
            desired.Multiply(movingEntity.MaxSpeed);

            Vector2D force = desired.Sub(movingEntity.Velocity);

            force.Truncate(movingEntity.Max_Force);

            return(force);
        }
Example #13
0
        public static Vector2D Separation(MovingEntity entity, List <MovingEntity> neighbours)
        {
            Vector2D steeringForce = new Vector2D(0, 0);

            foreach (MovingEntity neighbour in neighbours)
            {
                if (neighbour.Tag == false || entity.Pos == neighbour.Pos)
                {
                    continue;
                }

                Vector2D ToAgent = entity.Pos.Clone().Sub(neighbour.Pos);
                steeringForce.Add(ToAgent.Clone().Normalize().Divide(ToAgent.Length()));
            }

            return(steeringForce);
        }
Example #14
0
        public override void Update(float time_elapsed)
        {
            HashTagLifeGoal?.Process();

            Vector2D steeringForce = SteeringBehaviours.Calculate();
            Vector2D acceleration  = steeringForce.Divide(DMass);

            VVelocity.Add(acceleration.Multiply(time_elapsed));

            VVelocity.Truncate(DMaxSpeed);

            VPos.Add(VVelocity.Multiply(time_elapsed));

            if (VVelocity.LengthSquared() > 0.00000001)
            {
                VHeading = VVelocity.Clone().Normalize();
                VSide    = VHeading.Perpendicular();
            }
        }
Example #15
0
        public void Shoot(Vector2D gunLocation, Vector2D missileBayLocation)
        {
            switch (ShotCount)
            {
            case 3:
                if (_level.Ticks >= Bullet.LastFired + Bullet.FireRate)
                {
                    Bullet.LastFired = _level.Ticks;
                    _projectiles.Add(new Bullet(gunLocation, ShotPower, new StraightMotion(new Vector2D(0.41f, 0.25f))));
                    _projectiles.Add(new Bullet(gunLocation, ShotPower, new StraightMotion(new Vector2D(0.5f, 0))));
                    _projectiles.Add(new Bullet(gunLocation, ShotPower, new StraightMotion(new Vector2D(0.41f, -0.25f))));
                }
                break;

            case 2:
                if (_level.Ticks >= Bullet.LastFired + Bullet.FireRate / 2)
                {
                    _lastShotIndex   = (_lastShotIndex + 1) % 2;
                    Bullet.LastFired = _level.Ticks;
                    _projectiles.Add(new Bullet(gunLocation.Clone().TranslateByCoordinates(0, _lastShotIndex * -5), ShotPower, new StraightMotion(new Vector2D(0.5f, 0))));
                }
                break;

            default:
                if (_level.Ticks >= Bullet.LastFired + Bullet.FireRate)
                {
                    Bullet.LastFired = _level.Ticks;
                    _projectiles.Add(new Bullet(gunLocation, ShotPower, new StraightMotion(new Vector2D(0.5f, 0))));
                }
                break;
            }

            if (TotalMissiles < MissileCount && _level.Ticks >= Missile.LastFired + Missile.FireRate)
            {
                Missile.LastFired = _level.Ticks;
                _projectiles.Add(new Missile(missileBayLocation));
            }
        }
Example #16
0
        public static Vector2D Arrive(MovingEntity entity, Vector2D target)
        {
            Vector2D toTarget = target.Clone().Sub(entity.Pos);
            double   distance = toTarget.Length();

            if (distance <= 0)
            {
                return(new Vector2D(0, 0));
            }

            // fine tweaking of deceleration
            const double decelerationTweaker = 4;

            // calculate the speed required to reach the target
            double speed = distance / (1 * decelerationTweaker);

            // make sure velocity doesn't exceed the MaxSpeed
            speed = Math.Min(speed, entity.MaxSpeed);

            Vector2D desiredVelocity = toTarget.Multiply(speed / distance);

            return(desiredVelocity.Sub(entity.Velocity));
        }
        public override void Update(float timeElapsed)
        {
            // Calculate the combined force from each steering behaviour.
            Vector2D steeringForce = SB.Calculate();

            // Acceleration = Force / Mass (Newton's laws of physics).
            Vector2D acceleration = steeringForce.Clone().divide(Mass);

            // Update velocity.
            Velocity.Add(acceleration.Clone().Multiply(timeElapsed));

            // Make sure the moving entity does not exceed maximum speed.
            Velocity.Truncate(MaxSpeed);

            // Update position.
            Pos.Add(Velocity.Clone().Multiply(timeElapsed));

            // Update heading if the velocity is bigger than 0
            if (Velocity.LengthSquared() > 0)
            {
                Heading = Velocity.Clone().Normalize();
            }
        }
        static void InitShape()
        {
            Coefficients        DefaultCoefficients = TimeWarp.Coefficients;
            List <IGeometry2D>  goes = new List <IGeometry2D>();
            List <Coefficients> coes = new List <Coefficients>();


            Vector2D[] leftWingvertecies = new Vector2D[]
            {
                new Vector2D(20, -10),
                new Vector2D(5, 5),
                new Vector2D(-10, 5),
                new Vector2D(20, -30)
            };
            int length = leftWingvertecies.Length;

            Vector2D[] RightWingvertecies = new Vector2D[]
            {
                new Vector2D(20, 10),
                new Vector2D(20, 30),
                new Vector2D(-10, -5),
                new Vector2D(5, -5)
            };
            goes.Add(new Polygon2D(new ALVector2D(0, new Vector2D(15, -5)), leftWingvertecies));
            coes.Add(DefaultCoefficients);
            goes.Add(new Polygon2D(new ALVector2D(0, new Vector2D(15, 5)), RightWingvertecies));
            coes.Add(DefaultCoefficients);


            Vector2D[] RightWingvertecies2 = (Vector2D[])RightWingvertecies.Clone();
            Vector2D[] leftWingvertecies2  = (Vector2D[])leftWingvertecies.Clone();
            for (int pos = RightWingvertecies2.Length - 1; pos > -1; --pos)
            {
                RightWingvertecies2[pos] *= .7f;
            }
            for (int pos = leftWingvertecies2.Length - 1; pos > -1; --pos)
            {
                leftWingvertecies2[pos] *= .7f;
            }
            goes.Add(new Polygon2D(new ALVector2D(MathHelper.PI, new Vector2D(-15, 5)), leftWingvertecies2));
            coes.Add(DefaultCoefficients);
            goes.Add(new Polygon2D(new ALVector2D(MathHelper.PI, new Vector2D(-15, -5)), RightWingvertecies2));
            coes.Add(DefaultCoefficients);


            Vector2D[] RightWingvertecies3 = new Vector2D[]
            {
                new Vector2D(5, 5),
                new Vector2D(0, 7),
                new Vector2D(-15, 7),
                new Vector2D(-20, 5),
                new Vector2D(-10, -40),
                new Vector2D(0, -40)
            };
            Vector2D[] leftWingvertecies3 = new Vector2D[]
            {
                new Vector2D(0, 40),
                new Vector2D(-10, 40),
                new Vector2D(-20, -5),
                new Vector2D(-15, -7),
                new Vector2D(0, -7),
                new Vector2D(5, -5)
            };

            goes.Add(new Polygon2D(new ALVector2D(MathHelper.PI, new Vector2D(-8, 40)), leftWingvertecies3));
            coes.Add(DefaultCoefficients);
            goes.Add(new Polygon2D(new ALVector2D(MathHelper.PI, new Vector2D(-8, -40)), RightWingvertecies3));
            coes.Add(DefaultCoefficients);



            goes.Add(new Polygon2D(new ALVector2D(0, new Vector2D(0, 0)), Polygon2D.FromRectangle(7, 50)));
            coes.Add(DefaultCoefficients);
            DefaultShape = new RigidBodyTemplate(7, 809.028931f, goes.ToArray(), coes.ToArray());
            DefaultShape.BalanceBody();
            //DefaultShape.CalcInertiaMultiplier(.1f);
        }
Example #19
0
 public Vector2D GetAbsoluteGunPosition()
 {
     return(_gunPosition.Clone().TranslateByVector(Ship.Location));
 }
Example #20
0
 public Vertex(Vector2D v, IList <Bone> bones, IList <double> weights)
 {
     m_refPosition = v.Clone();
     m_bones       = bones;
     m_weights     = weights;
 }
Example #21
0
        public static Vector2D Seek(MovingEntity entity, Vector2D target)
        {
            Vector2D desiredVelocity = target.Clone().Sub(entity.Pos).Normalize().Multiply(entity.MaxSpeed);

            return(desiredVelocity.Sub(entity.Velocity));
        }
Example #22
0
 public Vector2D GetAbsoluteMissileBayPosition()
 {
     return(_missileBayPosition.Clone().TranslateByVector(Ship.Location));
 }
Example #23
0