Exemplo n.º 1
0
        /// <summary>
        /// calculates collisions between the ball and a paddle, and finds the paddle surface that was hit
        /// </summary>
        /// <param name="paddle">the paddle that is being hit</param>
        /// <param name="newPosition">the position the ball has</param>
        /// <param name="newVelocity">the velocity the ball is trying to move with</param>
        /// <returns>the position, length and surface of the collision</returns>
        public CollisionReturn CollidesWithPaddle(Paddle paddle, Vector newPosition, Vector newVelocity)
        {
            Ray[] Vectors = new Ray[4]; // vector rays of moving ball
            Vectors[TopEdge] = new Ray(newPosition, newVelocity);
            Vectors[RightEdge] = new Ray(Vector.Add(newPosition, new Vector(Size.X, 0)), newVelocity);
            Vectors[BottomEdge] = new Ray(Vector.Add(newPosition, new Vector(0, Size.Y)), newVelocity);
            Vectors[LeftEdge] = new Ray(Vector.Add(newPosition, new Vector(Size.X, Size.Y)), newVelocity);

            CollisionReturn collisionReturn = new CollisionReturn(false);

            foreach (Ray ray in Vectors)
            {
                int edgeID = 0;
                foreach (Ray edge in paddle.Edges)
                {
                    RaycastReturn raycastReturn = Ray.Raycast(ray, edge);
                    if (raycastReturn.Intersects)
                    {
                        if (collisionReturn.Intersects)
                        {
                            if (raycastReturn.length < collisionReturn.length)
                            {
                                collisionReturn = new CollisionReturn(raycastReturn);
                                collisionReturn.Edge = edgeID;
                            }
                        }
                        else
                        {
                            collisionReturn = new CollisionReturn(raycastReturn);
                            collisionReturn.Edge = edgeID;
                        }
                    }
                    edgeID++;
                }
            }
            return collisionReturn;
        }
Exemplo n.º 2
0
        /// <summary>
        /// calculates collisions between the ball and a block, and finds the block surface tha was hit
        /// </summary>
        /// <param name="block">the block being hit</param>
        /// <param name="newPosition">the position the ball has</param>
        /// <param name="newVelocity">the velocity the ball is trying to move with</param>
        /// <param name="collidedRays">a list of rayIDs which have already collided with a surface</param>
        /// <returns>the position, length and surface of the collision</returns>
        public CollisionReturn CollidesWithBlock(Block block, Vector newPosition, Vector newVelocity, List<int> collidedRays)
        {
            // vector rays of moving ball
            Ray[] Vectors = new Ray[4];
            Vectors[TopEdge] = new Ray(newPosition, newVelocity);
            Vectors[RightEdge] = new Ray(Vector.Add(newPosition, new Vector(Size.X, 0)), newVelocity);
            Vectors[BottomEdge] = new Ray(Vector.Add(newPosition, new Vector(0, Size.Y)), newVelocity);
            Vectors[LeftEdge] = new Ray(Vector.Add(newPosition, new Vector(Size.X, Size.Y)), newVelocity);

            CollisionReturn collisionReturn = new CollisionReturn(false);

            int rayID = 0;
            foreach (Ray ray in Vectors)
            {
                int edgeID = 0;
                if (!collidedRays.Contains(rayID)) // if the ray being used hasn't already collided with a box
                {
                    // check every edge of the block
                    foreach (Ray edge in block.Edges)
                    {
                        RaycastReturn raycastReturn = Ray.Raycast(ray, edge); // see if the ray meets the edge
                        if (raycastReturn.Intersects)
                        {
                            // if there has already been a collision, check for a shorter one
                            if (collisionReturn.Intersects)
                            {
                                if (raycastReturn.length < collisionReturn.length) // if the ball velocity ray hits this edge before any other ray hits an edge
                                {
                                    collisionReturn = new CollisionReturn(raycastReturn);
                                    collisionReturn.Edge = edgeID;
                                    collisionReturn.Ray = rayID;
                                }
                            }

                            // haven't had a ray collision yet, so use this collision by default
                            else
                            {
                                collisionReturn = new CollisionReturn(raycastReturn);
                                collisionReturn.Edge = edgeID;
                                collisionReturn.Ray = rayID;
                            }
                        }
                        edgeID++; // check next edge
                    }
                }
                rayID++; // check next ray
            }
            return collisionReturn; // return results
        }