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

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

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 on other sites

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 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.

Edited by taby

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 on other sites

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.

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

.

Edited by taby

1. 1
2. 2
3. 3
Rutin
13
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• Forum Statistics

• Total Topics
633700
• Total Posts
3013418
×