Quantcast
Channel: Community | MonoGame - Latest topics
Viewing all articles
Browse latest Browse all 6821

[SOLVED] Top down shooter - position of the bullet after rotating the player

$
0
0

@KuzDeveloper wrote:

Hi Everyone!

I am struggling with trigonometry. :slightly_smiling: In my top down shooter game, I am trying to set the position of the bullet when the player shoots, but I cannot find the working solution and it drives me crazy. I am really bad in trigonometry (that's my Sin...bad joke, I know :D), so if anyone could help me fixing my code that would be very good.

Here is my problem with a screenshot:

As you can see the bullet is always a bit above the gun (it is always on the top of the Origin of the texture).
The full source code is here:
Dropbox link

I was following different tutorials, and the best what I found calculates the angle and the position like this:

MouseState mouse = Mouse.GetState();

Vector2 mousePosition = new Vector2(mouse.Position.X, mouse.Position.Y);
//This is the player on the picture.           
Vector2 survivorPosition = new Vector2(RectangleSurvivorDestination.X, RectangleSurvivorDestination.Y);

Vector2 vector = (mousePosition - survivorPosition);

if(vector != Vector2.Zero)
   Vector2.Normalize(vector);

AngleSurvivor = (float)Math.Atan2(vector.Y, vector.X);
AngleBullet = AngleSurvivor;

//So far so good, the player and even the bullet is rotating properly.

/*THIS IS THE INCORRECT SECTION I GUESS*/
//The player texture width is 77px, height is 33px. I used hard-coded values
//for better readability.
//(77.0f / 2.0f) - this is because the Origin is the center of the image and I
//wanted the bullet to come from out of the barrel of the gun.
//(33.0f / 1.0f) - the gun is not at the center of the image, so I wanted to
//shift the bullet down to the barrel. Maybe this is wrong.
Vector2 bulletPosition = new Vector2(
   (float)(survivorPosition.X + (77.0f / 2.0f) * Math.Cos(AngleSurvivor)),
   (float)(survivorPosition.Y + (33.0f / 1.0f) * Math.Sin(AngleSurvivor)));
   RectangleBulletDestination = new Rectangle(
   (int)bulletPosition.X, (int)bulletPosition.Y,
   RectangleBulletDestination.Width, RectangleBulletDestination.Height);

Any ideas? :slightly_smiling: Thank you in advance!

Posts: 6

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 6821

Trending Articles