Sign in to follow this  

Setting up DirectX for 2D

This topic is 4337 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

We are trying to develop a 2D game engine, but we are having trouble understanding how to correctly setup DirectX to run 2D (Lack of documentation in December 2005). We are writing the engine using Managed DirectX 2005 December editor using C#. First of all we are wondering how to initilize the view / projection matrices. What we want is a camera we can move around the playing field, and a projection which is independent of the z-buffer (OrtoLH). How would you normally in a 2D game handle coordinates, would you translate into some static game coordinates, say (0,0) top-left corner and (1600, 1200) bottom-right corner or would you use the normal (-1, 1) top-left corner, (0,0) center and (1,-1) bottom-right? How should we handle resolution changes, eg. do we need to change the backbuffer to fit the current window size or should it stay static throughout the duration of it's lifetime.

Share this post


Link to post
Share on other sites
Quote:
Original post by elurahu
We are trying to develop a 2D game engine, but we are having trouble understanding how to correctly setup DirectX to run 2D (Lack of documentation in December 2005).

We are writing the engine using Managed DirectX 2005 December editor using C#.

First of all we are wondering how to initilize the view / projection matrices. What we want is a camera we can move around the playing field, and a projection which is independent of the z-buffer (OrtoLH). How would you normally in a 2D game handle coordinates, would you translate into some static game coordinates, say (0,0) top-left corner and (1600, 1200) bottom-right corner or would you use the normal (-1, 1) top-left corner, (0,0) center and (1,-1) bottom-right?

You'll find two links in this thread which may help you.


Quote:
Original post by elurahu
How should we handle resolution changes, eg. do we need to change the backbuffer to fit the current window size or should it stay static throughout the duration of it's lifetime.

I think nothing special here, you handle resolution changes just like any D3D application, don't you?

kp

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
anything that you can do with linear algebra you can do with a matrix,
you all you need to do is write some linear equations to transform a point from world space to screen space and put them into a matrix.

example
v, the point to transform
s, scale(meters_to_pixels or whatever)
p, camera pos


a(v) = v*s
b(v) = v-p
c(v) = (v.x, -v.y)
d(v) = (v.x, v.y - res_height)

and your final transform (t) is
v = t(v) = a(b(c(d(v))))

in matrix form
I is the identity matrix

A is a scaling matrix
A=I * s

B is a translation matrix
start with I then B._41=-p.x, B._42=-p.y

C just multiplies y by -1
start with I then m._22=-1

D is another translation matrix
start with I then D._42=-res_height

to get the final matrix transform, matrix multiplication is the same as composition, so
T = A * B * C * D

I believe in 2d the projection matrix isn't used because your allready 2d.

(warning: I just wrote alot of stuff and some is prolly wrong, you really need to derive it yourself, there are tons of good guides on matrices on the net)


Another option is to use pretransformed vertices ( look for D3DFVF_XYZRHW )
or vertex shaders and transform you vertices there.

Share this post


Link to post
Share on other sites
Quote:
Original post by elurahu
So would I be wrong is saying I should use transformed vertrices for UI and positioned for game entities so that the camera affects the game entities but not the UI?

It's not the only way of doing it, but that would be a common/trivial way of doing it..

Jack

Share this post


Link to post
Share on other sites

This topic is 4337 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this