Translate 2D mouse position to place an object in 3D space

Started by
13 comments, last by taby 8 years, 1 month ago

I want to be able to click on the screen and place a box at that spot in perspective 3D space. I would need to give the box the offsets in world view. The camera is at 0,0,0. In addition to the screen coordinates where 0,0 is the center of the screen, I also know the z order of where I want to box to go. Say -150

I've been struggling with this problem for a day now and google searches are not helping. Thought I could solve it with simple tri which is all I'm good at.

Can anyone solve this?

Advertisement

This code converts an x,y position into a 3D position (last_click_float_x, last_click_float_y). The eye position that I used is 0,0,1 and the image plane is at z = 0:


    void touch_up_pos(int x, int y)
    {
        const float pi = 4.0f*atanf(1.0f);
        const float aspect = (float)(m_renderbufferWidth) / (float)(m_renderbufferHeight);
            
        const float fx = 2.0f * ((float)(x) / (float)(m_renderbufferWidth - 1)) - 1.0f;
        const float fy = 2.0f * ((float)(y) / (float)(m_renderbufferHeight - 1)) - 1.0f;
        const float y_fov = pi/4; // pi/4 radians = 45 degrees
        const float tangent = tan(y_fov / 2.0f);
        last_click_float_x = aspect * tangent* fx;
        last_click_float_y = -tangent * fy;
        last_click_float_z = 0;
        
        // NSLog(@"touch up pos %f %f", last_click_float_x, last_click_float_y);
    }

This is how I do it (sorry for the poor html, it's weebly)

http://stainlessbeer.weebly.com/blog/efficient-method-of-3d-object-placement

Fun little snippet.

Many people try to turn a mouse's 2D position into a single location in a 3D world, forgetting that the projection results in a ray or a volume (the front of the volume is the area of a pixel).

Thank you guys for your help!

Taby, thank you for posting the code. It works GREAT!

All I had to do was multiply the final X and Y values by a positive z order.

I couldn't be more happy! There's no way I would have been able to write that code. Taby, you are Awesome!

Fun little snippet.

Many people try to turn a mouse's 2D position into a single location in a 3D world, forgetting that the projection results in a ray or a volume (the front of the volume is the area of a pixel).

Yeah, I use the concept of an image plane in my description of the code. Whether or not the code works for a raytracer is undecided as of yet. smile.png

Thank you guys for your help!

I found the code on Stack Overflow a few months ago. If you like the code, then upvote me.

done! You have your up vote.

I wish I could look at this code and know what it's doing. If anyone can explain it, please do.

Use printf or cout or NSLog or whatever to print out the values held by fx, fy, etc., to see what's going on during the calculations. Let me know how that goes.

.

This topic is closed to new replies.

Advertisement