# 2d gun shooting bullets at an angle.

Making a 2d left to right duck hunt type game, I am still relatively new to coding. A (horribly created gun haha) sits on the left side, the user pushes up/down to change the angle the gun is shooting at, the bullets are affected by a gravity force (9.81 added to the Y axis after being shot, simplistic way).

The rotation degree is found via

``rotation = (rotationValue* (float)Math.PI) / 180f; ``

Where rotationValue is the value changed when the user presses up or down. I have managed to get the bullets to follow the edge of the cannon and shoot (more or less from where the end of the cannon is located) using

``float xLength = 250 * (float)Math.Cos(rotation); float yLength = 250 * (float)Math.Sin(rotation); barrelPosition.X = position.X + xLength - 30; barrelPosition.Y = position.Y + yLength - 13; ``

Where barrelPosition is a Vector2d, and the position is based off the starting location of the cannon. The values (30 and 13) are numbers I've worked out to get it to look like its coming from the end of the cannon (as opposed to 13 pixels lower and 30 pixels further out). The 250 in the length variables is the length of the barrel of the cannon, which is my hypotenuse for finding my lengths to adjust where the ball shoots from.

So far all of this is working. My problem is that when the cannon fires, i need the balls to understand where its aimed and travel up at the angle the cannon is at. My cannon shoot function is the following. The code is taken from a previous project, limiting how many shots can be made in succession. My previous game, limited you based on the shotcount as well as now much time passed between shooting. I may end up using this, and have not deleted it. The -1*rotation in the velocity seems to have gotten the x axis to work as needed for the most part, however the y velocity was a guestimate. It fires close to how i want but it is off. I notice that the high and low angles are wrong, if i am aiming up at lets say 80o, it shoots like i am aimed at 10o. I tried to negative my numbers, but it makes the balls come from under the game haha.

``internal void Shoot() { int cannonBallIndex = 0; bool shot = false; if( state == State.Alive) { while ( cannonBallIndex < NumCannonBalls && !shot && lastShotMillis >= MillisBetweenShots) { shot = cannonBalls[cannonBallIndex].Shoot(barrelPosition, new Vector2(1*-rotation,0.5f/rotation)); cannonBallIndex++; if (shot) { lastShotMillis = 0; } } } } ``

The cannonballs shoot function is the following. It tells the cannon whether it shot or not. This again goes back to my previous game.

``float speed = 1000; internal bool Shoot(Vector2 position, Vector2 direction) { bool shotWasSuccessful = false; if(state == State.NotFlying) { state = State.Flying; this.position = position; velocity = Speed * direction; shotWasSuccessful = true; } return shotWasSuccessful; } ``

The update function for the cannonballs is the following. Just basically says if following, as the velocity to it based on gametime, and also pull it down with gravity.

``if (state == State.Flying) { position += (velocity) * (float)gameTime.ElapsedGameTime.TotalSeconds; } velocity.Y += MosquitoAttack.Gravity; ``

The cannon is initialized when the game starts, the cannonballs are initialized inside the cannon class. I am sure i am just missing something simple, but alas, it escapes me. Any help is appreciated.

by CanBeUsedAnywhere via /r/csharp