Greetings,

I've been given a challenge to build a simple game for my daughter. The idea is that you smash the spacebar, and a ball drops into the game, goes down some ramps, and lands at the bottom. Being a newbie programmer, this is becoming a challenge.

I decided to use ray-tracing to determine if my ball has collided with the ramps, and I have a Polygon class that defines the vertices of the bounding boxes for collisions with the ramps. I know that I need to determine how many times the rays cross the edges of the polygon, and I couldn't think of any way to do that except to calculate all possible points along the edges. I wrote this function to do this task. Just two questions

) Is there a more efficient way I can accomplish calculating the points along the edge, and
) Is there a more efficient way to accomplish my overall task (determine collision between two objects)

Edges is

private List<Point> Edges = new List<Point>();
// Calculate all possible points along an edge, given two points.
        private void InitializeEdge(Point p1, Point p2)
        {
            int ImagX, ImagY;
            bool p1XGreater = false, p1YGreater = false;
            // Imaginary point used for calculation.
            if (p1.X > p2.X)
            {
                ImagX = p1.X - p2.X;
                p1XGreater = true;
            }
            else
            {
                ImagX = p2.X - p1.X;
            }

            if (p1.Y > p2.Y)
            {
                ImagY = p1.Y - p2.Y;
                p1YGreater = true;
            }
            else
            {
                ImagY = p2.Y - p1.Y;
            }

            Point ImagPoint = new Point(ImagX, ImagY);

            // Determine how many calculations to make.
            int NumPointsAlongEdge;
            if (ImagX > ImagY)
                NumPointsAlongEdge = ImagX;
            else
                NumPointsAlongEdge = ImagY;

            // Calculate the points and add them to Edges.
            for (int i = 0; i < NumPointsAlongEdge; i++)
            {
                int CalcX = ImagX;
                CalcX = (CalcX * i) / NumPointsAlongEdge;
                int CalcY = ImagY;
                CalcY = (CalcY * i) / NumPointsAlongEdge;

                if (p1XGreater)
                    CalcX += p2.X;
                else
                    CalcX += p1.X;

                if (p1YGreater)
                    CalcY += p2.Y;
                else
                    CalcY += p1.Y;

                Point p = new Point(CalcX, CalcY);
                Edges.Add(p);
            }
        }

Perhaps this site can help you a bit.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.