Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm currently stuck with my game, which is from the top perspective. I'm currently using these sprites for player: (using the second at the top):

enter image description here

And here's what I use for my gun

enter image description here

My problem is that I am not sure how to calculate origin & position to make sure that gun is always in player's hands. This is my code for drawing player and gun

spriteBatch.Draw(Textures.Load("player"), new Vector2(playerPosition.X, playerPosition.Y), new Rectangle(35, 2, 27,27), Color.White, player.rotation, new Vector2(playerWidth / 2, playerHeight / 2), 1f, SpriteEffects.None, 1f);
spriteBatch.Draw(Textures.Load("Guns/deagle"), new Vector2(playerPosition.X, playerPosition.Y), null,                   Color.White, player.rotation, new Vector2(playerWidth / 2, playerHeight / 2), 1f, SpriteEffects.None, 1f);

With rotation of 0, I'd need to move my gun to playerPosition.Y + 15 to make sure gun is in guy's hands, but when I do it, it looks fine but as soon as I rotate him, it's even more weird than now.

Here's how it currently looks like, with my specified code above.

https://dl-web.dropbox.com/get/capture-14.mp4?w=40052a2d

All I need is to make sure that gun will be always in guy's hands

share|improve this question
    
It may be acting weird because your spritesheet is bad. 0 degrees is right/east. So if you you want sprites to face the way your direction input is, sprites needs to be pointing right/east. –  William 'MindWorX' Mariager Jan 25 '12 at 22:01
    
Here's a version that works, where I've fixed the sprites. I'm sure that's what's causing the issues for you. dl.dropbox.com/u/992656/permanent/MindWorX.MartinHelp.zip –  William 'MindWorX' Mariager Jan 25 '12 at 22:28
    
@MindWorX: for some reason, no, this is not the case. My rotation calculation works as Vector2 direction = new Vector2(player.x, player.y) - new Vector2(worldPosition.X, worldPosition.Y);player.rotation = (float)(Math.Atan2(direction.Y, direction.X)). If I had it the way you described, I had to add 180 degrees to the rotation –  Martin. Jan 26 '12 at 6:12
    
@MindWorX: please, put this as answer and describe "how it works" a bit more? I'd like to accept that one. Is that really matter of the 180 difference in rotation ? –  Martin. Jan 26 '12 at 6:31
    
@MindWorX: oh, I got it. I had completelly switched my logic for calculating player rotation, it means that 0° for your logic was actually 180° for my logic. It works fine now, thanks for your help –  Martin. Jan 26 '12 at 11:40
add comment

3 Answers

up vote 5 down vote accepted

You need to offset the gun's position based on the rotation of the player. Here's the first formula that comes to mind:

x = x + offset*cos(rotation)

y = y + offset*sin(rotation)

So in your specific case use this as the gun's position:

new Vector2( playerPosition.X + 15 * (float) Math.Cos(player.rotation), playerPosition.Y + 15 * (float) Math.Sin(player.rotation) )
share|improve this answer
    
Hmm, unfortunatelly, that's not what I need, but we are near the finish. i.imgur.com/cCfoF.png –  Martin. Jan 25 '12 at 16:10
    
I based the code on the fact that you said with rotation = 0 you need a translation of +15 in Y direction. Try swapping the sin and cos. –  Jannie Jan 25 '12 at 16:44
    
Unfortunately it's acting strange when I do so. Take a look at this video –  Martin. Jan 25 '12 at 19:03
    
@Martin this answer is probably close - you might try using an ellipse (use different multipliers for X and Y). –  Jonathan Dickinson Jan 25 '12 at 21:32
    
@JonathanDickinson: yeah, I didn't even think about trying to change multipliers. It works perfectly now (with -18): i.imgur.com/gfT92.png –  Martin. Jan 26 '12 at 11:41
add comment

I would suggest you treat your sprites as a group of sprites. When rotated, they will all rotate together as a single entity.

This would greatly simplify what you are trying to do. Have a look at: http://msdn.microsoft.com/en-us/library/bb194912.aspx

share|improve this answer
add comment

So here's actually how I solved it:

  • I had to apply MindVorX's line of code (origin of weapon should be Vector.Zero)
  • I had to apply Jannie's logic
  • I had to play with multiplier of Sin & Cos

so after all, it looks like

        spriteBatch.Draw(
          Textures.Load("player"),
          new Vector2(player.x, player.y),
          new Rectangle(35, 2, 27, 27),
          Color.White,
          player.rotation,
          new Vector2(27 / 2, 27 / 2), // half a player's width & height
          1f,
          SpriteEffects.None, 1f);
        spriteBatch.Draw(
          Textures.Load("Guns/deagle"),
          new Vector2( player.x - 18 * (float) Math.Cos(player.rotation), player.y + 18 * (float) Math.Sin(player.rotation) ),
          null,
          Color.White,
          player.rotation,
          Vector2.Zero, // zero here is important!
          1f,
          SpriteEffects.None, 1f);

which results in

Thanks all of you

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.