• Any C++/DirectX engine programmers on this board?
    47 replies, posted
Just wondering if there is any experienced programmers that have worked on directx programming that would be able to help with some questions i have?
Why don't you just post your questions so you can have multiple people answer them?
Ok , well you know in fps games the model of the gun moves with the camera, not just forward and back , but rotated with the camera to , i thought that the camera's look vector would be the direction the model would move in , but it seems its not. Does anyone know how todo the maths make a gun model move with the camera view? I currently have a camera that walks on a terrain if thats any help..
Wouldn't it just be something like this? [cpp]weaponPos = cameraPos + cameraDir * 8 - Vector( 0, 0, 10 ) weaponDir = cameraDir[/cpp]
D3DXMatrixTranslation only takes 1 D3DXVECTOR3, so how would you use 2 vector's? [editline]04:26PM[/editline] basically i had something like Camera->pos.x + Camera->Look.x + Camera->pos.y + Camera->Look.y + Camera->pos.z + Camera->Look.z with an offset of 3 to bring it infront of the camera , but i dont get the desired effect i want.. [editline]04:35PM[/editline] heres a screenshot of what i have so far, the red box is just an example object before i woud add a proper model etc, [img]http://img843.imageshack.us/img843/363/cambox.jpg[/img] [editline]04:52PM[/editline] [quote] Wouldn't it just be something like this? [URL="http://www.facepunch.com/#"]view plain[/URL][URL="http://www.facepunch.com/#"]copy to clipboard[/URL][URL="http://www.facepunch.com/#"]print[/URL][URL="http://www.facepunch.com/#"]?[/URL] [LIST=1] [*]weaponPos = cameraPos + cameraDir * 8 - Vector( 0, 0, 10 ) ; [/LIST] [/quote] when i do that the box moves away from the camera, so i changed 8 to 4 and when its close , it dosent seem to turn with the camera..
Use D3DXMatrixRotationAxis for rotation.
I'm a GL programmer, but I think this applies to DirectX as well. Can't you just skip the world/view matrix on the gun so that it just stays in eye coordinate space? If you go change its position so that it follows the camera, the world and view matrices basically just undo each other, so you can just eliminate them. It seems a whole lot simpler than updating it's position and orientation every game frame. I think most games render it last with depth testing off, at a different FOV so that it doesn't look all distorted.
I am thinking of use D3DXVec3TransformCoord on the View Matrix then that returned vector would be the direction... [editline]05:22PM[/editline] [QUOTE=ROBO_DONUT;24667045]I'm a GL programmer, but I think this applies to DirectX as well. Can't you just skip the world/view matrix on the gun so that it just stays in screen space? It seems a whole lot simpler than updating it's position and orientation every game frame. I think most games render it last with depth testing off, at a different FOV so that it doesn't look all distorted.[/QUOTE] I am not sure how you render models in screenspace :<
[QUOTE=Anddos;24667054]I am not sure how you render models in screenspace :<[/QUOTE] It's just like world space, except that the camera is right in the middle, looking in the direction of the positive or negative Z-axis. At least that's the convention in OpenGL. It might be the X or Y axis in Direct3D. Transformations go: local object coordinates -> [model matrix] -> world space -> [view matrix] -> eye coordinate space -> [projection matrix] -> clip coordinates
yep thats the same for directx , for the red shader its boxworld*view*projection = wvp , so would that be screenspace?
[QUOTE=Anddos;24667173]yep thats the same for directx[/QUOTE] Right, so set the world/view matrixes to identity (or skip them altogether if you can) and render directly to eye coordinate space. If the gun isn't positioned/oriented correctly, just create a simple model/view matrix with only a small offset in eye coords. Leave the player/camera position/orientation stuff out entirely.
these are already set in the camera class D3DXMatrixIdentity(&mView); D3DXMatrixIdentity(&mProj); D3DXMatrixIdentity(&mViewProj); i know what screenspace is , its x,y width and height and projection space is like near and far plane, i dont understand how to render a model in screenspace as thats the final transform i am doing with world*view*projection for the red shader :>
[QUOTE=Anddos;24667266]these are already set in the camera class D3DXMatrixIdentity(&mView); D3DXMatrixIdentity(&mProj); D3DXMatrixIdentity(&mViewProj); i know what screenspace is , its x,y width and height and projection space is like near and far plane, i dont understand how to render a model in screenspace as thats the final transform i am doing with world*view*projection for the red shader :>[/QUOTE] :doh: Sorry, I didn't mean screen space. I meant eye coordinates. Wrong words, my bad.
ok what is eye coordinates , gCamera->Pos?
[QUOTE=Anddos;24667355]ok what is eye coordinates , gCamera->Pos?[/QUOTE] I'm finding it difficult to explain without re-iterating what I've already said. Object coordinates are what are stored in the model file. When you load the model in Blender or 3DS Max or whatever, the center of the grid is the origin (0, 0, 0) in object space. To position the object in the world, you multiply by a model matrix. Suddenly the origin of the model, which was originally at (0, 0, 0) is now at (model.x, model.y, model.z). I'm ignoring rotations here for simplicity. Now we need to move the the object to the camera. Notice how I worded this. You are not moving the camera about in the world, you are moving everything in the world around the camera. This is what the view matrix is for. Transforming the model's origin by the view matrix results in (model.x - player.x, model.y - player.y, model.z - player.z). This is in eye coordinates. So you want to have the model follow the player? This is clearly accomplished by setting the model's position to the player's position (plus some offset, if necessary). Substituting player for model (because model = player now), the model's origin in eye coords is: (player.x - player.x, player.y - player.y, player.z - player.z) or (0, 0, 0) And we're right back where we started. The model and view matrix transformations cancel out, so we can skip them and use object/model coordinates as eye coordinates. In practice, the model probably won't be just quite where you want it (but it will follow the camera), so you can apply a minimal model or view matrix with a fixed offset from the camera's origin.
[quote] Object coordinates are what are stored in the model file. When you load the model in Blender or 3DS Max or whatever, the center of the grid is the origin (0, 0, 0) in object space. To position the object in the world, you multiply by a model matrix. Suddenly the origin of the model, which was originally at (0, 0, 0) is now at (model.x, model.y, model.z). [quote] i understand that part with 0,0,0 starts in the centre of the model and not 0,0,0 on the left handed axis till you do a world transform...I think i dont understand when you was explaining the world moves around the camera and not the camera moves around the world and Transforming the model's origin by the view matrix results in (model.x - player.x, model.y - player.y, model.z - player.z). This is in eye coordinates. what is the model vectors?,wont it just have position vectors? :( anyway here is a short video of what i have so far, i am thankfull for all the help so far.. [URL]http://www.youtube.com/watch?v=41LP2M1oYWU[/URL]
[quote]I think i dont understand when you was explaining the world moves around the camera and not the camera moves around the world[/quote] I am the center of my world. My laptop is two feet in front of me. My room's door is four feet to my right. When I'm walking in the hall, the wall at the far end is moving closer towards me. These are eye coordinates. This is how the computer sees things. Everything ends up being relative to the viewer and your computer's screen is the center of the world. You [i]can[/i] choose to see it the other way around, in world coords, where the camera is moving around the scene, but doing so will make your life difficult in cases like this.
Transforming the model's origin by the view matrix results in (model.x - player.x, model.y - player.y, model.z - player.z). This is in eye coordinates. so what would the models origin matrix be?, cause if you set D3DXMatrixIdentity(&Box), that sets it to world, so how do you get its model matrix?
[QUOTE=Anddos;24667829]Transforming the model's origin by the view matrix results in (model.x - player.x, model.y - player.y, model.z - player.z). This is in eye coordinates. so what would the models origin matrix be?, cause if you set D3DXMatrixIdentity(&Box), that sets it to world, so how do you get its model matrix?[/QUOTE] I don't really understand what you're asking. An identity matrix does nothing. If you set the model matrix to the identity, then object coordinates are the same as world coordinates. The object's origin in local object coords (0,0,0) is the same as its origin in world coordinates.
so i have the viewmatrix , so how do i get the objects origin matrix?, thats what i was trying to ask :)
[QUOTE=Anddos;24667916]so i have the viewmatrix , so how do i get the objects origin matrix?, thats what i was trying to ask :)[/QUOTE] For a model moving with the camera, you can set both the view matrix and the model (object?) matrix to the matrix identity. See how it looks in-game. The object might be off-screen or in the wrong place. If it is, apply a small offset transformation to [i]either[/i] the world or view matrices.
[QUOTE=ROBO_DONUT;24668000]For a model moving with the camera, you can set both the view matrix and the model (object?) matrix to the matrix identity. See how it looks in-game. The object might be off-screen or in the wrong place. If it is, apply a small offset transformation to [I]either[/I] the world or view matrices.[/QUOTE] well if i set this Box to IdentityMatrix , its just placed at 0,0,0 in worldspace, how would that follow the camera?
[QUOTE=Anddos;24668034]well if i set this Box to IdentityMatrix , its just placed at 0,0,0 in worldspace, how would that follow the camera?[/QUOTE] You also set the view matrix to identity. Thus object-space == world-space == eye-coords. All your vertexes are suddenly relative to the camera. You can save some calculations by skipping identity matrixes (world and view, in this case) because they don't do anything.
The view matrix is already set to it
[QUOTE=Anddos;24668082]The view matrix is already set to it[/QUOTE] Set to what? Identity? Then set the model matrix to identity. [editline]05:25PM[/editline] Unless you're transforming your projection matrix, which would be really unusual. It should be set once and not touched very frequently.
if i set the model (Box) to the identity , then its set to 0,0,0 which looks like its under the terrain
Your player/camera's position transformations should be part of the view matrix. It's not a strict rule, but it's a really helpful convention to follow. How are you setting player/camera position?
This is the whole camera class stuff, its acutally not my code , its from a book i have bought and trying to learn by adding stuff to the samples :) [code] class Camera { public: Camera(); const D3DXMATRIX& view() const; const D3DXMATRIX& proj() const; const D3DXMATRIX& viewProj() const; const D3DXVECTOR3& right() const; const D3DXVECTOR3& up() const; const D3DXVECTOR3& look() const; D3DXVECTOR3& pos(); void lookAt(D3DXVECTOR3& pos, D3DXVECTOR3& target, D3DXVECTOR3& up); void setLens(float fov, float aspect, float nearZ, float farZ); void setSpeed(float s); void update(float dt, Terrain* terrain, float offsetHeight); protected: void buildView(); public: D3DXMATRIX mView; D3DXMATRIX mProj; D3DXMATRIX mViewProj; // Relative to world space. D3DXVECTOR3 mPosW; D3DXVECTOR3 mRightW; D3DXVECTOR3 mUpW; D3DXVECTOR3 mLookW; float mSpeed; }; #endif // CAMERA_H [/code]Camera* gCamera = 0; [code] Camera::Camera() { //D3DXMatrixIdentity(&mView); //D3DXMatrixIdentity(&mProj); //D3DXMatrixIdentity(&mViewProj); mPosW = D3DXVECTOR3(0.0f, 0.0f, 0.0f); mRightW = D3DXVECTOR3(1.0f, 0.0f, 0.0f); mUpW = D3DXVECTOR3(0.0f, 1.0f, 0.0f); mLookW = D3DXVECTOR3(0.0f, 0.0f, 1.0f); // Client should adjust to a value that makes sense for application's // unit scale, and the object the camera is attached to--e.g., car, jet, // human walking, etc. mSpeed = 50.0f; } const D3DXMATRIX& Camera::view() const { return mView; } const D3DXMATRIX& Camera::proj() const { return mProj; } const D3DXMATRIX& Camera::viewProj() const { return mViewProj; } const D3DXVECTOR3& Camera::right() const { return mRightW; } const D3DXVECTOR3& Camera::up() const { return mUpW; } const D3DXVECTOR3& Camera::look() const { return mLookW; } D3DXVECTOR3& Camera::pos() { return mPosW; } void Camera::lookAt(D3DXVECTOR3& pos, D3DXVECTOR3& target, D3DXVECTOR3& up) { D3DXVECTOR3 L = target - pos; D3DXVec3Normalize(&L, &L); D3DXVECTOR3 R; D3DXVec3Cross(&R, &up, &L); D3DXVec3Normalize(&R, &R); D3DXVECTOR3 U; D3DXVec3Cross(&U, &L, &R); D3DXVec3Normalize(&U, &U); mPosW = pos; mRightW = R; mUpW = U; mLookW = L; buildView(); mViewProj = mView * mProj; } void Camera::setLens(float fov, float aspect, float nearZ, float farZ) { D3DXMatrixPerspectiveFovLH(&mProj, fov, aspect, nearZ, farZ); mViewProj = mView * mProj; } void Camera::setSpeed(float s) { mSpeed = s; } void Camera::update(float dt, Terrain* terrain, float offsetHeight) { // Find the net direction the camera is traveling in (since the // camera could be running and strafing). D3DXVECTOR3 dir(0.0f, 0.0f, 0.0f); if( gDInput->keyDown(DIK_W) ) dir += mLookW; if( gDInput->keyDown(DIK_S) ) dir -= mLookW; if( gDInput->keyDown(DIK_D) ) dir += mRightW; if( gDInput->keyDown(DIK_A) ) dir -= mRightW; // Move at mSpeed along net direction. D3DXVec3Normalize(&dir, &dir); D3DXVECTOR3 newPos = mPosW + dir*mSpeed*dt; if( terrain != 0) { // New position might not be on terrain, so project the // point onto the terrain. newPos.y = terrain->getHeight(newPos.x, newPos.z) + offsetHeight; // Now the difference of the new position and old (current) // position approximates a tangent vector on the terrain. D3DXVECTOR3 tangent = newPos - mPosW; D3DXVec3Normalize(&tangent, &tangent); // Now move camera along tangent vector. mPosW += tangent*mSpeed*dt; // After update, there may be errors in the camera height since our // tangent is only an approximation. So force camera to correct height, // and offset by the specified amount so that camera does not sit // exactly on terrain, but instead, slightly above it. mPosW.y = terrain->getHeight(mPosW.x, mPosW.z) + offsetHeight; } else { mPosW = newPos; } // We rotate at a fixed speed. float pitch = gDInput->mouseDY() / 150.0f; float yAngle = gDInput->mouseDX() / 150.0f; // Rotate camera's look and up vectors around the camera's right vector. D3DXMATRIX R; D3DXMatrixRotationAxis(&R, &mRightW, pitch); D3DXVec3TransformCoord(&mLookW, &mLookW, &R); D3DXVec3TransformCoord(&mUpW, &mUpW, &R); // Rotate camera axes about the world's y-axis. D3DXMatrixRotationY(&R, yAngle); D3DXVec3TransformCoord(&mRightW, &mRightW, &R); D3DXVec3TransformCoord(&mUpW, &mUpW, &R); D3DXVec3TransformCoord(&mLookW, &mLookW, &R); // Rebuild the view matrix to reflect changes. buildView(); mViewProj = mView * mProj; } void Camera::buildView() { // Keep camera's axes orthogonal to each other and of unit length. D3DXVec3Normalize(&mLookW, &mLookW); D3DXVec3Cross(&mUpW, &mLookW, &mRightW); D3DXVec3Normalize(&mUpW, &mUpW); D3DXVec3Cross(&mRightW, &mUpW, &mLookW); D3DXVec3Normalize(&mRightW, &mRightW); // Fill in the view matrix entries. float x = -D3DXVec3Dot(&mPosW, &mRightW); float y = -D3DXVec3Dot(&mPosW, &mUpW); float z = -D3DXVec3Dot(&mPosW, &mLookW); mView(0,0) = mRightW.x; mView(1,0) = mRightW.y; mView(2,0) = mRightW.z; mView(3,0) = x; mView(0,1) = mUpW.x; mView(1,1) = mUpW.y; mView(2,1) = mUpW.z; mView(3,1) = y; mView(0,2) = mLookW.x; mView(1,2) = mLookW.y; mView(2,2) = mLookW.z; mView(3,2) = z; mView(0,3) = 0.0f; mView(1,3) = 0.0f; mView(2,3) = 0.0f; mView(3,3) = 1.0f; } [/code]
I really don't think your view matrix is set to identity when the box is drawn. Right before drawing the box, push the view matrix (if you can, I don't know if D3D has this) or copy it to a temporary location, set the view matrix to identity ([i]after[/i] updating the camera and drawing the terrain), draw the box, then pop/restore the old view matrix.
I was just thinking the inverse of view is worldspace ,so perhaps i could render from an offset off that?
Sorry, you need to Log In to post a reply to this thread.