コード例 #1
0
 /// <summary>
 /// Creates an 2D axis aligned bounding box.
 /// </summary>
 /// <param name="rectangle">Source rectangle to copy.</param>
 public Box2D(IReadOnlyBox2D rectangle)
 {
     this.MinX  = rectangle.MinX;
     this.MinY  = rectangle.MinY;
     this.SizeX = rectangle.SizeX;
     this.SizeY = rectangle.SizeY;
 }
コード例 #2
0
        /// <summary>
        /// Test for intersection of two rectangles (excluding borders)
        /// </summary>
        /// <param name="a">A rectangle</param>
        /// <param name="b">A second rectangle</param>
        /// <returns>true if the two rectangles overlap</returns>
        public static bool Intersects(this IReadOnlyBox2D a, IReadOnlyBox2D b)
        {
            bool noXintersect = (a.MaxX <= b.MinX) || (a.MinX >= b.MaxX);
            bool noYintersect = (a.MaxY <= b.MinY) || (a.MinY >= b.MaxY);

            return(!(noXintersect || noYintersect));
        }
コード例 #3
0
ファイル: Level.cs プロジェクト: twentySeven7/Zenseless
 public Sprite(string name, IReadOnlyBox2D renderBounds, int layer, NamedStream namedStream)
 {
     Layer        = layer;
     NamedStream  = namedStream ?? throw new ArgumentNullException(nameof(namedStream));
     Name         = name;
     RenderBounds = renderBounds ?? throw new ArgumentNullException(nameof(renderBounds));
 }
コード例 #4
0
 private static void DrawBox(IReadOnlyBox2D rect)
 {
     GL.Vertex2(rect.MinX, rect.MinY);
     GL.Vertex2(rect.MaxX, rect.MinY);
     GL.Vertex2(rect.MaxX, rect.MaxY);
     GL.Vertex2(rect.MinX, rect.MaxY);
 }
コード例 #5
0
        /// <summary>
        /// Test for intersection of the specified box and circle (excluding borders).
        /// </summary>
        /// <param name="box">The box.</param>
        /// <param name="circle">The circle.</param>
        /// <returns>True if the two objects overlap.</returns>
        public static bool Intersects(this IReadOnlyBox2D box, IReadOnlyCircle circle)
        {
            float AxisDeltaDelta(float min, float max, float center)
            {
                var diffMin = min - center;

                if (0 < diffMin) //left case
                {
                    return(diffMin * diffMin);
                }
                else
                {
                    var diffMax = center - max;
                    if (0 < diffMax) //right case
                    {
                        return(diffMax * diffMax);
                    }
                }
                return(0f);
            }

            float d = 0f;

            d += AxisDeltaDelta(box.MinX, box.MaxX, circle.CenterX);
            d += AxisDeltaDelta(box.MinY, box.MaxY, circle.CenterY);
            return(d < circle.Radius * circle.Radius);
        }
コード例 #6
0
ファイル: Logic.cs プロジェクト: twentySeven7/Zenseless
        private static float PaddleBallResponse(IReadOnlyBox2D paddle, IReadOnlyBox2D ball)
        {
            float vY = (paddle.CenterY - ball.CenterY) / (0.5f * paddle.SizeY);

            vY = OpenTK.MathHelper.Clamp(vY, -2.0f, 2.0f);
            return(vY);
        }
コード例 #7
0
        /// <summary>
        /// If an intersection with the frame occurs do the minimal translation to undo the overlap
        /// </summary>
        /// <param name="rectangleA">The rectangle that will be moved to avoid intersection</param>
        /// <param name="rectangleB">The rectangle to check for intersection</param>
        public static void UndoOverlap(this Box2D rectangleA, IReadOnlyBox2D rectangleB)
        {
            if (!rectangleA.Intersects(rectangleB))
            {
                return;
            }

            Vector2[] directions = new Vector2[]
            {
                new Vector2(rectangleB.MaxX - rectangleA.MinX, 0), // push distance A in positive X-direction
                new Vector2(rectangleB.MinX - rectangleA.MaxX, 0), // push distance A in negative X-direction
                new Vector2(0, rectangleB.MaxY - rectangleA.MinY), // push distance A in positive Y-direction
                new Vector2(0, rectangleB.MinY - rectangleA.MaxY)  // push distance A in negative Y-direction
            };
            float[] pushDistSqrd = new float[4];
            for (int i = 0; i < 4; ++i)
            {
                pushDistSqrd[i] = directions[i].LengthSquared();
            }
            //find minimal positive overlap amount
            int minId = 0;

            for (int i = 1; i < 4; ++i)
            {
                minId = pushDistSqrd[i] < pushDistSqrd[minId] ? i : minId;
            }

            rectangleA.MinX += directions[minId].X;
            rectangleA.MinY += directions[minId].Y;
        }
コード例 #8
0
        /// <summary>
        /// draws a GL quad, textured with an animation.
        /// </summary>
        /// <param name="rectangle">coordinates of the GL quad</param>
        /// <param name="totalSeconds">animation position in seconds</param>
        public void Draw(IReadOnlyBox2D rectangle, float totalSeconds)
        {
            var id = (int)CalcAnimationFrame(totalSeconds);

            textures[id].Activate();
            rectangle.DrawTexturedRect(Box2D.BOX01);
            textures[id].Deactivate();
        }
コード例 #9
0
ファイル: Logic.cs プロジェクト: GreenRobotics/Zenseless
        private Vector2 CollisionResponse(IReadOnlyBox2D collider)
        {
            ball.UndoOverlap(collider);
            var newVel = ballVelocity;

            newVel.Y = -newVel.Y;
            return(newVel);
        }
コード例 #10
0
 public void Add(ITexture tex, IReadOnlyBox2D bounds)
 {
     if (!textures.ContainsKey(tex))
     {
         textures.Add(tex, new List <IReadOnlyBox2D>());
     }
     textures[tex].Add(bounds);
 }
コード例 #11
0
        /// <summary>
        /// Draws the specified rectangle.
        /// </summary>
        /// <param name="rectangle">The rectangle.</param>
        /// <param name="id">The identifier.</param>
        public void Draw(IReadOnlyBox2D rectangle, uint id)
        {
            var texCoords = SpriteSheet.CalcSpriteTexCoords(id);

            Texture.Activate();
            rectangle.DrawTexturedRect(texCoords);
            Texture.Deactivate();
        }
コード例 #12
0
        /// <summary>
        /// draws a GL quad, textured with an animation.
        /// </summary>
        /// <param name="rectangle">coordinates ofthe GL quad</param>
        /// <param name="totalSeconds">animation position in seconds</param>
        public void Draw(IReadOnlyBox2D rectangle, float totalSeconds)
        {
            var id        = CalcAnimationSpriteID(FromID, ToID, AnimationLength, totalSeconds);
            var texCoords = SpriteSheet.CalcSpriteTexCoords(id);

            SpriteSheet.Activate();
            rectangle.DrawTexturedRect(texCoords);
            SpriteSheet.Deactivate();
        }
コード例 #13
0
ファイル: View.cs プロジェクト: twentySeven7/Zenseless
 private static void Draw(IReadOnlyBox2D rectangle)
 {
     GL.Begin(PrimitiveType.Quads);
     GL.TexCoord2(0.0f, 0.0f); GL.Vertex2(rectangle.MinX, rectangle.MinY);
     GL.TexCoord2(1.0f, 0.0f); GL.Vertex2(rectangle.MaxX, rectangle.MinY);
     GL.TexCoord2(1.0f, 1.0f); GL.Vertex2(rectangle.MaxX, rectangle.MaxY);
     GL.TexCoord2(0.0f, 1.0f); GL.Vertex2(rectangle.MinX, rectangle.MaxY);
     GL.End();
 }
コード例 #14
0
ファイル: View.cs プロジェクト: GreenRobotics/Zenseless
 internal void DrawBox(IReadOnlyBox2D paddle)
 {
     GL.Begin(PrimitiveType.Quads);
     GL.Vertex2(paddle.MinX, paddle.MinY);
     GL.Vertex2(paddle.MaxX, paddle.MinY);
     GL.Vertex2(paddle.MaxX, paddle.MaxY);
     GL.Vertex2(paddle.MinX, paddle.MaxY);
     GL.End();
 }
コード例 #15
0
 private static void DrawBox(IReadOnlyBox2D rect, float depth)
 {
     GL.Begin(PrimitiveType.Quads);
     GL.Vertex3(rect.MinX, rect.MinY, depth);
     GL.Vertex3(rect.MaxX, rect.MinY, depth);
     GL.Vertex3(rect.MaxX, rect.MaxY, depth);
     GL.Vertex3(rect.MinX, rect.MaxY, depth);
     GL.End();
 }
コード例 #16
0
 /// <summary>
 /// Draws a textured rectangle.
 /// </summary>
 /// <param name="rect">The rectangle coordinates.</param>
 /// <param name="texCoords">The rectangle texture coordinates.</param>
 public static void DrawTexturedRect(this IReadOnlyBox2D rect, IReadOnlyBox2D texCoords)
 {
     GL.Begin(PrimitiveType.Quads);
     GL.TexCoord2(texCoords.MinX, texCoords.MinY); GL.Vertex2(rect.MinX, rect.MinY);
     GL.TexCoord2(texCoords.MaxX, texCoords.MinY); GL.Vertex2(rect.MaxX, rect.MinY);
     GL.TexCoord2(texCoords.MaxX, texCoords.MaxY); GL.Vertex2(rect.MaxX, rect.MaxY);
     GL.TexCoord2(texCoords.MinX, texCoords.MaxY); GL.Vertex2(rect.MinX, rect.MaxY);
     GL.End();
 }
コード例 #17
0
ファイル: Program.cs プロジェクト: octogame/Zenseless
 private static void DrawPlayer(IReadOnlyBox2D o)
 {
     GL.Begin(PrimitiveType.Triangles);
     GL.Color3(Color.GreenYellow);
     GL.Vertex2(o.MinX, o.MinY);
     GL.Vertex2(o.MaxX, o.MinY);
     GL.Vertex2(o.CenterX, o.MaxY);
     GL.End();
 }
コード例 #18
0
 private static void DrawDiamont(IReadOnlyBox2D o)
 {
     GL.Begin(PrimitiveType.Quads);
     GL.Vertex2(o.CenterX, o.MinY);
     GL.Vertex2(o.MaxX, o.CenterY);
     GL.Vertex2(o.CenterX, o.MaxY);
     GL.Vertex2(o.MinX, o.CenterY);
     GL.End();
 }
コード例 #19
0
 /// <summary>
 /// Calculates the corner points of an axis aligned box.
 /// </summary>
 /// <param name="rectangle">The input rectangle.</param>
 /// <returns>4 corner points</returns>
 public static Vector2[] CalcCornerPoints(this IReadOnlyBox2D rectangle)
 {
     return(new Vector2[]
     {
         new Vector2(rectangle.MinX, rectangle.MinY),
         new Vector2(rectangle.MaxX, rectangle.MinY),
         new Vector2(rectangle.MaxX, rectangle.MaxY),
         new Vector2(rectangle.MinX, rectangle.MaxY),
     });
 }
コード例 #20
0
 public ComponentPlayer(Box2D frame, IReadOnlyBox2D clipFrame)
 {
     if (ReferenceEquals(null, frame))
     {
         throw new Exception("Valid frame needed");
     }
     this.frame     = frame;
     this.clipFrame = clipFrame;
     this.Shoot     = false;
 }
コード例 #21
0
ファイル: Program.cs プロジェクト: octogame/Zenseless
 private static void DrawBullet(IReadOnlyBox2D o)
 {
     GL.Begin(PrimitiveType.Quads);
     GL.Color3(Color.White);
     GL.Vertex2(o.MinX, o.MinY);
     GL.Vertex2(o.MaxX, o.MinY);
     GL.Vertex2(o.MaxX, o.MaxY);
     GL.Vertex2(o.MinX, o.MaxY);
     GL.End();
 }
コード例 #22
0
ファイル: Renderer.cs プロジェクト: twentySeven7/Zenseless
 public IDrawable CreateDrawable(string type, IReadOnlyBox2D frame, IAnimation animation)
 {
     if (registeredTypes.TryGetValue(type, out ITexture tex))
     {
         IDrawable drawable = new AnimatedSprite(tex, frame, animation);
         drawables.Add(drawable);
         return(drawable);
     }
     throw new Exception("Unregisterd type " + type.ToString());
 }
コード例 #23
0
 private static void DrawPaddle(IReadOnlyBox2D frame)
 {
     GL.Begin(PrimitiveType.Quads);
     GL.Color3(Color.Green);
     GL.Vertex2(frame.MinX, frame.MinY);
     GL.Vertex2(frame.MaxX, frame.MinY);
     GL.Vertex2(frame.MaxX, frame.MaxY);
     GL.Vertex2(frame.MinX, frame.MaxY);
     GL.End();
 }
コード例 #24
0
 private void DrawRect(IReadOnlyBox2D rectangle, Color4 color)
 {
     GL.Color4(color);
     GL.Begin(PrimitiveType.Quads);
     GL.Vertex2(rectangle.MinX, rectangle.MinY);
     GL.Vertex2(rectangle.MaxX, rectangle.MinY);
     GL.Vertex2(rectangle.MaxX, rectangle.MaxY);
     GL.Vertex2(rectangle.MinX, rectangle.MaxY);
     GL.End();
 }
コード例 #25
0
        private static void DrawCross(IReadOnlyBox2D o)
        {
            GL.Begin(PrimitiveType.Lines);
            GL.Vertex2(o.MinX, o.MinY);
            GL.Vertex2(o.MaxX, o.MaxY);

            GL.Vertex2(o.MaxX, o.MinY);
            GL.Vertex2(o.MinX, o.MaxY);
            GL.End();
        }
コード例 #26
0
 private static void DrawTexturedRect(IReadOnlyBox2D rect, ITexture tex, IReadOnlyBox2D texCoords)
 {
     tex.Activate();
     GL.Begin(PrimitiveType.Quads);
     GL.TexCoord2(texCoords.MinX, texCoords.MinY); GL.Vertex2(rect.MinX, rect.MinY);
     GL.TexCoord2(texCoords.MaxX, texCoords.MinY); GL.Vertex2(rect.MaxX, rect.MinY);
     GL.TexCoord2(texCoords.MaxX, texCoords.MaxY); GL.Vertex2(rect.MaxX, rect.MaxY);
     GL.TexCoord2(texCoords.MinX, texCoords.MaxY); GL.Vertex2(rect.MinX, rect.MaxY);
     GL.End();
     tex.Deactivate();
 }
コード例 #27
0
 private static void DrawTexturedRect(IReadOnlyBox2D Rectangle, ITexture tex)
 {
     GL.Color3(Color.White);
     tex.Activate();
     GL.Begin(PrimitiveType.Quads);
     GL.TexCoord2(0.0f, 0.0f); GL.Vertex2(Rectangle.MinX, Rectangle.MinY);
     GL.TexCoord2(1.0f, 0.0f); GL.Vertex2(Rectangle.MaxX, Rectangle.MinY);
     GL.TexCoord2(1.0f, 1.0f); GL.Vertex2(Rectangle.MaxX, Rectangle.MaxY);
     GL.TexCoord2(0.0f, 1.0f); GL.Vertex2(Rectangle.MinX, Rectangle.MaxY);
     GL.End();
     tex.Deactivate();
 }
コード例 #28
0
 /// <summary>
 /// Tests two rectangles for equal size and position
 /// </summary>
 /// <param name="other">second rectangle</param>
 /// <returns>False if not a rectangle</returns>
 public bool Equals(IReadOnlyBox2D other)
 {
     if (other is null)
     {
         return(false);
     }
     if (ReferenceEquals(this, other))
     {
         return(true);
     }
     return(MinX == other.MinX && MinY == other.MinY && SizeX == other.SizeX && SizeY == other.SizeY);
 }
コード例 #29
0
 /// <summary>
 /// Checks if a point is inside a rectangle (including borders)
 /// </summary>
 /// <param name="rectangle">A rectangle</param>
 /// <param name="x">The x-coordinate of point</param>
 /// <param name="y">The y-coordinate of point</param>
 /// <returns>true if point is inside the rectangle (including borders)</returns>
 public static bool Contains(this IReadOnlyBox2D rectangle, float x, float y)
 {
     if (x < rectangle.MinX || rectangle.MaxX < x)
     {
         return(false);
     }
     if (y < rectangle.MinY || rectangle.MaxY < y)
     {
         return(false);
     }
     return(true);
 }
コード例 #30
0
ファイル: Program.cs プロジェクト: octogame/Zenseless
 private static void DrawEnemy(IReadOnlyBox2D o)
 {
     GL.Begin(PrimitiveType.Triangles);
     GL.Color3(Color.White);
     GL.Vertex2(o.CenterX, o.CenterY);
     GL.Vertex2(o.MaxX, o.CenterY);
     GL.Vertex2(o.MaxX, o.MaxY);
     GL.Color3(Color.Violet);
     GL.Vertex2(o.CenterX, o.CenterY);
     GL.Vertex2(o.MinX, o.CenterY);
     GL.Vertex2(o.MinX, o.MinY);
     GL.End();
 }