Moving an object through 3D space

This is a discussion on Moving an object through 3D space within the Game Programming forums, part of the General Programming Boards category; Hi Folks; I am trying to build a 3D game and I need to move an object through 3D space. ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    5

    Moving an object through 3D space

    Hi Folks;

    I am trying to build a 3D game and I need to move an object through 3D space. Essentially what I'm working with is a 10X10X10 cube and I want to move an object from (x1, y1, z1) to (x2, y2, z2). I tried a couple things including 3D vectors, but I don't know enough about them and two linear formulas y = m1*x+b1 and z = m2*x+b2 but I still get a bizzar result. Can any of you help me out with this?

    Thanks for your help!

  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    depends. You could use glTranslate between glPushMatrix() and glPopMatrix() unless you are in Direct X I don't know

  3. #3
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    a simple way would be to have a set of variables for each degree of freedom(x,y,z) then have a movement section of x+=x_vel, y+=y_vel, and z+=z_vel. Then you can have a separate function to calculate the velocity for each DoF.

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    You need a camera class. I'm using one based on an idea by Frank D. Luna. If you can get his book Introduction to 3D Game Programming with DirectX 9.0. It's available over at www.amazon.com. This class is very similar to what is in his book.

    CCamera.h
    Code:
    #ifndef CCAMERA 
    #define CCAMERA
     
     
    class CCamera
    {
      protected:
    	D3DXVECTOR3 Right;
    	D3DXVECTOR3 Up;
    	D3DXVECTOR3 Look;
    	D3DXVECTOR3 Pos;
     
      public:
    	CCamera(void);
    	virtual ~CCamera(void) {}
     
    	void Strafe(float _units); // left/right
    	void Fly(float _units); // up/down
    	void Walk(float _units); // forward/backward
     
    	void Pitch(float _angle); // rotate on right vector
    	void Yaw(float _angle); // rotate on up vector
    	void Roll(float _angle); // rotate on look vector
     
    	void GetViewMatrix(D3DXMATRIX* _V); 
    	void GetPosition(D3DXVECTOR3* _pos); 
    	void SetPosition(D3DXVECTOR3* _pos); 
     
    	void getRight(D3DXVECTOR3* _right);
    	void getUp(D3DXVECTOR3* _up);
    	void getLook(D3DXVECTOR3* _look);
    };
     
    #endif		  //end of CCamer.h
    CCamera.cpp
    Code:
     
    #include "CCamera.h"
     
    CCamera::CCamera()
    {
     
      Pos = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
      Right = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
      Up = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
      Look = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
     
    }
    void CCamera::GetPosition(D3DXVECTOR3* _pos)
    {
      *_pos =Pos;
    }
     
    void CCamera::SetPosition(D3DXVECTOR3* _pos)
    {
       Pos = *_pos;
    }
     
    void CCamera::GetRight(D3DXVECTOR3* _right)
    {
       *_right = Right;
    }
     
    void CCamera::GetUp(D3DXVECTOR3* _up)
    {
      *_up = Up;
    }
     
    void CCamera::GetLook(D3DXVECTOR3* _look)
    {
      *look = Look;
    }
     
    void CCamera::Walk(float _units)
    {
      // move only on xz plane for land object
      //if( _cameraType == LANDOBJECT )
      // Pos += D3DXVECTOR3(Look.x, 0.0f,Look.z) * _units;
     
      //if( _cameraType == AIRCRAFT )
      
      Pos +=Look * _units;
    }
     
    void CCamera::Strafe(float _units)
    {
      // move only on xz plane for land object
      //if( _cameraType == LANDOBJECT )
      //Pos += D3DXVECTOR3(Right.x, 0.0f, Right.z) * _units;
      //if( _cameraType == AIRCRAFT )
      
      Pos += Right * _units;
    }
     
    void CCamera::Fly(float _units)
    {
      //if( _cameraType == AIRCRAFT )
      
      Pos += Up * _units;
    }
     
    void CCamera::Pitch(float _angle)
    {
      D3DXMATRIX T;
      D3DXMatrixRotationAxis(&T, &Right, _angle);
     
      // rotate _up and _look around _right vector
      D3DXVec3TransformCoord(&Up,&Up, &T);
      D3DXVec3TransformCoord(&Look,&Look, &T);
    }
     
    void CCamera::Yaw(float _angle)
    {
      D3DXMATRIX T;
      
      // rotate around world y (0, 1, 0) always for land object
      //if( _cameraType == LANDOBJECT )
      
      //D3DXMatrixRotationY(&T, angle);
      // rotate around own up vector for aircraft
      
      if( _cameraType == AIRCRAFT )
      D3DXMatrixRotationAxis(&T, &Up, _angle);
      
      // rotate _right and _look around _up or y-axis
      D3DXVec3TransformCoord(&Right,&Right, &T);
      D3DXVec3TransformCoord(&Look,&Look, &T);
    }
     
    void CCamera::Roll(float _angle)
    {
      // only roll for aircraft type
      //if( _cameraType == AIRCRAFT )
      //{
    	
    	D3DXMATRIX T;
    	D3DXMatrixRotationAxis(&T, &Look, _angle);
    	
    	// rotate _up and _right around _look vector
    	D3DXVec3TransformCoord(&Right,&Right, &T);
    	D3DXVec3TransformCoord(&Up,&Up, &T);
      
      //}
    }
     
    void CCamera::GetViewMatrix(D3DXMATRIX* _V)
    {
       // Keep camera's axes orthogonal to eachother
       D3DXVec3Normalize(&Look, &Look);
       D3DXVec3Cross(&Up, &Look, &Right);
       D3DXVec3Normalize(&Up, &Up);
       D3DXVec3Cross(&Right, &Up, &Look);
       D3DXVec3Normalize(&Right, &Right);
       
       // Build the view matrix:
       float x = -D3DXVec3Dot(&Right, &Pos);
       float y = -D3DXVec3Dot(&Up, &Pos);
       float z = -D3DXVec3Dot(&Look, &Pos);
       
       (*_V)(0,0) = Right.x; (*_V)(0, 1) = Up.x; (*_V)(0, 2) = Look.x; (*_V)(0, 3) = 0.0f;
       (*_V)(1,0) = Right.y; (*_V)(1, 1) = Up.y; (*_V)(1, 2) = Look.y; (*_V)(1, 3) = 0.0f;
       (*_V)(2,0) = Right.z; (*_V)(2, 1) = Up.z; (*_V)(2, 2) = Look.z; (*_V)(2, 3) = 0.0f;
       (*_V)(3,0) = x;		(*_V)(3, 1) = y;	 (*_V)(3, 2) = z;	   (*_V)(3, 3) = 1.0f;
    }
    That should do the trick. You should probably inline the smaller functions later.

    Hope this helps.

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    Then to fly through 3D space all you do is move along the correct vector.

    The CCamera::Walk() function does this for you. After all objects have been transformed by the view matrix and the coordinate space has been aligned correctly to match your orientation (which is what the view matrix does) the vectors will be correct.

    To move backwards just multiply units by a negative value - hence send a negative value as the units parameter thus walking backwards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Build an array of strings dynamically
    By Nazgulled in forum C Programming
    Replies: 29
    Last Post: 04-07-2007, 09:35 PM
  2. Replies: 4
    Last Post: 01-16-2006, 04:58 PM
  3. Linked List Templates and Object Types
    By ventolin in forum C++ Programming
    Replies: 10
    Last Post: 06-16-2004, 12:05 PM
  4. Moving the object '@' x and y?
    By bluehead in forum C++ Programming
    Replies: 4
    Last Post: 11-06-2001, 04:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21