Search code examples
c#xna2dcollision-detectionsprite

Seeking advice for best way to create a reflecting "light beam" for 2D game in XNA


Sorry if this question is a bit open ended, but I'm pretty new to C# and XNA... and this forum in fact!

I'm creating a game and require a beam of light to emit from a fixed point (bottom left in attached screen capture, belonging to class named PowerStation) and reflect from mirrors which can be moved and rotated by the user. I've added a per-pixel collision detection method, as can be seen working in the attached capture (Mirror turns red).

Here is a screen cap of the game so far

Currently I've been trying to test for obstacles to the beam's path by creating a Point and moving it along the path of the light until a collision is detected; from there recording the distance travelled and then stretching the Beam sprite in a non-uniform way by the required amount. This is proving difficult already and I think there's still a long way to go with this method.

I was just wondering if anyone has any advice as to the best method to go about detecting obstacles, their rotation, and determining the direction to reflect the Beam (by which side of Mirror is hit), before I fully commit to something that might get really complicated or never even work?

Here is what my Beam class looks like currently... all but one classes inherit from one base class called Object and Objects are all declared in a static objectList belonging to a separate class Items. Apologies if this is bad, messy coding!

        class Beam : Object
{
    private Vector2 start;
    private double length;
    private Vector2 POC;



    public Beam(Vector2 pos)
        : base(pos)
    {

        spriteName = "beam";
        depth = 0.2f;
        solid = true;
        foreach (Object o in Items.objectList)
        {
            if (o.GetType() == typeof(PowerStation))
            {
                start = o.Origin;
            }
        }
    }

    public override void Update()
    {
        Point newPoint = new Point((int)Origin.X, (int)Origin.Y);
        while ((!(collision(new Mirror(new Vector2(0, 0))))) && (newPoint.X > 0) && (newPoint.Y > 0)) // && boundaries of window
        {
            newPoint.Y--; //will be changed to a Vector
        }

        POC = PointOfCollision(new Mirror(new Vector2(0, 0))); // Need to make it do POC of newPoint, not just the Beam Object!
        length = FindLength(start, new Vector2(50, 50));
        //Scale = new Vector2( , );     //amount to scale sprite


        base.Update();
    }

    private double FindLength(Vector2 pos1, Vector2 pos2)
    {
        return (Math.Sqrt(Math.Pow(Math.Abs(pos2.X - pos1.X), 2.0f) + Math.Pow(Math.Abs(pos2.Y - pos1.Y), 2.0f)));
    }
}

Any help would be greatly appreciated! Thanks in advance


Solution

  • Forget pixels--since the mirrors apparently can be at any angle you'll certainly have places where you intersect a mirror not on an even pixel. While you could simply count the impact point as the even pixel this will produce a slightly wrong path for the beam.

    Instead, take your beam and iterate over all the mirrors, compute the intersection of the beam and the plane of the mirror (handle the case where there is no intersection), then check the intersection to make sure it's within the physical mirror. Record the match that occurs within the shortest distance.

    You can almost certainly speed this calculation by figuring bounding boxes for all the mirrors in advance and when checking a beam note what quadrant it's heading towards from the current point--you can reject at least half (and usually 3/4) of all the mirrors with two integer comparisons each.

    Repeat until there is either no intersection (the beam escapes) or it hits something that stops it rather than reflects it.

    If there are a LOT of mirrors you could take this even farther and chop the screen up into sectors, each of which has a list of mirrors in it. Check only the mirrors in the current sector, if you don't get a hit figure out what sector it enters next and repeat. This is a bit more math casting the ray of the beam in exchange for excluding most of the mirrors without a single instruction. If you are dealing with user-placed mirrors I doubt there would be enough of them to be worth doing it.