How do you convert cursor position to 3D position?


How can I convent the position of the mouse to 3D coordinates?

I know this is a quicky, so hurry up and respond :smiley:

I don’t like your attitude so I’m not even going to try, or give this a any thought.
And apparently you’re not either.

was it the big grin? I wasn’t trying to be rude, I help alot of ppl on this fourm and now im asking for a little somthing in return, thats all. Is it bad to ask for it in a timely fashion? BTW, i have given it a lot of thought and research and found no solutions, I wouldn’t be posting if I haven’t.

Take a look at the gluProject and gluUnProject functions in the BGL module of Blender’s Python API - that may work, but I have never used it so I’m not sure. But it sounds like what you’re asking for.

I’m sorry… I think it was you bumping your own thread after 20 minutes saying hurry up.

What are you trying to use this for?
If you want a custom cursor at the mouse position you can use raySource
that returns the xyz position of the cursor, but you have to use a mouseOverAny sensor,
here’s a tutorial by blendenzo.

Yes, after a little more research, I came across that tutorial but it did not work the way I wanted it to. What I am trying to do is let the user select an object and the move it with the mouse. I figured out a way to this this without converting the 2D cords. to 3D. I just saved the last cursor pos. and subtracted it from the current pos. to see how much it had moved, and then translated the object with this data (that sounded like i was smart). But anyway, another problem arose, the question this time is the exact opposite. How do if convert 3D cords. to 2D? I am going to look into volumes solution because I think thats where the answer may lay. In the mean time, if anyone has another solution, I would be happy to hear it :slight_smile:

you can use camera.getScreenPosition(arg) this isn’t actually converting though.
This give you the objects screen position but you have to multiply it by the height and width of the screen to get the real screen pos.

import GameLogic as g
import Rasterizer as r

c = g.getCurrentController()

object = c.owner

camera = g.getCurrentScene().active_camera
x,y = camera.getScreenPosition(object)

width = r.getWindowWidth()
height = r.getWindowHeight()

x = int(x * width)
y = int(y * height)

print "Object Position: X",x, "Y",y

Here’s a couple of threads that may help with moving an object with the mouse.

Hope that helps. I shouldn’t have responded the way I did earlier.

Keep in mind a point in 2D represents a line in 3D. So you can’t just convert.

A usual way is to set a constant distance to the screen, which provides the third coordinate within the 3D world.
Another way is to follow the line until it hits something (follow the links PhilB told you).