Exemple #1
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));
        }
        private void NullifyContactForces(PhysicsBody body, ref Vector vector)
        {
            foreach (var contact in contacts.FindAll(c => c.body1 == body))
            {
                if (!contact.Active)
                {
                    continue;
                }

                var    target = contact.body2;
                double par    = vector.ScalarProjection(contact.normal);
                double perp   = vector.ScalarProjection(contact.normal.LeftNormal);

                vector = perp * contact.normal.LeftNormal + (par < 0 ? par : 0) * contact.normal;
            }

            foreach (var contact in contacts.FindAll(c => c.body2 == body))
            {
                if (!contact.Active)
                {
                    continue;
                }

                var    target = contact.body1;
                double par    = vector.ScalarProjection(contact.normal);
                double perp   = vector.ScalarProjection(contact.normal.LeftNormal);

                vector = perp * contact.normal.LeftNormal + (par < 0 ? par : 0) * contact.normal;
            }
        }
Exemple #3
0
        /// <summary>
        /// Onko peliolio kahden pisteen välissä
        /// </summary>
        /// <param name="pos1">Ensimmäinen piste</param>
        /// <param name="pos2">Toinen piste</param>
        /// <returns></returns>
        public bool IsBetween(Vector pos1, Vector pos2)
        {
            Vector normal = (pos2 - pos1).Normalize();
            double ep     = this.AbsolutePosition.ScalarProjection(normal);
            double p1p    = pos1.ScalarProjection(normal);
            double p2p    = pos2.ScalarProjection(normal);

            if (ep < p1p || ep > p2p)
            {
                return(false);
            }

            double pn = pos1.ScalarProjection(normal.RightNormal);
            double en = this.AbsolutePosition.ScalarProjection(normal.RightNormal);

            return(Math.Abs(en - pn) <= 0.5 * Math.Sqrt(this.Width * this.Width + this.Height * this.Height));
        }
Exemple #4
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));
        }