Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

This is a follow-up to my earlier question:

Game Editor - When screen is clicked, how do you identify which object that is clicked?

Something I've noticed is that picking only calculates a ray and checks for the closest objects that intersects it, but since this method isn't working for placing object in a 3D World what are the best way of doing that? A ray is just a line that goes from one point in a certain direction, it checks the first object that intersects it that is closest. But not if you don't have any objects.

I've used one method for calculating a position with an Isometric Camera, but currently I have an arc ball camera and it doesn't produce the same results. Is there a general way that actually produces a good coordinate?

This is currently the method I'm trying to understand, but I've found no tutorials/information on how to actually translate numbers between 0 and 1 to real coordinates on the screen.

3D Arc Ball Camera with this method:

public static Vector3 GetScreenCoordinates(MouseState Mouse, Viewport viewport, Matrix Projection, Matrix View, Matrix World)
{
    Vector3 pos1 = viewport.Unproject(new Vector3(Mouse.X, Mouse.Y, 0), Projection, View, World);
    Vector3 pos2 = viewport.Unproject(new Vector3(Mouse.X, Mouse.Y, 1), Projection, View, World);
    Vector3 dir = Vector3.Normalize(pos2 - pos1);
    dir.Normalize();

    return dir;
}
share|improve this question
    
Do you mean that you're in an empty 3D world and you're trying to place an object with screen clicks? –  Polar Mar 18 '13 at 18:05
1  
Your question depends on what coordinate system you want these coordinates in: Device Coordinates (all pixels on the monitor), Application/Screen Coordinates (all pixels in the application window, ie. Mouse.X/Mouse.Y), View Coordinates (3d from the viewer's location), or World Coordinates. –  Shotgun Ninja Mar 18 '13 at 18:08
1  
Also, the comment on your original thread:(gamedev.stackexchange.com/a/31341/18568) gives some good links for understanding the sequence of transformations between coordinate systems. First, figure out what system you want them in, then take Mouse.X and Mouse.Y and apply the correct transformations. –  Shotgun Ninja Mar 18 '13 at 18:14
    
@ShotgunNinja There is the slight issue that with just an x and a y value (mouse position), with no reference points, you can't obtain a 3D location. –  Polar Mar 18 '13 at 19:41
add comment

1 Answer 1

Use a plane or three planes to check collision against them.

If the world is empty you need to collide with something... one easy solution is setting a reference plane... usually the ground... and check ray collision against the plane to get coordinates in world space...

when you work with objects in an editor, the easier is working in world coordinates... and being independent of screen coordinates...

Here is a video of the editor i'm doing now... where you can see how it is done...

share|improve this answer
    
Why is a ray-cast insufficient? –  Anko Mar 19 '13 at 8:08
    
if you want to place objects in an empty world you need a plane reference to let the ray collide with something... and get coordinates in world space... that is the most interesting... –  Blau Mar 19 '13 at 9:19
    
But creating a plane at Y: 0 is not very effective if you wish to have a more dynamic editor. I've used your example earlier to calculate the location, but to dynamically set the plane to whatever fits the camera. Say I rotate the camera so it's 90 degrees from the ground, meaning that the ground is below the camera but not seen by the matrices; why would I still get an Y: 0 in when I click about say 50 pixels above from Y or something. To somehow dynamically place this plane depending on the camera location. –  Deukalion Mar 19 '13 at 12:44
    
you can combine collision against a plane or three planes and against the current 3d models like I show in the video... I only let to rotate the camera around Up vector... and it's enough to edit/model my isometric world... –  Blau Mar 19 '13 at 16:51
    
Only problem with your method is that I can't get a good value on Y -Axis, because everything in the scene shouldn't be at 0 without any height. And it isn't an isometric camera. This works, but won't give me the height: pastebin.com/Z6nR2H5H –  Deukalion Mar 20 '13 at 15:36
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.