• Advertisement
Sign in to follow this  

2D Camera System

This topic is 2754 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

How do I setup a 3D camera for use with 2D? I attempted it, but it didn't look like it was working. Does D3DXMatrixLookAtLH work with D3DXSPRITEs? Really I just need a good tutorial on this (already googled everywhere).

This is what I have so far:


#ifndef _CAMERA_
#define _CAMERA_

#include <d3d9.h>
#include <d3dx9.h>
#include <vector>

using namespace std;

struct camera
D3DVIEWPORT9 viewport;
int width, height;


class cameraSystem
cameraSystem( LPDIRECT3DDEVICE9 device,int width, int height);

int create_camera( int width, int height);
void set_camera( int cam){ view_current = cam;}

void set_position( D3DXVECTOR3 &pos);
void set_zoom( float zoom) { view_zoom = zoom;}
D3DXMATRIX &get_View_matrix(){ return matView;}

inline int get_view_current(){ return view_current;}

void applyView();


D3DXMATRIX matPerspective;
D3DXMATRIX matWorld;

vector<camera*> camera_list;

int screen_width, screen_height;
int view_current;
float view_zoom;


#include "camera.h"

cameraSystem::cameraSystem( LPDIRECT3DDEVICE9 device,int width, int height):
//Setup orthographic projection matrix
D3DXMatrixOrthoLH (&matPerspective, screen_width, screen_height, 1.0f, 10.0f);
D3DXMatrixIdentity (&matWorld);
D3DXMatrixIdentity (&matView);
pDevice->SetTransform (D3DTS_PROJECTION, &matPerspective);
pDevice->SetTransform (D3DTS_WORLD, &matWorld);
pDevice->SetTransform (D3DTS_VIEW, &matView);


create_camera( width, height);

for (UINT i =0; i < camera_list.size(); ++i)
delete camera_list;


int cameraSystem::create_camera( int width, int height)
int i = camera_list.size();

camera *tmp = new camera();
tmp->viewport.X = 0;
tmp->viewport.Y = 0;
tmp->viewport.Width = width;
tmp->viewport.Height = height;
tmp->viewport.MinZ = 0.0f;
tmp->viewport.MaxZ = 1.0f;

tmp->width = width;
tmp->height = height;
tmp->pos = D3DXVECTOR3( 0.0f, 0.0f, 0.0f);

camera_list.push_back( tmp);
return i;

void cameraSystem::set_position( D3DXVECTOR3 &pos)
camera_list[view_current]->pos = pos;
//camera_list[view_current]->pos.z = view_zoom;
//D3DXMatrixLookAtLH( &matView, &camera_list[view_current]->pos, &camera_list[view_current]->pos, &D3DXVECTOR3( 0.0f, 1.0f, 0.0f));

D3DXMatrixTranslation( &matView, pos.x, pos.y, 0);

void cameraSystem::applyView()
//pDevice->SetTransform( D3DTS_WORLD, &matWorld);
pDevice->SetTransform( D3DTS_VIEW, &matView);

It doesn't seem to move anything. I use a bunch of D3DXSPRITEs, so is there something I have to set to make it work with those?

[Edited by - justin12343 on August 26, 2010 9:39:07 PM]

Share this post

Link to post
Share on other sites
It doesn't seem to move anything.

Are you trying to ask about something? What is "It" and what is it "It" should move?
Does D3DXMatrixLookAtLH work with D3DXSPRITEs?

Without seeing any of what you're trying to do with sprites - In general, D3DXSPITE is rendered to the viewport, so it isn't affected by your view or projection matrices. The position, etc., for a D3DXSPRITE is in screen coordinates.

If you need more help than that, you'll have to describe more specifically what you're having trouble with.

Share this post

Link to post
Share on other sites
My question is, How do I correctly setup an orthographic view for use with 2d? I'd rather do it this way than cheating by using D3DXSPRITE::SetTransform. If I use a orthographic view, do I now have to pass the D3DXSPRITE_OBJECTSPACE parameter for it to use the view matrix?

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement