Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Real Mouse Position

Started by lightworker Apr 5, 2005 at 5:10 PM 9 replies 2.1k views
Original Post
lightworker
lightworker
Hi I have serious problem and I will be greatfull for any help I have created app in managed Direct3D that displays large bitmap (orthophotomap) and I am able to put sptites that looks like poligons over it (I am using ortho view) Actualy I have 2 problems: Converting mouse pointer position from screen to "real world" coordinates so I can select any poligon, and allso I should be able to draw such poligons oer photomap. I did some good work but in wrong way that was complicating many things and I so had to start again - everything worked allmost fine but I had problems with poligons position (after inserting poligon was translated by vector dependent on screen position where it was created)... I am still douing some things by testing and checking - I can not figure out how Scaling matrix changes actual size of my map I am providing some code - please help First I was trying to scale world matrix now I am douing it with Projection - same problems So my matrix setup code looks like this: protected void SetupMatrices() { if(scale > 5) scale = 5; if(scale < 0.2) scale = 0.2F; device.Transform.View = Microsoft.DirectX.Matrix.RotationY((float) ( Math.PI )); device.Transform.View *= Microsoft.DirectX.Matrix.LookAtRH( new Vector3(this.cam_offset_x, -1, this.cam_offset_y), new Vector3(cam_offset_x, 0, cam_offset_y), new Vector3(0, 0, 1)); device.Transform.World = Microsoft.DirectX.Matrix.Identity; device.Transform.Projection = Microsoft.DirectX.Matrix.Scaling(scale, scale,1 ); device.Transform.Projection *= Microsoft.DirectX.Matrix.OrthoLH(dest_form.Width, dest_form.Height, -20, 20); } How can I figureout where my cursor points?
Slaweklightworker@wp.pl
matthughson
matthughson
Just so things go a little easier on you when looking for help, the process you are discribing is often refered to as Picking, or Ray Picking.

Here's a little tutorial on doing it: click it up. It uses DX7 and C++, so there will be some conversion issues with managed DX, but all the math is there, which is probably going to be the harder part.

Good luck!

Matt Hughson
__________________________________[ Website ] [ Résumé ] [ [email=contact[at]matthughson[dot]com]Contact[/email] ][ Have I been Helpful? Hook me up! ]
lightworker
lightworker
Thanx

I have greater experience with C++ then c# but I was using only OpenGL with it - so far I had no problems with conversion from unmanaged dx to mdx - thanx for help - I am trying it right now!

Thanx again (btw I have presentation today about 10:00 AM and it is allmost 1:00 AM so I am in a rush :)

Slaweklightworker@wp.pl
lightworker
lightworker
I have noticed that I have allready saw this tutorial - good one but it assumes that I am using 3d view - I am interested only in 2D with ortho view so I believe that I have no triangular Frustum but more rectangular one.. or am I mistaken?

Slaweklightworker@wp.pl
matthughson
matthughson
Oh so the whole thing is in Ortho mode? No projection view at all??

Basically what I'm getting at is that if everything is in orthoview, and you used your real window dimensions to create that view, then the mouse is already in same coordinate system as the objects in your app.

Matt Hughson
__________________________________[ Website ] [ Résumé ] [ [email=contact[at]matthughson[dot]com]Contact[/email] ][ Have I been Helpful? Hook me up! ]
lightworker
lightworker
Yep
(Actualy MousePos+CamPos)
But only when scale factor is 1.0
Scaling is main issue - I am using Scaling matrix to do "zoom" effect.
In "normal" 2d graphics like GDI+ I did such things just by (cam_position+coord)/zoom_factor

In D3D this does not work - Scaling Matrix is different somehow

Slaweklightworker@wp.pl
turnpast
turnpast
Something to look into. Vector3 provides the good looking method called Unproject() I have not used it myself, but it appears to be made for just this sort of situation.
lightworker
lightworker
Indeed Interesting
First attempt brings failure:

With code like:

Microsoft.DirectX.Direct3D.Viewport vp = new Viewport();
vp.Width = this.dest_form_pres.Width;
vp.Height = dest_form_pres.Height;
vp.X = 0;
vp.Y = 0;
Microsoft.DirectX.Vector3 v3d = new Microsoft.DirectX.Vector3(x,y,0);
this.SetupMatrices();
v3d.Unproject(vp, device.GetTransform(TransformType.Projection), device.GetTransform(TransformType.View), device.GetTransform(TransformType.World));


CursorPosition = new PointF(v3d.X, v3d.Y);



I am getting CursorPosition.X or CursorPosition.Y is not a number
I will try to find a way arround it
Slaweklightworker@wp.pl
lightworker
lightworker
After playing arround with this method I have found it returning weird numbers - transforming camera changes coordinates under cursor for certain point and y-coordinate does not even change during mouse move - weird - code bellow:

Microsoft.DirectX.Vector3 v3d = new Vector3(x,y,1);
Microsoft.DirectX.Direct3D.Viewport vp = new Viewport();
vp.Width = this.dest_form_pres.Width;
vp.Height = dest_form_pres.Height;
vp.X = 0;
vp.Y = 0;
this.SetupMatrices();
v3d = Microsoft.DirectX.Vector3.Unproject(v3d,device.Viewport, device.Transform.Projection,device.Transform.View, device.Transform.World);
CursorPosition = new PointF(v3d.X+this.cam_offset_x, v3d.Y+this.cam_offset_y);
Slaweklightworker@wp.pl
turnpast
turnpast
Like I said I have never used it myself. I have seen an example (not english) where the programmer called unporject twice, first with mousepos: x,y,0 and then with mousepos: x,y,1 and used the resulting two positions to create a directional vector sutable for use with Mesh.Intersect.
lightworker
lightworker
Yes I have found more examples like that

For me - I do not need direction because I am using ortho view so there is only one direction - I am interested only in one point (vector start position) so I do not have to normalize or test for intersection

Thanx for your help :)
Slaweklightworker@wp.pl

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.