• ### Announcements

#### Archived

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

# rendering 3D objects in 2D space

## Recommended Posts

How could I get an normal 3D-object to show up on an specific screen position (e.g. centered on coordinate 130,220)? The scaling of the object is not that important...

##### Share on other sites
Erm.. i''m not sure if this is what you are looking for, but you could always do one of the following:

Pre-Render your object and simply put it as a sprite in your scene.

or

draw a sprite behind your object (D3DXSprite i believe), then draw your object just like normal.

To be honest with you, I don''t have a clue as to what you are asking, because this seems obvious so I doubt it is what yo uare looking for.

##### Share on other sites
hehe, I know how to render 3D objects, but 3D objects are normaly translated in 3D space (x,y,z), but I need to set the position of the 3D-object by just using the x and y coordinate on my screen. This is important for stuff like collision detection with the 2D-world...

##### Share on other sites
if scaling is not important and you just need your 3d object to be on point (a,b) can''t you just drop the z value to 0 or some other constant and set its position as (a,b,constant_z) -- for all objects -- i guess i am kind of confused by your question.

##### Share on other sites
you need to use an orthogonal projection matrix (look it up in the docs). When you set the location of the object the z component will be meaningless, and the x,y components will represent a coordinate pair with the origin at the center of the screen. If you do this you will have to scale your objects before you render them, since you can''t move your object along the z axis, to change their size.

##### Share on other sites
Another cute method is to make them RHW (can''t remember the acronym off the top of my head). Basically set them to be of type
(D3DFVF_XYZRHW| ALLyourOTHERflags) rather than type (D3DFVF_XYZ| ALLyourOTHERflags). Then you HAVE to do all the transforms yourself, the coordinates for that object will be from 0 to screenWidth and 0 to screenHeight starting at the upper left corner. If you ever did any programming back in the days before nifty DX Matricies you shouldn''t have any problems here. You also have another FLOAT to add onto your vertex buffer ie. 10 x, 20 y, -30 z is the vertex: {10.0f, 20.0f, -30.0f, 1.0f}. The extra 1.0f at the end is the RHW transform, you can use this to scale your object in some pretty cool ways. I use this for overlays and such because I like to work in screen coordinates rather than world coordinates. The downside is the math involved if you want to rotate or scale your object, fortunatly there is a whole set of D3DXMatrix stuff you can apply directly to the buffer (rather than the world view).

Brett Lynnes
cheez_keeper@hotmail.com

##### Share on other sites
how could I use an ortho view matrix? It looks very odd...

##### Share on other sites
You can render things in "Camera space" using the inverse view matrix as your initial transform. This is one way of rendering objects that always stay in front of the camera. e.g.

matInvView is the inverse view matrix (doh!)
matTrans is the transformation of the object in camera space. Without this the object will be rendered at the camera position and probably fall behind (at least partially) the front clipping plane. Try a simple matrix at first with nothing but a positive Z translation then the object will remain in the centre of the screen no matter where the camera points.
matFinal is the final world transform to apply to the object.

matTrans and matInvView will need multiplying together..
D3DXMatrixMultiply( &matFinal, &matTrans, &matInvView );

If you want an object to appear at a specific X,Y on the screen you are going to have to consider the FOV and the Z depth of the object to calculate matTrans.

Hope that helps.

Regards

Matt

##### Share on other sites
I don''t understand

What about some real code or an tutorial? :/

##### Share on other sites
It''s probably simpler than I made it sound..

D3DXMATRIX matInvView, matTrans, matFinal;// keep your own view matrix: matView// inverse it..D3DXMatrixInverse( &matInvView, NULL,  &matView );// make a translation/rotation matrix for the// object in camera spaceD3DXMatrixIdentity( &matTrans );// translate 50 units in Z (into the screen)..matTrans._43 = 50.0f;// multiply the inverse view and the translation matrices..D3DXMatrixMultiply( &matFinal, &matTrans, &matInvView );// set the world transform..m_lpD3DDevice->SetTransform( D3DTS_WORLD, matFinal );// now render your object here...

I''ve just written that from memory but it''s fairly straightforward. Now, if you want to arrive at a particular screen X & Y you''ll need to calculate your matTrans differently. The X position will be relative to the angle between the object and the camera''s Z axis in the XZ plane, the Y will be similar except it''s working in the YZ plane. Remember also that your view matrix maps (X0,Y0) to the centre of the screen. I''ve never been interested in placing an object at a particular X,Y- normally just in the centre of the screen as in the above example, but it''s just two more 2d trig calculations away and the above example should be a starting point for your own experimentation.

Good luck

- Matt