Jump to content
  • Advertisement
Sign in to follow this  
Volvic

getting the direction my canon is facing

This topic is 4249 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am making a tank game and i have got my tank to move with physics properties and the rotate the turret/tower left and right and to rotate the canon up and down, what i did is split the mesh up into 3 1) Tank base 2) Tank turret/tower 3) Tank canon i position the turret by mulitplying the Tank base final Matrix by the Turret trans Matrix to cretae a final matrix for the turret so that it is aligned relevant to the tank base, and the tank canon is aligned with the tank Turret in the same way. Now i have my bullet all set up and it fires from the direction the base is facing and it has all physic properties set such as gravity and it takes into consideration of the angle that the canon has moved. MY PROBLEM IS: I need the Bullet to fire from the direction the cannon is facing? by the way i'm using DirectX 9 and c++

const float TankCannon::MAX_ANG_UP = -18.0f;
const float TankCannon::MAX_ANG_DOWN = 8.0f;
const float TankCannon::ROTATION_RATE = 1.0f;
/********************************************************************
* Constructor
********************************************************************/
TankCannon::TankCannon(void)
{
	pBoundingBox = NULL;
	fAngle = 0.0f;

	vDirection.x = 0.0f;
	vDirection.y = 0.0f;
	vDirection.z = 1.0f;
}
/********************************************************************
* Destructor
********************************************************************/
TankCannon::~TankCannon(void)
{
	pBoundingBox = NULL;
}
/********************************************************************
* changeMesh
********************************************************************/
void	TankCannon::changeMesh(LPDIRECT3DDEVICE9 device, std::string filename)
{
	ModelFileName = filename;
	Model->loadModel(device, ModelFileName.c_str());
}
/********************************************************************
* setModelType
********************************************************************/
void	TankCannon::setModelType(std::string filename)
{
	ModelFileName = filename;
}
/********************************************************************
* create
********************************************************************/
bool	TankCannon::create(LPDIRECT3DDEVICE9 device)
{
	Model = new CModel();
	Model->loadModel(device, ModelFileName.c_str());
	pBoundingBox = new CBoundingVolumes();
	pBoundingBox->BoundingVolumes_CreateBox(device,Model->mesh,size);
	return true;
}
/********************************************************************
* get_BoundingBox
********************************************************************/
void TankCannon::get_BoundingBox(sBoundingBox* BBox)
{
	*BBox = pBoundingBox->s_BoundingBox;
}
/********************************************************************
* getPosition
********************************************************************/
void TankCannon::getPosition(D3DXVECTOR3 &position) 
{
	position = positionVector;
} 
/********************************************************************
* setPosition
********************************************************************/
void TankCannon::setPosition(D3DXVECTOR3 newPosition)
{
	positionVector = newPosition;
}
/********************************************************************
* getDirection
********************************************************************/
void TankCannon::getDirection(D3DXVECTOR3 &Direction) 
{
	D3DXVECTOR3 tempTankDirection;
	D3DXVec3Normalize(& tempTankDirection, & vDirection);	
	Direction = tempTankDirection;
} 
/********************************************************************
* update()
********************************************************************/
void TankCannon::update(float dtime, D3DXVECTOR3 TankCannonOffSetPos,bool Up, bool Down)
{
	positionVector.x = TankCannonOffSetPos.x;
	positionVector.y = TankCannonOffSetPos.y;
	positionVector.z = TankCannonOffSetPos.z;

	//Turn the Cannon of the Tank
	if(Down && !Up) 
	{ 
		if(fAngle > MAX_ANG_UP) 
		{ 
			fAngle-=ROTATION_RATE; 
		}
	}
	if(Up && !Down) 
	{ 
		if(fAngle < MAX_ANG_DOWN)
		{ 
			fAngle+=ROTATION_RATE;
		}
	}
}
/********************************************************************
* render()
********************************************************************/
void TankCannon::render(LPDIRECT3DDEVICE9 device)
{
	D3DXMATRIX transMatrix;
	D3DXMATRIX rotMatrix;
	D3DXMATRIX scaleMatrix;
	D3DXMATRIX finalMatrix;

	D3DXMatrixTranslation(&transMatrix, positionVector.x, positionVector.y, positionVector.z);
	// rotate the TankCannon Up and Down
	D3DXMatrixRotationX(&rotMatrix,D3DXToRadian(fAngle));
	D3DXMatrixScaling(&scaleMatrix, size, size, size);
	D3DXMatrixMultiply(&transMatrix, &rotMatrix, &transMatrix);
	D3DXMatrixMultiply(&transMatrix, &scaleMatrix, &transMatrix);	
	// multiply the TankCannon matrix by the TankTower matrix to set it relative to the TankTower
	D3DXMatrixMultiply(&finalMatrix, &transMatrix, &pTankTower->finalMatrix);
	device->SetTransform(D3DTS_WORLD, &finalMatrix);
	Model->render(device);
}
/********************************************************************
* setSize()
********************************************************************/
void TankCannon::setSize(float Size)
{
	size = Size;
}


Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
horizontally, the bullet should launch the same direction as the turret base
along that line, it then needs to be tilted upwards by the same angle as the cannon

Share this post


Link to post
Share on other sites
Unless I've misread what you've posted, multiplying a unit vector by your rotation matrices to get the direction vector for your bullet should work.

Share this post


Link to post
Share on other sites
What if it's a crab canon? Then you couldn't tell which way it's facing, because it would be the same in both directions.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!