2D Camera System

Started by
2 comments, last by justin12343 13 years, 7 months ago
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:

camera.h
#ifndef _CAMERA_#define _CAMERA_#include <d3d9.h>#include <d3dx9.h>#include <vector>using namespace std;struct camera{	D3DVIEWPORT9 viewport;	D3DXVECTOR3 pos;	int width, height;};class cameraSystem{public:	cameraSystem( LPDIRECT3DDEVICE9 device,int width, int height);	~cameraSystem();	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();private:	LPDIRECT3DDEVICE9 pDevice;	D3DXMATRIX matPerspective;	D3DXMATRIX matView;	D3DXMATRIX matWorld;	vector<camera*> camera_list;	int screen_width, screen_height;	int view_current;	float view_zoom;};#endif


camera.cpp
#include "camera.h"cameraSystem::cameraSystem( LPDIRECT3DDEVICE9 device,int width, int height):	pDevice(device),	screen_width(width),	screen_height(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);	view_current=0;	view_zoom=0;	create_camera( width, height);}cameraSystem::~cameraSystem(){	for (UINT i =0; i < camera_list.size(); ++i)	{		delete camera_list;	}	camera_list.clear();}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]
Advertisement
**BUMP** Added some source code. 8P
Quote:It doesn't seem to move anything.

Are you trying to ask about something? What is "It" and what is it "It" should move?
Quote: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.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

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?

This topic is closed to new replies.

Advertisement