Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


Converting the mouse position to


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 ThePointingMan   Members   -  Reputation: 196

Like
0Likes
Like

Posted 04 April 2013 - 12:00 AM

I'm in the process of making a 2d game, and have made a transition from using d3d's 2D scene to just using 3d quads, however this is giving me a bit of issues with mouse control. While yes the mouse position is indeed moving in the direction I am moving my mouse physically, it is moving at a different rate. I am wandering how one would deal with this? my screen is 800/600 and my view transform matrix is 600 units away on the z axis, looking at everything from a distance.



Sponsor:

#2 Steve_Segreto   Crossbones+   -  Reputation: 1538

Like
2Likes
Like

Posted 04 April 2013 - 01:11 AM

If you mean that you went from using a 2-d orthographic projection matrix to using a 3-d perspective projection matrix, then you are going to have to think in terms of a view frustum. Because the virtual world your camera is looking at is now 3-d dimensional along the z-axis, the width of the horizon is now specified in world coordinates, not simply 0..599 like in 2-d.

 

Can you give more details about your projection matrix and what you are trying to accomplish?



#3 slicer4ever   Crossbones+   -  Reputation: 3942

Like
1Likes
Like

Posted 04 April 2013 - 01:39 AM

I'm in the process of making a 2d game, and have made a transition from using d3d's 2D scene to just using 3d quads, however this is giving me a bit of issues with mouse control. While yes the mouse position is indeed moving in the direction I am moving my mouse physically, it is moving at a different rate. I am wandering how one would deal with this? my screen is 800/600 and my view transform matrix is 600 units away on the z axis, looking at everything from a distance.

As Steve said, if you are using a perspective matrix, you need to translate your screen coordinates to world coordinates. look up UnProject/Project functions to accomplish this for generic perspective/view matrixs.

Edited by slicer4ever, 04 April 2013 - 01:41 AM.

Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

#4 ThePointingMan   Members   -  Reputation: 196

Like
0Likes
Like

Posted 04 April 2013 - 01:46 AM


If you mean that you went from using a 2-d orthographic projection matrix to using a 3-d perspective projection matrix, then you are going to have to think in terms of a view frustum. Because the virtual world your camera is looking at is now 3-d dimensional along the z-axis, the width of the horizon is now specified in world coordinates, not simply 0..599 like in 2-d.

 

Can you give more details about your projection matrix and what you are trying to accomplish?

1So these are the Matrices I have set up for my camera.

 D3DXMatrixLookAtLH(&mCamera,	
    &D3DXVECTOR3 (0, 0, 600),    // the camera position
    &D3DXVECTOR3 (0, 0, 0.0f),      // the look-at position
    &D3DXVECTOR3 (0.0f, -1.0f, 0.0f));    // the up direction

	D3DXMatrixPerspectiveFovLH(&mLense,
                               D3DXToRadian(45),//Field of view
                               _w / _h,//dimensions or somethin
                               1.0f,    // the near view-plane
                               1000.0f);    // the far view-plane
// set the view transform
d3ddev->SetTransform(D3DTS_VIEW, &(camera->GetCamera()));
 
    // set the projection transform
d3ddev->SetTransform(D3DTS_PROJECTION, &(camera->GetLense()));
 

What I'm doing is creating a level editing program for a game I am making, there is a grid on the screen and I wish to make it so that when I click on one of the tiles it changes to the type of tile i have selected. I have just been using the getcursorpos() and screentoclient() to find the mouse position, but since this isn't a flat 2d program those co-ordinates aren't lining up with what I see on screen.


Edited by ThePointingMan, 04 April 2013 - 01:47 AM.


#5 slicer4ever   Crossbones+   -  Reputation: 3942

Like
1Likes
Like

Posted 04 April 2013 - 02:04 AM

ThePointingMan, on 04 Apr 2013 - 03:50, said:

Steve_Segreto, on 04 Apr 2013 - 03:15, said:
If you mean that you went from using a 2-d orthographic projection matrix to using a 3-d perspective projection matrix, then you are going to have to think in terms of a view frustum. Because the virtual world your camera is looking at is now 3-d dimensional along the z-axis, the width of the horizon is now specified in world coordinates, not simply 0..599 like in 2-d.

Can you give more details about your projection matrix and what you are trying to accomplish?

1So these are the Matrices I have set up for my camera.
 D3DXMatrixLookAtLH(&mCamera,	
    &D3DXVECTOR3 (0, 0, 600),    // the camera position
    &D3DXVECTOR3 (0, 0, 0.0f),      // the look-at position
    &D3DXVECTOR3 (0.0f, -1.0f, 0.0f));    // the up direction

	D3DXMatrixPerspectiveFovLH(&mLense,
                               D3DXToRadian(45),//Field of view
                               _w / _h,//dimensions or somethin
                               1.0f,    // the near view-plane
                               1000.0f);    // the far view-plane
// set the view transform
d3ddev->SetTransform(D3DTS_VIEW, &(camera->GetCamera()));

// set the projection transform
d3ddev->SetTransform(D3DTS_PROJECTION, &(camera->GetLense()));

What I'm doing is creating a level editing program for a game I am making, there is a grid on the screen and I wish to make it so that when I click on one of the tiles it changes to the type of tile i have selected. I have just been using the getcursorpos() and screentoclient() to find the mouse position, but since this isn't a flat 2d program those co-ordinates aren't lining up with what I see on screen.

I don't know the directX naming conventions for Project/UnProject(they might not exist, and you'll have to implement them yourself). but basically this is what i'd do in pseudo-code:
  Matrix View = GetViewMatrix(); //Get our view matrix(your lookat function might return this, or it might directly set the matrix to the gpu?)
  Matrix Perspective = GetPerspectiveMatrix(); //Get our perspective matrix.
  Vector2 MouseScreen = GetMouseScreenCoordinates(); //Get the screen coordinates that are relative to the window.
  Vector3 WorldNear = UnProject(MouseScreen.X, MouseScreen.Y, 0.0f, View, Perspective); //We get the world coordinates that are at the camera's near plane.
  Vector3 WorldFar = UnProject(MouseScreen.X, MouseScreen.Y, 1.0f, View, Perspective); //We get the world coordinates that are at the camera's far plane.
  //Since the game tile's world position z-depth is at 0(note that the camera is at 600, but the object's are at 0 on the z-axis(or so i assume anyway)).
  //We basically have a ray from the front of the camera, to the back of the camera, now we just need to get the point on the plane that the game is on:
  Vector3 WorldDir = WorldFar-WorldNear;
  float D = WorldNear.Z/WorldDir.Z; //since our plane is along the z axis, we can assume that the A and B components of the plane are 0, and the C component is 1. and that the Distance of the plane is also 0, which simplifies our calculations. down to the near/dir. (see: [url="http://www.yaldex.com/game-programming/0131020099_ch22lev1sec2.html"]http://www.yaldex.com/game-programming/0131020099_ch22lev1sec2.html[/url]
  Vector3 WorldPoint = WorldNear+WorldDir*D; //you can take the x/y position and treat these values the same way you did when the game was 2D.  You don't need the z value unless your game world has actual depth.
And that's basically the gist of it.
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

#6 Vortez   Crossbones+   -  Reputation: 2704

Like
-6Likes
Like

Posted 04 April 2013 - 02:13 AM

I have the code to do this using gluUnproject, but apparently, im too noob to help so, too bad...



#7 slicer4ever   Crossbones+   -  Reputation: 3942

Like
4Likes
Like

Posted 04 April 2013 - 02:30 AM

I have the code to do this using gluUnproject, but apparently, im too noob to help so, too bad...

Three things. (and why i down-voted your post):
1. You can't actually delete your posts, their's this handy thing called history, and everything you've done/said is still quite accesible.
2. Getting angry about being down-voted is completely immature, accept it, and move on. You yourself escalated the situation when all people did was give you reasons not to use macros, and everyone was very polite about it.
3. Taking out your frustration to other threads is even more immature.

So your options are to either leave gamedev because you can't handle the community, or to learn to accept criticism when someone disagrees with how you do things. Noone is attacking you directly, they are simply saying their are better ways to do something.

Edited by slicer4ever, 04 April 2013 - 02:32 AM.

Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

#8 Endurion   Crossbones+   -  Reputation: 3644

Like
2Likes
Like

Posted 04 April 2013 - 03:37 AM

The functions to project/unproject exist (at least up to D3DX9), they're called D3DXVec3Project and D3DXVec3Unproject.


Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

#9 Poigahn   Crossbones+   -  Reputation: 519

Like
0Likes
Like

Posted 04 April 2013 - 04:33 AM

I do not know if this would help, I do not use this language, But when I did the same thing in the language I use,  I had to relocate the call to the function to draw the mouse to a different section of the Draw/Renderworld Loop.

 

  Where as I had the mouse draw prior to updating the world, I had to move it to after the update/render world code and then do a page flip.

 

This solved my problem as it was much like yours.

 

Again!!  I stress that I am using a different language!!


Your Brain contains the Best Program Ever Written : Manage Your Data Wisely !!


#10 ThePointingMan   Members   -  Reputation: 196

Like
1Likes
Like

Posted 04 April 2013 - 05:40 PM

Firstly, thankyou very much Slice4Ever andEndurion, you two have been a great help. I'm still having a wee bit of trouble with the Unproject function though. D3d's unproject looks like this

 

 

D3DXVECTOR3* D3DXVec3Unproject(
_Inout_  D3DXVECTOR3 *pOut,
_In_     const D3DXVECTOR3 *pV,
_In_     const D3D10_VIEWPORT *pViewport,
_In_     const D3DXMATRIX *pProjection,
_In_     const D3DXMATRIX *pView,
_In_     const D3DXMATRIX *pWorld
);
 

As far as I can tell I should be putting the vector holding the mouse co-ords for pV,

the projection matrix and view matrix in their places,

but I'm unsure as to what I should be putting in the ViewPort section and pWorld. Honestly I don't even fully understand what a view port is!

 

Edit: Alright, funny that trying to setup the mouse would teach me so much about the camera, at one point I ended up accidentally turning my level editor into a first person shooter! Minus the bullets of course :/ I understand what the view port is now, all is good! Thank you all for your help! For others who may come across this issue, here is something that helped me out a bit. an example of what this should look like using directx's functions http://gamedev.stackexchange.com/questions/44364/zooming-into-mouse-position and MSDN's page on viewports http://msdn.microsoft.com/en-ca/library/windows/desktop/bb206341(v=vs.85).aspx

 

Edit*2: I have noticed one slight error though, I remember dealing with this with my last project but I don't quite remember how. The WS style SYSMENU includes a little bar at the top of the screen, and this is interfering with my mouse position, does anyone know of any ways to work around this?


Edited by ThePointingMan, 05 April 2013 - 12:59 AM.


#11 SlamDrag   Members   -  Reputation: 111

Like
0Likes
Like

Posted 05 April 2013 - 08:20 AM

I do not know if this would help, I do not use this language, But when I did the same thing in the language I use,  I had to relocate the call to the function to draw the mouse to a different section of the Draw/Renderworld Loop.

 

  Where as I had the mouse draw prior to updating the world, I had to move it to after the update/render world code and then do a page flip.

 

This solved my problem as it was much like yours.

 

Again!!  I stress that I am using a different language!!

Language shouldn't matter, implementation does.



#12 slicer4ever   Crossbones+   -  Reputation: 3942

Like
1Likes
Like

Posted 05 April 2013 - 09:47 AM

ThePointingMan, on 04 Apr 2013 - 19:44, said:
Firstly, thankyou very much Slice4Ever andEndurion, you two have been a great help. I'm still having a wee bit of trouble with the Unproject function though. D3d's unproject looks like this


D3DXVECTOR3* D3DXVec3Unproject(
_Inout_  D3DXVECTOR3 *pOut,
_In_	 const D3DXVECTOR3 *pV,
_In_	 const D3D10_VIEWPORT *pViewport,
_In_	 const D3DXMATRIX *pProjection,
_In_	 const D3DXMATRIX *pView,
_In_	 const D3DXMATRIX *pWorld
);
As far as I can tell I should be putting the vector holding the mouse co-ords for pV,
the projection matrix and view matrix in their places,
but I'm unsure as to what I should be putting in the ViewPort section and pWorld. Honestly I don't even fully understand what a view port is!

Edit: Alright, funny that trying to setup the mouse would teach me so much about the camera, at one point I ended up accidentally turning my level editor into a first person shooter! Minus the bullets of course :/ I understand what the view port is now, all is good! Thank you all for your help! For others who may come across this issue, here is something that helped me out a bit. an example of what this should look like using directx's functions http://gamedev.stackexchange.com/questions/44364/zooming-into-mouse-position and MSDN's page on viewports http://msdn.microsoft.com/en-ca/library/windows/desktop/bb206341(v=vs.85).aspx

Edit*2: I have noticed one slight error though, I remember dealing with this with my last project but I don't quite remember how. The WS style SYSMENU includes a little bar at the top of the screen, and this is interfering with my mouse position, does anyone know of any ways to work around this?

In old openGL(i.e. 1.1/2.0) the view matrix was called the modelview matrix, which combines both the model matrix, and the view matrix. from my understanding of that function, it expects the view matrix(your camera matrix) is separate from the model(or world) matrix.

the viewport(as you've discovered), is simply the actual window rectangle that your world is drawn into, it's useful for making split-screen games, and for rendering to small framebuffers for post-processing effects.

as for your last edit, in my implementation of reading mouse input, i read the mouse messages passed from windows. Which takes care of making sure the mouse is actually the top left of the drawable window. however if i remember correctly(and it's been awhile since i've had to do it on windows) you needed to get the window client size, and subtract that from the total window size to get the correct offset? But you'll have to do a bit more googling as I don't remember the exact implementation.
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS