Notifications
Clear all

[Closed] Mouse Coords to World Space Coords?

Hei everyone,

I’m currently facing a couple of problems, that’s why I thought I might get some professional help here .
Actually I just want to get the coords of my mouse pointer and compare them to the position of some vertices.

E.g.: If (distance (Mouse.pos $blubb.Verts[5].pos) < 10) then “moo”.

BUT I’m running into serious Problems now. First of all the mouse.pos value is point2 and the value of the vertex is Point3 so I can’t create a distance. I thought I could cast the point2 into point3 but this didn’t work out.

Secondly and even more serious: The Mouse.pos are screen coords. But I need WorldSpace Coords to make up a meaningful comparison between the cursor and the vertex.

Does anybody have an idea of how I could achieve this?

Thanks

“ko’gen”.

3 Replies

Look into the ‘gw’ methods. These will transform a pixel coordinate in the view (get the mouse position in the view (mouse.pos), rather than on the screen (mouse.screenPos)) to a coordinate or ray in the scene.

Most of these methods, such as mapScreenToView, need you to specify the ‘depth’ of your pixel in the scene – as the viewport is only 2D and your scene is 3D. So you may wish to use the ray method, mapScreenToWorldRay, and construct a rather long ray. You can then use the ‘Point-Line Distance’ function described in the Geometrical calculations : points, lines, planes : intersections, distances, angles topic to get the distance between that point and the closest point on that line (ray), in world units.

You could also go about it the other way – depending on whether you really need a ‘world distance’ between your mouse cursor and the objects. If you just want the distance in pixels in the viewport, have a peek at the tutorial ‘how to create a vertex renderer’. There’s a function in there that will map a world coordinate point to a 2D plane; such as your viewport. From there you can do a very simple calculation to get the distance between those 2D pixel points.

Hei Zeboxx2,

thanks for your help.
Actually I was hoping there is another method that’s easier and faster. The algorithm needs to calculate the distance for every point of the mesh from the cursor. So creating a ray for every single point of a mesh within 1/25second will be too slow for that purpose, I guess. Temporarily I’m thinking about another way to do what I want.

I’m young, so I’ve got plenty of time

Thanks again.

Cheers “ko’gen”.

you wouldn’t actually construct a ray for every vertex (even though that wouldn’t be particularly slow). You would only construct a ray once, for the mouse’s cursor, so that you have a line going through world space. Then you can run that “distance between point and line” function to get the distance.

The alternative – projecting the mesh’s verts to pixel coordinates instead – is pretty fast as well; the example in the help file should show that much, and it even paints pixels into a bitmap… much slower than what you’d need

Essentially, though, you’re asking a question that you think is simple, but isn’t. Your mouse cursor is in 2D coordinates, the points in your scene are in 3D coordinates. You can’t get a meaningful distance between them without either making your mouse’s coordinates 3D in a meaningful way, or the points in your scene 2D.

There’s also the ‘pickPoint’ function, that will return the position of the world space ‘cursor’ (the type you’d see if you use snaps), but you’re somewhat limited in what you can do while that function is active, you have to activate it explicitly, etc.