# Where to you add the change in position due to camera movement?

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

## Recommended Posts

Hallo, I was wondering how people changed the position of game sprites when it comes to rendering. If you have a scrolling screen you need to move the sprites each frame to take into account the scrolling. How are you doing this? A few ways I can come up with: 1. When rendering use spritePos + cameraPos 2. Use spritePos += change in cameraPos 3. Something else? Regards,

##### Share on other sites
Usually the camera is a separate entity that moves independently of objects in the game world. When calculating the rendering position for a simple 2D game you would use something like spritePos - cameraPos, however when the camera moves all you change is 'cameraPos'. The objects maintain their world position, but the math works out so it appears the objects are moving relative to the camera (since the result of the subtraction changes).

##### Share on other sites
I agree with Zipster; it's lots easier -- your game objects don't have to depend on the camera object's position.

Here's how I do mine:

typedef Vector2<double> v2; // or whatever vector you're usingclass camera{public:	v2	m_axes[2];				// x and y axes as represented by rotation and scale	v2	m_viewport_center;			// center of screen	v2	m_origin;				// origin of camera in screen coordinates	double	m_scale;				// 1 world cordinate is this many pixels on the screen 	double	m_rotation;				// rotation of camera, in radians	public:	cammera() : 		m_rotation( 0.0 ), 		m_scale( 32.0 ), 		m_origin( 0.0, 0.0 ), 		m_viewport_center( 0.0, 0.0 ),	{		update();	}		~camera() { }		void update()	{		m_axes[0] = v2( cos( m_rotation ), sin( m_rotation ) ) * m_scale; // Here we set the x-axis to be the x-component of a rotated vector (length of scale -- one unit = "scale" pixels)		m_axes[1] = m_axes[0].perpendicular(); // get the y-component of the rotated vector	}		v2 project( const v2 &p ) const	{		// Here's where the magic happens:		return ( (p.x - m_origin.x) * m_axes[0] + 			 (p.y - m_origin.y) * m_axes[1] ).inv_y() +  // where inv_y() returns v2( x, -y )			m_viewport_center;	}};class player{private:	wtf_lol_sprite	*m_my_sprite;  // Or whatever drawing method you're using		v2		m_my_position;	double		m_my_rotation;	/* ... */	public:	/* ... */		void draw( const camera &cam_ref )	{		v2 projected_position = cam_ref.project( m_my_pos );				m_my_sprite->render_ex( 			projected_position.x,  	//	x position on screen for sprite			projected_position.y,	//	y position on screen for sprite			cam_ref.m_scale * 3.0 * (1.0/256.0), 	//	scale of sprite (player's size is 3 units, and the sprite is 256 pixels in size )			m_my_rotation -cam_ref.m_rotation );	//	rotation of sprite	}};

(You could use a global camera object, instead of passing a camera to every class's draw method)

[Edited by - _fastcall on August 20, 2008 1:30:15 AM]

1. 1
2. 2
Rutin
20
3. 3
khawk
18
4. 4
A4L
14
5. 5

• 12
• 16
• 26
• 10
• 44
• ### Forum Statistics

• Total Topics
633760
• Total Posts
3013723
×