# Turn camera round in circles

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

## Recommended Posts

ok been a long time since ive done math and trig at school and i cant for the life of me remember how to create a circle on a graph. what im want to do is make the camera spin around. heres a stand in code that does the trick so you know what im talking about but it isnt very good at all (NOTE dots are just for readability on the forum) im just after the math to do it properly so the radius stays the same from the x,z position float Xpos = 0.0f; //x position in 3D space float Zpos = 0.0f; //z position in 3D space float lookX = 0.0f; //look at x point float lookZ = 0.2f; //look at z point //Turn left if(KEY_DOWN(VK_LEFT)) { ...float dif = 0.2f; ...if(Xpos >= lookX && Zpos >= lookZ) ...{ ......lookX += dif; ......lookZ -= dif; ...} ...else if(Xpos <= lookX && Zpos >= lookZ) ...{ ......lookX += dif; ......lookZ += dif; ...} ...else if(Xpos <= lookX && Zpos <= lookZ) ...{ ......lookX -= dif; ......lookZ += dif; ...} ...else if(Xpos >= lookX && Zpos <= lookZ) ...{ ......lookX -= dif; ......lookZ -= dif; ...} }

##### Share on other sites
There is a link at the top right of the page. It is titled "faq" and provides a section about tags, besides others useful for posting code snippets w/o dotting. Please modify your OP using "source" tags.

From the textual description in the OP I understand that the camera should be fixed to its position, but it should rotate about the y axis. The shown code snippet does, well, something different. Since the key presses should rotate the camera, you cannot alter cartesian (i.e. straight rectangular) co-ordinates independently. Instead you should alter circular co-ordinates, i.e. an angle in this case. Then convert the angle to the cartesian co-ordinates.

So, if you have an angle α that is increased/decreased by key presses, convert it using e.g.
lookX = radius * sin( α )lookZ = radius * cos( α )

Choosing this dependeny means that the camera looks at z direction if the angle is 0, and rotates into the positive x if the angle starts increasing. Maybe you want to chose another initial direction. In that case play with exchanging sine and cosine and negate them (so you have 4 possibilities to choose from in total).

The rotation so far doesn't take the position of the camera into account. That yields in rotations not entirely about the camera's local axis as soon as Xpos and/or Zpos differ from 0. So you have to consider a translation, too:
lookX = radius * sin( α ) + XposlookZ = radius * cos( α ) + Zpos

The above should do what you look for (if I've understood the OP correctly).

BTW: Although I'm normally not a friend of the "search the internet" answer, I have to wonder why such general stuff like trigonometric functions isn't looked up there. E.g. the Trigonometric Functions article at wikipedia.com is just one possibility.

##### Share on other sites
ok it works but its rather glitchy, im pretty sure it has to do with the rest of my code for changing the x and z positions, pretty new to this so yea

//Function to move playervoid PlayerMovement(){	static float Xpos = 0.0f;	static float Ypos = 5.0f;	static float Zpos = 0.0f;	static float lookX = 1.0f;	static float lookY = 5.0f;	static float lookZ = 0.0f;	//Move forward	if(KEY_DOWN(VK_UP))	{		if(Xpos > lookX)		{			float dif = (Xpos - lookX);			Xpos -= dif;			lookX -= dif;		}		else if(Xpos < lookX)		{			float dif = (lookX - Xpos);			Xpos += dif;			lookX += dif;		}		if(Zpos > lookZ)		{			float dif = (Zpos - lookZ);			Zpos -= dif;			lookZ -= dif;		}		else if(Zpos < lookZ)		{			float dif = (lookZ - Zpos);			Zpos += dif;			lookZ += dif;		}	}	//Move back	if(KEY_DOWN(VK_DOWN))	{		if(Xpos > lookX)		{			float dif = (Xpos - lookX);			Xpos += dif;			lookX += dif;		}		else if(Xpos < lookX)		{			float dif = (lookX - Xpos);			Xpos -= dif;			lookX -= dif;		}		if(Zpos > lookZ)		{			float dif = (Zpos - lookZ);			Zpos += dif;			lookZ += dif;		}		else if(Zpos < lookZ)		{			float dif = (lookZ - Zpos);			Zpos -= dif;			lookZ -= dif;		}	}	//Turn left	if(KEY_DOWN(VK_LEFT))	{		static float index = 0.0f;		index -= 0.05f;		lookX = 1.0f * sinf(index) + Xpos;		lookZ = 1.0f * cosf(index) + Zpos;	}	//Turn right	if(KEY_DOWN(VK_RIGHT))	{		static float index = 0.0f;		index += 0.05f;		lookX = 1.0f * sinf(index) + Xpos;		lookZ = 1.0f * cosf(index) + Zpos;	}	PlayerPosition(Xpos, Ypos, Zpos, lookX, lookY, lookZ);}

##### Share on other sites
never mind, figured it out, just had to declare index for both turn left and right :)