My solution to this is to find the current position of the mouse, find the angle between that point and the character's current direction vector, and then finally rotate the character's direction vector by that angle to make it point toward the mouse.
I've never used vectors in my games before, and I'm running into a problem; namely, my code implementation isn't working Here is my code (C++):
//set weapon's direction
float angle = m_vDirection.findAngleRadians(mouse);
//rotate
m_vDirection.rotate(angle);
//get angle between weapon direction and 0 degrees
Vector2D vectemp;
vectemp.x = m_vPosition.x - m_rCamera->x;
vectemp.y = m_vPosition.y - m_rCamera->y;
float angleFromZero = m_vDirection.findAngleDegrees(vectemp);
if(angleFromZero > 360)
angleFromZero /= 360;
//set image accordingly
if((angleFromZero >= 0 && angleFromZero <= 30) ||
(angleFromZero >= 330 && angleFromZero <= 0))
{
currentDirection = RIGHT;
}
else if(angleFromZero >= 31 && angleFromZero <= 59)
{
currentDirection = UPRIGHT;
}
else if(angleFromZero >= 60 && angleFromZero <= 120)
{
currentDirection = UP;
}
else if(angleFromZero >= 121 && angleFromZero <= 149)
{
currentDirection = LEFTUP;
}
else if(angleFromZero >= 150 && angleFromZero <= 210)
{
currentDirection = LEFT;
}
else if(angleFromZero >= 211 && angleFromZero <= 239)
{
currentDirection = LEFTDOWN;
}
else if(angleFromZero >= 240 && angleFromZero <= 300)
{
currentDirection = DOWN;
}
else if(angleFromZero >= 301 && angleFromZero <= 329)
{
currentDirection = RIGHTDOWN;
}
m_pSprite->setClip(clips[currentDirection]);
As you can see, I try to find the direction that the weapon is currently pointing by checking the angle between a vector (that I think is) pointing along 0 degrees, and the main character's direction vector. This is the part that I think is really not working. And in actuality, I expect it not to because when I think about it, I feel like there is definitely something wrong with my theory, because I don't know what the origin of these vectors are, which could throw off my whole assumption of the 0-360 degrees being the directions that I think they are.
If anyone can tell me what I'm doing wrong, or if I'm going about this problem in the completely wrong way, please help me out! Thanks in advance
Here are my implementations for Vector2D::findAngleRadians(), Vector2D::findAngleDegrees(), and Vector2D::rotate() just in case anyone wants to see them:
float Vector2D::findAngleRadians(Vector2D& rhs)
{
float dotProd,
lengthLHS,
lengthRHS,
angleCos;
//find dot product
dotProd = dot(rhs);
//find length of each vector
lengthLHS = getLength();
lengthRHS = rhs.getLength();
//find the cosine of the angle
angleCos = dotProd / (lengthLHS * lengthRHS);
//return angle between the two vectors in radians
return acosf(angleCos);
}
//*****************************************************
float Vector2D::findAngleDegrees(Vector2D& rhs)
{
const double PI = 3.1415926;
float dotProd,
lengthLHS,
lengthRHS,
angleCos,
angleInRads;
//find dot product
dotProd = dot(rhs);
//find length of each vector
lengthLHS = getLength();
lengthRHS = rhs.getLength();
//find the cosine of the angle
angleCos = dotProd / (lengthLHS * lengthRHS);
//find angle in radians
angleInRads = acosf(angleCos);
//return angle between the two vectors in degrees
return (angleInRads * (180 / PI));
}
//*****************************************************
Vector2D& Vector2D::rotate(float t)
{
x = x * cosf(t) - y * sinf(t);
y = y * cosf(t) + x * sinf(t);
return *this;
}