Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8107423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:45:57+00:00 2026-06-06T00:45:57+00:00

Sorry if this question is a bit open ended, but I’m pretty new to

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T00:45:59+00:00Added an answer on June 6, 2026 at 12:45 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

sorry if this question seems a bit open ended, but I'm often wanting to
Sorry if this question seems a bit complex but I think its all related
Sorry if this question is mind numbingly easy to answer, but I'm a bit
Sorry of this appears a bit simple a question, but is there a way
I'm sorry if this question is a bit basic, or if I've missed an
I'm sorry if this is a bit of a C-noob question: I know I
Sorry if this question seems stupid, but it's stumped me for a couple days,
Sorry for this question. There is a lot of information but unfortunately I haven't
Sorry if this question is too vague, but I'd rather not muddy it's point
Sorry if this question is very easy, but I can't find the answer anywhere.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.