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.

I'm using viewport.project() to find my mouse position in 3D space. I have my projection, view and world matrices defined in the same area that they are used to draw the model. Code is below:

NOTE: Please forgive the awful mess, I've been working on this for two weeks and to no avail. EDIT: It is notable to mention that I have disabled rotation in case it affected my mouse position. It did not. Here is the method that I'm calling to get the mouse position.

viewport.Project(New Vector3(mousePos.X, mousePos.Y, 0), projection, view, world)

Dim transforms() As Matrix
transforms = New Matrix(Model.Bones.Count) {}
Dim campos As Vector3
campos = New Vector3(0.0F, 0.0F, 5000.0F)
Model.CopyAbsoluteBoneTransformsTo(transforms)
For Each Mesh As ModelMesh In Model.Meshes
    For Each effect As BasicEffect In Mesh.Effects
        effect.EnableDefaultLighting()
        camTarget = New Vector3(0, 0, 0)
        myScale.X = 0.2
        myScale.Y = 0.2
        myScale.Z = 0.2
        effect.View = Matrix.CreateLookAt(campos, New Vector3(0, 0, 0), Vector3.Up)
        Dim translationMatrix As Matrix
        effect.World = transforms(Mesh.ParentBone.Index)
        world = effect.World
        effect.World = effect.World * Matrix.CreateScale(myScale)

        '   effect.World = effect.World * Matrix.CreateRotationY(CSng(MathHelper.ToRadians(CSng(Rotation)))) * Matrix.CreateRotationY(MathHelper.ToRadians(-90.0F))
        effect.World = effect.World * Matrix.CreateFromYawPitchRoll(1.0F, 1.0F, 0.0F)
        translationMatrix = Matrix.CreateTranslation(WorldPos + New Vector3(Offset.X, Offset.Y, 0))
        effect.World = effect.World * translationMatrix
        WorldPos += MovementRay * Speed



        effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(60.0F), Camera._aspectRatio, 1.0F, 100000.0F)
        'effect.Projection = Matrix.CreateOrthographic(Viewport.Width, Viewport.Height, -5000, 5000)
        projection = effect.Projection

        view = effect.View


        'Testing

        'Testing



        If Selected = True Then
            effect.AmbientLightColor = Color.Green.ToVector3
        End If
        ScreenPos = Viewport.Project(WorldPos, effect.Projection, effect.View, effect.World)
        Mesh.Draw()
    Next
Next

My mouse position only occasionally actually produces a reasonable result- every other time, it's completely off and it throws my calculations. What am I doing wrong?

share|improve this question

closed as too localized by Byte56, John McDonald, jhocking, Josh Petrie, bummzack Apr 17 '13 at 7:42

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

2  
Unfortunately, "fix my code" questions are too localized for the site. I suggest you find a know working example and build off of that. Voting to close as too localized. –  Byte56 Apr 16 '13 at 16:30
    
On my last question you down voted me because I wasn't clear enough. Now you are downvoting me because I'm too specific. Voting to consider you an angry little man with too much time on your hands. –  Ares513 Apr 16 '13 at 20:57
    
You can start voting at 3000 rep. I haven't seen the "angry little man" voting option, but you could probably suggest it in Game Development Meta. Please consider, I'm not the only one voting, just the only one commenting. Would you prefer no one commented and you were just down voted? I comment because it's useful for you to know why you're being down voted. Try to keep that in mind and keep your comments professional. –  Byte56 Apr 16 '13 at 21:31
    
shrugs I see no reason why down voting is even necessary. I'm an amateur programmer looking for advice, not a veteran. I talked to people on the #xna irc chat, and they suggested I include my code and be more specific. Obviously they were wrong. –  Ares513 Apr 16 '13 at 22:44
2  
The vote represents the community opinion of the question, including how interesting it is and how well it fits within the guidelines of the site. The vote is on the question, not you. Try not to take it personally. Beginner questions are fine. However, there are rules for what the questions can be about. I encourage you to continue asking your questions, and answering any you know how to answer. Once you have 20 rep, you can ask more localized questions like this in Game Development Chat. –  Byte56 Apr 16 '13 at 23:02

1 Answer 1

up vote 2 down vote accepted

You are using the wrong method?

Project

http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.viewport.project.aspx

Projects a 3D vector from object space into screen space.

As you can read there Project is from world coordinates to screen coordinates (even though it returns a Vector3). If you want to know what your mouse hovers over you should use UnProject:

UnProject

http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.viewport.unproject.aspx

Converts a screen space point into a corresponding point in world space.

Note that of course unprojecting will only give you a ray, since given only the camera and 2D mouse coordinates you still don't know the depth. You can find the exact position by finding the closest point of intersection with the ray and your geometry.

share|improve this answer

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