Aiming with the mouse

Started by
2 comments, last by Thaumaturge 16 years, 1 month ago
Hi! I'm making a 2D shooter game. I want to aim with the mouse so, I'm using this formula :
void Update()
{
	D3DXVECTOR2 vec_aim = Engine::GetInstance()->GetInputModule()->GetMousePosition();
	D3DXVECTOR2 vec_soldier = D3DXVECTOR2(GetPosition().x, GetPosition().y);

	m_Angle = atan((vec_aim.y - vec_soldier.y) / (vec_aim.x - vec_soldier.x));
}
But, there is a problem: it works only if the mouse abscissa coordinate are superior to the soldier abscissa coordinate. If not, the angle return to PI/2 or -PI/2 Click here to download the binaries Thanks for all.
Advertisement
Perhaps I'm mistaken, but have you tried atan2(dy/dx)?

MWAHAHAHAHAHAHA!!!

My Twitter Account: @EbornIan

No, I didn't tried this, but this works and I don't know why. Thanks :)
It's my pleasure. ^_^

Atan2 provides answers ranging over (-PI, PI], rather than atan's (-PI/2, PI/2], if I'm not much mistaken. ;)

MWAHAHAHAHAHAHA!!!

My Twitter Account: @EbornIan

This topic is closed to new replies.

Advertisement