Options

# rotating an object based on mouse location and muzzle point.

• #### Index

• rotating an object based on mouse location and muzzle point.
• This is a locked, single-post thread from Creation Asylum. Archived here to prevent its loss.

This is going to be slightly advanced for most but I posted this by request from GS who wanted to see the snippet. I would narrow it down to tiny pieces however I am not that great at writing tutorials on this stuff so if you have a basic understanding of C# and XNA you will be able to get this fairly easily I think.

This is in my main level class in the update function to determine the angle of the players torso as well as getting the muzzle's location based on the torso's angle. I did leave out some of the gamepad related stuff because if you can figure out how to do it with the mouse you can generally figure it out with the mouse. Where you see R, that is actually the distance you want from the origin (the "hinge" that the character rotates off of) for the muzzle "point" (where you can essentially make anything happen, such as particle effects or in the case here where i am, obviously, determining where the bullets should emit from). I would also like to point out that the reason the muzzleangle and player_torso.angle are different when Player_Torso.Dir.X is less than zero because when you use SpriteEffects.FlipHorizontally, i believe the coordinates get all swapped around so you have to go with two negatives for the coordinates but you dont have to with muzzleangle, so I just separated their values.
Code:
```Player_Torso.mouseLoc = new Vector2(ms.X, ms.Y + 25); //setup the mouse location             crosshairLoc = new Vector2(crosshairX, crosshairY);                                           Player_Torso.dir = new Vector2(crosshairLoc.X - (graphics.PreferredBackBufferWidth * .5f),                         crosshairLoc.Y - (graphics.PreferredBackBufferHeight * .5f)); //Your original vector subtraction was backwards. In vector math, A-B results in a vector that points from B to A                 if (Player_Torso.dir.X < 0) //Simplified this because the calculation was already done when creating Player_Torso.dir                 {                     Player_Torso.flip = SpriteEffects.FlipHorizontally;                     Player_Torso.angle = (float)((Math.Atan2(-Player_Torso.dir.Y, -Player_Torso.dir.X)));                     muzzleangle = (float)((Math.Atan2(Player_Torso.dir.Y, -Player_Torso.dir.X)));                     muzzle.X = -R * (float)Math.Cos(muzzleangle); //Note the negative sign                     muzzle.Y = R * (float)Math.Sin(muzzleangle) - 10;                 }                 else                 {                     Player_Torso.flip = SpriteEffects.None;                     Player_Torso.angle = (float)((Math.Atan2(Player_Torso.dir.Y, Player_Torso.dir.X)));                     muzzleangle = (float)((Math.Atan2(Player_Torso.dir.Y, Player_Torso.dir.X)));                     muzzle.X = R * (float)Math.Cos(muzzleangle);                     muzzle.Y = R * (float)Math.Sin(muzzleangle) - 10;                 }                 crosshairX = ms.X;                 crosshairY = ms.Y;             }                          if (ms.LeftButton == ButtonState.Pressed && old_ms.LeftButton == ButtonState.Released)             {                                  mouseLocationX = player.Position.X + ms.X - 342;                 mouseLocationY = player.Position.Y + ms.Y - 225;                 if (ms.X < graphics.PreferredBackBufferWidth * .55f  &&                     ms.X > graphics.PreferredBackBufferWidth * .40f &&                     ms.Y < graphics.PreferredBackBufferHeight * .58f &&                     ms.Y > graphics.PreferredBackBufferHeight * .40f)                 {                     weapon.isFired = false;                 }                 else                 {                     weapon.isFired = true;                 }             }             weapon.Update(Weapon.WeaponType.Shotgun,                 new Vector2(muzzle.X + player.Position.X , muzzle.Y + player.Position.Y),                 mouseLocationX,                 mouseLocationY,                 _gameTime);```

in the weapon class, I did this.

Code:
```if (isFired == true)                     {                         for (int b = 0; b <= bullet.Length - 1; b++)                         {                             Vector2 _destination = new Vector2(_destX - 55, _destY - 75);                             bullet[b].Collision = new Rectangle((int)bullet[b].Position.X, (int)bullet[b].Position.Y, 10, 10);                             Vector2 movement = _destination - _origin;                             if (movement != Vector2.Zero)                                 movement.Normalize();                             bullet[b].Position += movement * shotSpeed * (float)gameTime.ElapsedGameTime.TotalMilliseconds;                             appearanceTimer += (float)gameTime.ElapsedGameTime.Milliseconds;                             if (appearanceTimer >= appearanceInterval)                             {                                 showBullet = true;                                                              }                             shotTimer += (float)gameTime.ElapsedGameTime.TotalSeconds;                             shotInterval = shotRange;                             if (shotTimer >= shotInterval)                             {                                 isFired = false;                                 showBullet = false;                                 shotTimer = 0f;                                 appearanceTimer = 0f;                             }                         }                     }                     else                     {                         for (int b = 0; b <= bullet.Length - 1; b++)                         {                             bullet[b].Position = _origin;                         }                     }```

In my player_torso class, I did this with the draw class. (a few other things that I can delete but I am keeping in there in case I need to change something)

Code:
`_spriteBatch.Draw(SpriteTextures[0], position, source, Color.White, angle, new Vector2(source.Width * .50f, source.Height * .55f), 1.0f, flip, 0.0f);`

If you have any questions, just post here and I'll answer any questions to the best of my ability (and if it appears I left something out, feel free to let me know and I'll explain it or post what I am missing.)
Thanks given by: