I've been trying to work for a few days, on a little weapon system where you can aim with your mouse and shoot bullets in the direction you're aiming. This is all in 2D to be clear.
Right now, I can calculate the spawn point of the bullet at the tip of the gun quite well and the angle of the sprite is correct and pointing at the mouse however, I believe I am actually calculating the angle somehow inversely and because of this, I can't find a good way to give the bullets; when spawned; the correct starting velocity to fire in the correct direction as well as the correct sprite rotation.
I am calculating and setting the angle of the sprite to the angle given between the mouse position and the character position like so: (Following code is in the Vector2 class)
double Vector2::AngleRad(Vector2* other) { return atan2(other->y - y, other->x - x); } double Vector2::AngleDeg(Vector2* other) { return radiansToDegrees(AngleRad(other)); } and calculating the bullets spawn point (which works right now with the given angle, even though it's reversed) like so: (Following code is in the weapon class)
void Weapon::CalculateBulletExit() { double centerX = GetPos()->x + GetCollisionBox()->W() / 2; double centerY = GetPos()->y + GetCollisionBox()->H() / 2; double radius = GetCollisionBox()->W() / 2; double radiansDeg = degreesToRadians(entityAngle); pointOfExit->x = (centerX + radius * cos(radiansDeg)); pointOfExit->y = (centerY + radius * sin(radiansDeg)); } So now I want to spawn a bullet at the correct position (the tip of the gun) with the right sprite rotation, and the the correct object velocity. Right now this is how I am doing that: (Following code is in the bullet class)
bool Bullet::Spawn(double heading, Vector2* spawnPoint, double bulletSpeed) { if (mat->dead) { //Make the bullet alive and then return true that the bullet has been spawned mat->dead = false; //Set position and initial velocity double xGoPos = spawnPoint->x - GetCollisionBox()->W() / 2; double yGoPos = spawnPoint->y - GetCollisionBox()->H() / 2; entityBox->X(xGoPos); entityBox->Y(yGoPos); linearVelocity->x = xGoPos + cos(degreesToRadians(heading)) * bulletSpeed; linearVelocity->y = yGoPos + sin(degreesToRadians(heading)) * bulletSpeed; entityAngle = heading; return true; } else { return false; } } But this is spawning the bullet with a sprite rotation facing 180° backwards, and a velocity weirdly pointing downwards and away. Plus the velocity seems to always towards the right even when I am aiming left.
What is the easiest and fastest way of giving a bullet a velocity in the direction of the mouse (aiming direction) and am I actually calculating the angle correctly or horrifically wrong? :P
From what I've researched and read, there's heaps of ways of getting the angle between two vectors but all I am able to precisely know is the character's bounding box as x, y, w, h. The weapon bounding box as x, y, w, h. And the mouse position as an x, y vector2 class I made. Thanks for any suggestions and help that I get!
