Exemple #1
0
        private void UpdateChildrenPos()
        {
            Vector pdiff = Position - prevPos;
            Angle  adiff = Angle - prevAngle;

            if (ObjectCount > 0)
            {
                foreach (var o in Objects)
                {
                    o.Angle    += adiff;
                    o.Position += pdiff;
                    Vector vdiff = o.Position - Position;
                    o.Position += -vdiff + Vector.FromLengthAndAngle(vdiff.Magnitude, adiff + vdiff.Angle);
                }
            }

            if (Objects.AmountToBeAdded > 0)
            {
                var objs = Objects.GetObjectsAboutToBeAdded();
                foreach (var o in objs)
                {
                    o.Angle    += adiff;
                    o.Position += pdiff;
                    Vector vdiff = o.Position - Position;
                    o.Position += -vdiff + Vector.FromLengthAndAngle(vdiff.Magnitude, adiff + vdiff.Angle);
                }
            }

            prevPos   = Position;
            prevAngle = Angle;
        }
Exemple #2
0
        /// <summary>
        /// Liikuttaa kappaletta kohti määränpäätä.
        /// </summary>
        protected virtual void MoveToTarget()
        {
            if (!moveTarget.HasValue)
            {
                moveTimer.Stop();
                return;
            }

            Vector d  = moveTarget.Value - Position;
            double vt = moveSpeed * moveTimer.Interval;

            if (d.Magnitude < vt)
            {
                Vector targetLoc = moveTarget.Value;
                moveTimer.Stop();
                Position   = moveTarget.Value;
                moveTarget = null;

                if (arrivedAction != null)
                {
                    arrivedAction();
                }
            }
            else
            {
                Position += Vector.FromLengthAndAngle(vt, d.Angle);
            }
        }
Exemple #3
0
        protected override void MoveToTarget()
        {
            if (!moveTarget.HasValue)
            {
                Stop();
                moveTimer.Stop();
                return;
            }

            Vector d  = moveTarget.Value - AbsolutePosition;
            double vt = moveSpeed * moveTimer.Interval;

            if (d.Magnitude < vt)
            {
                Vector targetLoc = moveTarget.Value;
                Stop();
                moveTimer.Stop();
                moveTarget = null;

                if (arrivedAction != null)
                {
                    arrivedAction();
                }
            }
            else
            {
                Vector dv = Vector.FromLengthAndAngle(moveSpeed, d.Angle) - this.Velocity;
                Hit(Mass * dv);
            }
        }
Exemple #4
0
        /// <summary>
        /// Onko piste <c>p</c> tämän olion ympäröivän suorakulmion sisäpuolella.
        /// </summary>
        public bool IsInsideRect(Vector point)
        {
            Vector p = this.AbsolutePosition;

            if (AbsoluteAngle == Angle.Zero)
            {
                // A special (faster) case of the general case below
                if (point.X >= (p.X - Width / 2) &&
                    point.X <= (p.X + Width / 2) &&
                    point.Y >= (p.Y - Height / 2) &&
                    point.Y <= (p.Y + Height / 2))
                {
                    return(true);
                }
            }
            else
            {
                Vector unitX  = Vector.FromLengthAndAngle(1, this.AbsoluteAngle);
                Vector unitY  = unitX.LeftNormal;
                double pX     = p.ScalarProjection(unitX);
                double pY     = p.ScalarProjection(unitY);
                double pointX = point.ScalarProjection(unitX);
                double pointY = point.ScalarProjection(unitY);

                if (pointX >= (pX - Width / 2) &&
                    pointX <= (pX + Width / 2) &&
                    pointY >= (pY - Height / 2) &&
                    pointY <= (pY + Height / 2))
                {
                    return(true);
                }
            }

            return(IsInsideChildren(point));
        }
Exemple #5
0
        /// <summary>
        /// Maanpinnan normaalivektori annetulla x-koordinaatilla
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        public Vector GetGroundNormal(double x)
        {
            if (heights == null || x < Left || x > Right)
            {
                return(Vector.UnitY);
            }

            int    n    = heights.Length;
            double step = Width / (n - 1);

            double indexX     = (Width / 2 + x) / step;
            int    lowerIndex = (int)Math.Floor(indexX);
            int    upperIndex = (int)Math.Ceiling(indexX);

            if (upperIndex >= n)
            {
                return(Vector.UnitY); // DEBUG
            }
            if (lowerIndex == upperIndex)
            {
                return((GetGroundNormal(x - step / 2) + GetGroundNormal(x + step / 2)) / 2);
            }

            double k = (heights[upperIndex] - heights[lowerIndex]) / step;

            return(Vector.FromLengthAndAngle(1, Angle.ArcTan(k) + Angle.RightAngle));
        }
Exemple #6
0
        protected virtual void PrepareThrowable(PhysicsObject obj, Angle angle, double force, double distanceDelta, double axialDelta)
        {
            double d = (this.Width + obj.Width) / 2 + distanceDelta;
            Angle  a = this.AbsoluteAngle + angle;

            obj.Position = this.AbsolutePosition + a.GetVector() * d + (a + Angle.RightAngle).GetVector() * axialDelta;
            obj.Hit(Vector.FromLengthAndAngle(force, a));
        }
Exemple #7
0
 protected void Move(Angle direction)
 {
     if (Owner == null)
     {
         return;
     }
     Owner.Move(Vector.FromLengthAndAngle(Speed, direction));
     Turn(direction);
 }
Exemple #8
0
        protected void Move(Vector direction)
        {
            if (Owner == null || direction == Vector.Zero)
            {
                return;
            }
            double d = Math.Min(direction.Magnitude, Speed);

            Owner.Move(Vector.FromLengthAndAngle(d, direction.Angle));
            Turn(direction.Angle);
        }
Exemple #9
0
        public override void Update(Time time)
        {
            if (Velocity.Magnitude > MaxVelocity)
            {
                Velocity = Vector.FromLengthAndAngle(MaxVelocity, Velocity.Angle);
            }
            if (Math.Abs(AngularVelocity) > MaxAngularVelocity)
            {
                AngularVelocity = Math.Sign(AngularVelocity) * MaxAngularVelocity;
            }

            base.Update(time);
        }
Exemple #10
0
        public void Update(Time time)
        {
            if (Velocity.Magnitude > MaxVelocity)
            {
                Velocity = Vector.FromLengthAndAngle(MaxVelocity, Velocity.Angle);
            }
            if (AngularVelocity > MaxAngularVelocity)
            {
                AngularVelocity = MaxVelocity;
            }

            //base.Update( time );
        }
Exemple #11
0
        /// <summary>
        /// Asetetaan uusi suunta. Hakee olioita edestä ja sivuilta ja arpoo tyhjistä kohdista uuden suunnan.
        /// Jos eteen tai sivuille ei pääse, uusi suunta on taaksepäin.
        /// </summary>
        private void SetNextDirectionAndDestination()
        {
            PhysicsObject owner = this.Owner as PhysicsObject;

            if (owner == null || owner.Game == null)
            {
                return;
            }

            Game          game       = owner.Game;
            List <Vector> directions = new List <Vector> {
                direction,
                Vector.FromLengthAndAngle(tileSize, direction.Angle - Angle.RightAngle),
                Vector.FromLengthAndAngle(tileSize, direction.Angle + Angle.RightAngle)
            };

            GameObject tile;
            double     radius = tileSize / 5;

            while (directions.Count > 0)
            {
                Vector directionCandidate = RandomGen.SelectOne <Vector>(directions);
                directions.Remove(directionCandidate);

                if (labyrinthWallTag != null)
                {
                    tile = game.GetObjectAt(owner.Position + directionCandidate, labyrinthWallTag, radius);
                }
                else
                {
                    tile = game.GetObjectAt(owner.Position + directionCandidate, radius);
                }

                //PhysicsObjects only!
                if (tile == null || tile as PhysicsObject == null || (owner.CollisionIgnoreGroup != 0 && (tile as PhysicsObject).CollisionIgnoreGroup == owner.CollisionIgnoreGroup))
                {
                    direction = directionCandidate.Normalize() * tileSize;
                    //direction.X = Math.Round(direction.X);
                    //direction.Y = Math.Round(direction.Y);

                    destination = owner.Position + direction;
                    return;
                }
            }

            direction = -direction.Normalize() * tileSize;
            //direction.X = Math.Round(direction.X);
            //direction.Y = Math.Round(direction.Y);

            destination = owner.Position + direction;
        }
Exemple #12
0
        /// <summary>
        /// Onko piste <c>p</c> tämän olion sisäpuolella.
        /// </summary>
        public bool IsInside(Vector point)
        {
            Vector p = this.AbsolutePosition;
            double pX, pY;
            double pointX, pointY;

            if (AbsoluteAngle == Angle.Zero)
            {
                // A special (faster) case of the general case below
                pX     = p.X;
                pY     = p.Y;
                pointX = point.X;
                pointY = point.Y;
            }
            else
            {
                Vector unitX = Vector.FromLengthAndAngle(1, this.AbsoluteAngle);
                Vector unitY = unitX.LeftNormal;
                pX     = p.ScalarProjection(unitX);
                pY     = p.ScalarProjection(unitY);
                pointX = point.ScalarProjection(unitX);
                pointY = point.ScalarProjection(unitY);
            }

            if (Shape.IsUnitSize)
            {
                double x = 2 * (pointX - pX) / this.Width;
                double y = 2 * (pointY - pY) / this.Height;
                if (this.Shape.IsInside(x, y))
                {
                    return(true);
                }
            }
            else
            {
                double x = pointX - pX;
                double y = pointY - pY;
                if (this.Shape.IsInside(x, y))
                {
                    return(true);
                }
            }

            return(IsInsideChildren(point));
        }
Exemple #13
0
        private void SetTargetPosition(double dt)
        {
            targetPosition = CurrentTarget.AbsolutePosition;

            if (Delay > 0 && DistanceToTarget.Value > float.Epsilon)
            {
                double maxlength = Math.Sqrt(Math.Pow(Game.Instance.Level.Width, 2) + Math.Pow(Game.Instance.Level.Height, 2));
                targetPosition += Vector.FromLengthAndAngle(maxlength, (CurrentTarget.AbsolutePosition - Owner.AbsolutePosition).Angle);
            }

            IPhysicsObject physTarget = CurrentTarget as IPhysicsObject;

            if (physTarget != null)
            {
                // Take speed and acceleration into account
                targetPosition += ((physTarget.Acceleration * dt) + physTarget.Velocity) * dt;
            }
        }
Exemple #14
0
        ///<inheritdoc/>
        public override void Update(Time time)
        {
            if (Velocity.Magnitude > MaxVelocity)
            {
                Velocity = Vector.FromLengthAndAngle(MaxVelocity, Velocity.Angle);
            }
            if (Math.Abs(AngularVelocity) > MaxAngularVelocity)
            {
                AngularVelocity = Math.Sign(AngularVelocity) * MaxAngularVelocity;
            }

            if (!IsDestroyed)
            {
                Body.Update(time);
            }
            UpdateChildrenPos();
            base.Update(time);
        }
Exemple #15
0
 /// <summary>
 /// Palauttaa satunnaisen vektorin.
 /// </summary>
 /// <param name="minLength">Vektorin minimipituus.</param>
 /// <param name="maxLength">Vektorin maksimipituus.</param>
 /// <returns>Satunnainen vektori</returns>
 public static Vector NextVector(double minLength, double maxLength)
 {
     return(Vector.FromLengthAndAngle(NextDouble(minLength, maxLength), NextAngle()));
 }