   Making a box move in the camera direction
  I am trying to translate a box in the camera look direction so i can understand how fps games work, heres my code.The box would be a bullet but this is just early programming for me to understand things better,does anyone know why it dosent do what i expected it todo?D3DXMATRIX GetView;device->GetTransform(D3DTS_VIEW,&GetView);D3DXMATRIX GetViewInView;D3DXMatrixInverse(&GetViewInView,NULL,&GetView);D3DXVECTOR3 CameraPos(GetViewInView._41,GetViewInView._42,GetViewInView._43 + 5.0f); //look.x look.y look.zD3DXVECTOR3 CameraDir(GetView._31,GetView._32,GetView._33);D3DXVec3Normalize(&CameraDir,&CameraDir);float speed = 0.1f;D3DXMATRIX BoxMatrix;D3DXMatrixTranslation(&BoxMatrix,CameraPos.x+CameraDir.x*gtimeDelta*speed,    CameraPos.y+CameraDir.y*gtimeDelta*speed, CameraPos.z+CameraDir.z*gtimeDelta*speed);device->SetTransform(D3DTS_WORLD,&BoxMatrix);gdevice->SetMaterial(&d3d::RED_MTRL);Box->DrawSubset(0);   
  In this line:D3DXVECTOR3 CameraDir(GetView._31,GetView._32,GetView._33);You should probably be using GetViewInView, not GetView.As for the rest, it depends on where and under what circumstances your 'box' code is being executed.   ;Quote:Original post by jykIn this line:D3DXVECTOR3 CameraDir(GetView._31,GetView._32,GetView._33);You should probably be using GetViewInView, not GetView.As for the rest, it depends on where and under what circumstances your 'box' code is being executed.So you use the camera inverse matrix direction , which would be worldspace direction right?I see the cube infront of camera but it dosent seem to to be moving back[Edited by - Anddos on May 11, 2010 5:28:05 AM];Quote:I see the cube infront of camera but it dosent seem to to be moving backBased on the code that you posted, it looks to me like the position of the box will always be fixed relative to the camera (because the camera position and direction are read every update, and then the box is immediately positioned based on the current position and direction).If you want the effect of the camera 'firing' the box, you need to read the camera position and direction only once, when the box is created, and then update the box's position incrementally from there on.In short, it looks like your logic is wrong in at least a couple different ways. If the code you posted was posted out of context and is actually executed at different times and under different circumstances, then I'd recommend posting your actual code so that we can take a look at it. If however the code is executed as posted, then I recommend stepping through it carefully and thinking about what exactly will happen each update (at which point the problems with the code should become clear).   ;  So i think CameraPos is the problem as the cube is always set to this position?But if i didnt use this then where would the cube spawn from etc?[Edited by - Anddos on May 12, 2010 12:15:15 PM];Quote:Original post by AnddosSo i think CameraPos is the problem as the cube is always set to this position?But if i didnt use this then where would the cube spawn from etc?When the cube spawns, it spawns relative to the current camera transform (the simplest thing to do in this case would be to assign the camera transform to the cube directly).However, you only do this once, when the cube is spawned. In subsequent updates, all you do is move the cube some distance along its forward direction vector.   ;  So can you correct my code so its working how i want?   ;Quote:So can you correct my code so its working how i want?Not really. I don't know in what context the code you posted is executed, and I couldn't really tell you how to restructure it without seeing your whole program (or at least the relevant parts of it). Generally speaking, the 'spawning' and assignment of the initial transform and the per-update moving of the box will happen in different places, so it's not just a 'code issue', it's a 'program structure' issue.What I would recommend though is to try and do it yourself, and then post back here with any questions you might have. I suppose you could also post your whole program (be sure to use [source] tags); if it's not too long, someone might be able to take a look at it and give you some tips.   ;  To the op. You need to understand the basic of linear algebra (vectors and their operation) before getting tied down with implementation and or API. With that said its a simple operation.Let say we want to start the object from the position of the camera without any offset and move in in the direction of the camera. Here is the basic vector operation.cPos - camera posviewDir - camera view direction vector (where the camera is pointing), should be normalized (doesn't have to )incr - The increment we want to move. could be any unit.newPos - The final position of the object after moving incr units.newPos = cPos + (viewDir * incr);   