Archived

This topic is now archived and is closed to further replies.

Biggles

What weird coordinate system is this?

Recommended Posts

I''m having a bit of trouble with doing some simple 2D in Direct3D 8. I''ve got a system set up that uses the absolute position of the mouse to draw a small sqaure on the screen. When you move the mouse, the square moves. This is a simple cursor system. I''ve used an orthogonal matrix for the 2D like the article on this site about doing 2D in Direct3D said, but the coordinates don''t seem to work well at all. For example, the y-axis is reversed from what I thought it would be. Higher values make the box go up, not down. I also have a function that should restrict the box to the area inside the screen, but it still goes off the edge as if the edge of the screen isn''t where I thought it would be. About the only thing I''m think I know is that the centre of the screen is (0,0), but I''m not even sure about that. Here''s some code:
  	D3DXMATRIX Ortho2D;
	D3DXMATRIX Position;
	D3DXMATRIX Identity;

	D3DXMatrixOrthoLH (&Ortho2D, (float) width, (float) height, 0.0f, 1.0f);
	D3DXMatrixTranslation (&Position, absPosition.x, absPosition.y, 0.0f);
	D3DXMatrixIdentity (&Identity);

	pDevice->SetTransform (D3DTS_PROJECTION, &Ortho2D);
	pDevice->SetTransform (D3DTS_WORLD, &Position);
	pDevice->SetTransform (D3DTS_VIEW, &Identity);

	pDevice->SetVertexShader (D3DFVF_PANELVERTEX);
	pDevice->SetStreamSource (0, pVB, sizeof (PanelVertex));
	pDevice->DrawPrimitive (D3DPT_TRIANGLEFAN, 0, 2);  
Could someone expplain how the coordinate system in 2D works? Thanks. -------------------- Never eat anything bigger than your own head.

Share this post


Link to post
Share on other sites
The coordinates system ranges from -1 to 1 on the x axis and from 1 to -1 on the y axis. Thus, when you increase y, the box will go up for sure. Anyway, you can simply flip this behavior by manually changing the projection matrix. After creating the ortho matrix, you can add the following line of code:
Ortho2D._22 = -Ortho2D._22;
I haven''t tried it, but I think it will also work if you give the height value in negative when you call D3DXMatrixOrthoLH.

Share this post


Link to post
Share on other sites
So the coordinate system works in sortof percentages, rather than actual pixels? I guess that makes sense, since that way it''s independant of resolution. I''ll go fix up my code now, thanks.


--------------------

Never eat anything bigger than your own head.

Share this post


Link to post
Share on other sites
I tried constraining the mouse cursor to between -1 and 1 on both axes, and now it just moves in a little 2 by 2 area in the centre of the screen.
According to the article "2D Rendering in DirectX 8" on this site:

quote:
We are now set up for 2D drawing, now we need something to draw. The way things are set up, our drawing area goes from -WindowWidth to WindowWidth and -WindowHeight to WindowHeight. One thing to note, in this code, the width and the height are being specified in pixels. This allows us to think about everything in terms of pixels, but we could have set the width and height to say 1.0 and that would have allowed us to specify sizes, etc. in terms of percentages of the screen space, which would be nice for supporting multiple resolutions easily. Changing the matrix allows for all sorts of neat things, but for simplicity, we''ll talk about pixels for now…


I followed the example code in that article to do what I did. Why does the mouse cursor still go off the edge of the screen? Should I be specifying half the window width and height in my call to D3DXMatrixOrthoLH()?


--------------------

Never eat anything bigger than your own head.

Share this post


Link to post
Share on other sites
I''ve gone through and checked the values coming out of the mouse (I''m using DirectInput to do the mouse movements) and the values I''m getting are very odd. A small movement of the mouse gives a relative position vector with values in the order of magnitude of ~10^7. This seems extraordinarily large for a small movement of the mouse.


--------------------

Never eat anything bigger than your own head.

Share this post


Link to post
Share on other sites
I haven''t used DirectInput before. To check whether the error is happening in the drawing code or the input code, I suggest you replace the DirectInput code temporarily by the Win32 function call GetCursorPos. If this works out perfectly, then you have to go and check your DirectInput code, otherwise, there is something wrong in the drawing code.

Share this post


Link to post
Share on other sites
For the strange values with the mouse, this just requires a simple cast of dwData member of the mouse object data structure from a double word to an int. This makes sense because a double word is always positive, while you need positive and negative values.

As for the coordinate systems, I haven''t read the tutorial you are talking about but I can give some general tips regarding this. It all basically has to do with the pipeline. If you want to use a coordinate system where the values are specified in percentages (like the -1 to 1 one you mentioned), then you specify your objects vertex coordinates using that. (.5, .5, .5) for one corner for example. Then when the object is passed through the projection matrix, it is "projected" on to the screen, i.e. converted into actual pixel values. However if you want to position your objects using only pixel values, get the size of the window using D3DVIEWPORT and GetViewport, enter in the pixel values you want (0,0 = top left, y increases downward, x increases to the right), and do not use a projection matrix, and there you go. You might not need to use a world or view matrix either depending on what you are doing. Hope that clears it up somewhat. I haven''t done this in ages but I had similar confusions when I was programming a few months ago.

GL,
Tom Rhodes

Share this post


Link to post
Share on other sites
Thanks for your help. I think I''m getting the hang of it now. It''s just taking a bit of adjusting after having done so much in 3D.


--------------------

Never eat anything bigger than your own head.

Share this post


Link to post
Share on other sites