So I have a turret type gun, with a base sprite () and a separate sprite for the shaft (). The shaft is drawn behind the base, and rotates to point towards the mouse position. The angle of the shaft is stored as _turretAngle
.
My issue comes in when I try to position the bullet correctly. The bullet in question () needs to always fire from the tip of the shaft, angled correctly, and of course, in the proper direction.
To angle the bullet, I simply use _turretAngle
, and the direction is new Vector2(-Math.Cos(_turretAngle), -Math.Sin(_turretAngle));
but I can't get the initial position correct.
I currently have it as follows:
int i = _bullets.Count - 1;
Bullet b = _bullets[i];
b.SetAngle(_turretAngle);
float cos = (float)Math.Cos(_turretAngle);
float sin = (float)Math.Sin(_turretAngle);
b.SetDirection(new Vector2(-cos, -sin));
var posX = (Position.X - (Shaft.Width * cos) - (b.Width * cos)) + (Width / 2) - ((b.Width / 2) * cos);
var posY = Position.Y - (Shaft.Height * sin) - (b.Height * sin);
b.SetPosition((int)posX, (int)posY);
b.BulletState = Bullet.State.Active;
The bullet is so close to being position properly, but it is not position at the shaft, instead (depending on the angle of the turret) positioned anywhere from 5-15 pixels ahead of the turret.
How it appears currently:
How I'd like it to appear:
I know I might be being nit-picky here, but I'd really like to know how the math is wrong.
I realize there might be some information missing, so let me know if I need to add anything.