Sign in to follow this  
kiwidrifter

Turn camera round in circles

Recommended Posts

kiwidrifter    100
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 this post


Link to post
Share on other sites
haegarr    7372
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( α ) + Xpos
lookZ = 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 this post


Link to post
Share on other sites
kiwidrifter    100
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 player
void 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 this post


Link to post
Share on other sites

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