• Advertisement
Sign in to follow this  

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

This topic is 781 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

 

Share this post


Link to post
Share on other sites
Advertisement

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!

Share this post


Link to post
Share on other sites

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

Edited by taby

Share this post


Link to post
Share on other sites

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.

Edited by taby

Share this post


Link to post
Share on other sites

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.

Edited by taby

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement