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 9222365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:49:22+00:00 2026-06-18T03:49:22+00:00

I developped a small games like minecraft. and I’m stuck for a few days

  • 0

I developped a small games like “minecraft”. and I’m stuck for a few days on the calculation of the direction of the cursor.
Let me explain.
I would like to target a cube with my cursor in the middle of the screen and make it disappear with a click. But I do not see any how to recover this information ..
Can someone point me to a “sample / tutorial” or explanation please?
Thank you very much.

My screenShoot picture game :
enter image description here

  • 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-18T03:49:22+00:00Added an answer on June 18, 2026 at 3:49 am

    I take no credit for most of this–it’s what I used in my 3D XNA game and it worked. The comments are fairly explanative. Basically it projects a Ray straight from your cursor position and checks to see if it Intersects() the boundingsphere of any map objects (Units in this case)

        /// <summary>
        /// Creates a Ray translating screen cursor position into screen position
        /// </summary>
        /// <param name="projectionMatrix"></param>
        /// <param name="viewMatrix"></param>
        /// <returns></returns>
        public Ray CalculateCursorRay(Matrix projectionMatrix, Matrix viewMatrix)
        {
            // create 2 positions in screenspace using the cursor position. 0 is as
            // close as possible to the camera, 1 is as far away as possible.
            Vector3 nearSource = new Vector3(mousePosition, 0f);
            Vector3 farSource = new Vector3(mousePosition, 1f);
    
            // use Viewport.Unproject to tell what those two screen space positions
            // would be in world space. we'll need the projection matrix and view
            // matrix, which we have saved as member variables. We also need a world
            // matrix, which can just be identity.
            Vector3 nearPoint = GraphicsDevice.Viewport.Unproject(nearSource,
                projectionMatrix, viewMatrix, Matrix.Identity);
    
            Vector3 farPoint = GraphicsDevice.Viewport.Unproject(farSource,
                projectionMatrix, viewMatrix, Matrix.Identity);
    
            // find the direction vector that goes from the nearPoint to the farPoint
            // and normalize it....
            Vector3 direction = farPoint - nearPoint;
            direction.Normalize();
    
            // and then create a new ray using nearPoint as the source.
            return new Ray(nearPoint, direction);
        }
    
        private Vector3 cursorRayToCoords(Ray ray, Vector3 cameraPos)
        {
            Nullable<float> distance = ray.Intersects(new Plane(Vector3.Up, 0.0f));
            if (distance == null)
                return Vector3.Zero;
            else
            {
                return cameraPos + ray.Direction * (float)distance;
            }
        }
    
        public override void Update(GameTime gameTime)
        {
            cursorRay = CalculateCursorRay(camera.Projection, camera.View);
            cursorOnMapPos = cursorRayToCoords(cursorRay, cursorRay.Position);
            checkInput(gameTime);
    
            base.Update(gameTime);
        }
    
        /// <summary>
        /// Returns the nearest unit to the cursor
        /// </summary>
        /// <returns>The unit nearest to the cursor</returns>
        private Unit getCursorUnit()
        {
            Unit closestUnit = null;
            float? nearestDistance = null;
            float? checkIntersect = null;
    
            foreach (Unit unit in map.Units)//checks to see if unit selection
            {
                checkIntersect = cursorRay.Intersects(unit.BoundingSphere);
                if (checkIntersect != null)//if intersection detected
                {
                    if (nearestDistance == null) //first unit found
                    {
                        closestUnit = unit;
                        nearestDistance = (float)checkIntersect;
                    }
                    else if ((float)checkIntersect < (float)nearestDistance)//for any others, only find the nearest
                    {
                        closestUnit = unit;
                        nearestDistance = (float)checkIntersect;
                    }
                }
            }
    
            return closestUnit;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have developed a small application with Google Web Toolkit and would like to
I've developed a Ruby application (a small game), and I would like to 'distribute'
I developed a small nifty webserver in C and would like to evaluate its
I have developed a small view that contains special char like 'é' and when
We are a small group of developers and we want to create a Future-like
I am developping a small training ASP .Net web application. I have developped an
I have developed a small administration page for game servers which you can add/remove/edit
I developed small CakePHP application, and now I want to add one more table
Hi i developed small web application, in that all the functionality working fine but
I've developed a small (very small) ftp client and server. Both of these programs

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.