GameDev.net Posting Guidelines (please read before posting)
For Beginners Forum FAQs (please read before posting)
Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.
Posted 20 April 2012 - 02:53 AM
Posted 20 April 2012 - 08:34 AM
D3DXMATRIX matView; // the view transform matrix static float MouseSpeed = 0.0075f; if (keystate[DIK_F1]) { MouseSpeed += 0.0001f; } else if (keystate[DIK_F2]) { if (MouseSpeed > 0.0001f) { MouseSpeed -= 0.0001f; } } static float AngleX = 0.0f; AngleX += mousestate.lX * MouseSpeed; static float AngleY = 0.0f; AngleY += mousestate.lY * MouseSpeed; static float LookX = 0.0f; static float LookY = 0.0f; static float LookZ = 0.0f; static float MoveX = 0.0f; static float MoveY = 0.0f; static float MoveZ = 0.0f; if(keystate[DIK_A] & 0x80) { MoveX -= 0.3f; } if(keystate[DIK_D] & 0x80) { MoveX += 0.3f; } if(keystate[DIK_S] & 0x80) { MoveY -= 0.3f; } if(keystate[DIK_W] & 0x80) { MoveY += 0.3f; } LookX = cos(AngleX) * sin(AngleY); LookY = sin(AngleY); LookZ = sin(AngleX) * sin(AngleY); D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3 (MoveX, MoveY, MoveZ), // the camera position &D3DXVECTOR3 (LookX + MoveX, LookY + MoveY, LookZ + MoveZ), // the look-at position + to go along with movement &D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); // the up direction d3ddev->SetTransform(D3DTS_VIEW, &matView); // set the view transform to matView D3DXMATRIX matProjection; // the projection transform matrix D3DXMatrixPerspectiveFovLH(&matProjection, D3DXToRadian(45), // the horizontal field of view (FLOAT)SCREEN_WIDTH / (FLOAT)SCREEN_HEIGHT, // aspect ratio 1.0f, // the near view-plane 1000.0f); // the far view-plane d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection); // set the projection // ----------------------------------------------------------- CAMERA SETUP <END> --------------------------------------------------------------------------// // select the vertex buffer to display d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX)); d3ddev->SetIndices(i_buffer); //select the index buffer D3DXMATRIX matTranslateA; // a matrix to store the translation for triangle A D3DXMATRIX matRotateX; // a matrix to store the rotation for each triangle D3DXMATRIX matRotateY; static float indexX = 0.0f; // index+=0.03f; static float indexY = 0.0f; if(keystate[DIK_LEFT] & 0x80) { indexY += 0.03f; } if(keystate[DIK_RIGHT] & 0x80) { indexY -= 0.03f; } if(keystate[DIK_DOWN] & 0x80) { indexX += 0.03f; } if(keystate[DIK_UP] & 0x80) { indexX -= 0.03f; } //building matrices D3DXMatrixTranslation(&matTranslateA, 0.0f, 0.0f, 15.0f); //15.0f away from the camera D3DXMatrixRotationX(&matRotateX, indexX); D3DXMatrixRotationY(&matRotateY, indexY); // tell Direct3D about each world transform, and then draw another triangle d3ddev->SetTransform(D3DTS_WORLD, &(matRotateX * matRotateY * matTranslateA)); d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12); //single primitive: d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
Posted 20 April 2012 - 04:43 PM
Really... that's just stupid. What a messed up convention. Anyway.Also, even tho it was a typo, Sin actually means arcussinus or sin^-1. (asked the math teacher and only one search result on google)
“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”
Posted 21 April 2012 - 03:11 AM
LookX = cos(AngleX) * sin(AngleY); LookY = sin(AngleX) * cos (AngleY); LookZ = sin(AngleX) * sin(AngleY);
Posted 22 April 2012 - 04:20 PM
You must be mistaken. http://en.wikipedia....ian_coordinates.Nevermind. Bacterius, your formula was wrong. The right formula is:
LookX = cos(AngleX) * sin(AngleY); LookY = sin(AngleX) * cos (AngleY); LookZ = sin(AngleX) * sin(AngleY);
This is where your logic is failing. The camera doesn't look at whatever the sin and cos gives you. It looks in the direction whatever the sin and cos gives you. To actually get the camera target for the D3D lookat function you need to add that direction to your camera's position to get another position corresponding to the target.So the object is at 0,0,15
The camera at 0,0,0
and looks at whatever the sin and cos gives me.
“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”
Posted 22 April 2012 - 07:56 PM
“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”
Posted 23 April 2012 - 02:15 AM
Posted 23 April 2012 - 02:41 AM
You should be able to set the initial view direction by setting the initial values for the angles. You can use the inverse of the spherical coordinates to obtain angles from an actual direction (which can be more convenient, say you want to look ahead of you in the Z direction when you first start your program, you use the inverse spherical coordinates on the (0, 0, 1) vector, and obtain the correct two angles).I am always at the mercy of what sin and cos gives me.
Sin and cos defines the point (excuse me, direction) where I look at when my program starts, I don't like that.
I want to set my starting view direction MYSELF.
You still seem to be stuck on this. What I'm showing you is for first-person perspective (I thought this is what you wanted). It's obviously a lot different for third-person perspective, because then it depends on how what perspective you actually want (in terms of what happens when you move the mouse, what when you zoom in/out, etc...). As for the mouse, this is just the interface between you (the player) and your ability to change the view angles (which allow you to change in which direction the camera is looking).Also, how am I supposed to make a camera follow a character and keep looking
at the direction of the character if I cannot define where I look at except for manually
following him with my mouse?
I actually did make a mistake in the formula, it should be (this explains why it didn't work on the Y-axis):Oh and... my formula is right.
With your formula, the camera's view doesn't even shift on the Y axis and does some weird stuff.
With the formula I defined earlier, everything works perfectly.
“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”
Posted 23 April 2012 - 05:46 AM
say you want to look ahead of you in the Z direction when you first start your program, you use the inverse spherical coordinates on the (0, 0, 1) vector, and obtain the correct two angles).
LookX = cos(AngleX) * sin(AngleY);
LookY = cos(AngleY);
LookZ = sin(AngleX) * sin(AngleY);
I am not sure what you are looking for anymore. What is your goal? I was under the impression you wanted to implement a first-person perspective camera, where you use the keyboard to move the camera around the world and the mouse to look around (much as you would in any 3D first-person game). Please correct me if I was wrong because this is what I'm working under.
Posted 24 April 2012 - 01:20 AM
You can get a view direction from some angles, and you can invert this process to get the appropriate angles for a view direction... or just set an initial direction that you like the look of?
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.
GameDev.net™, the GameDev.net logo, and GDNet™ are trademarks of GameDev.net, LLC.