4
\$\begingroup\$

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

\$\endgroup\$
5
  • \$\begingroup\$ 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. \$\endgroup\$
    – William
    Jan 25, 2012 at 22:01
  • \$\begingroup\$ 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 \$\endgroup\$
    – William
    Jan 25, 2012 at 22:28
  • \$\begingroup\$ @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 \$\endgroup\$
    – Martin.
    Jan 26, 2012 at 6:12
  • \$\begingroup\$ @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 ? \$\endgroup\$
    – Martin.
    Jan 26, 2012 at 6:31
  • \$\begingroup\$ @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 \$\endgroup\$
    – Martin.
    Jan 26, 2012 at 11:40

3 Answers 3

5
\$\begingroup\$

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) )
\$\endgroup\$
5
  • \$\begingroup\$ Hmm, unfortunatelly, that's not what I need, but we are near the finish. i.imgur.com/cCfoF.png \$\endgroup\$
    – Martin.
    Jan 25, 2012 at 16:10
  • \$\begingroup\$ 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. \$\endgroup\$
    – Jannie
    Jan 25, 2012 at 16:44
  • \$\begingroup\$ Unfortunately it's acting strange when I do so. Take a look at this video \$\endgroup\$
    – Martin.
    Jan 25, 2012 at 19:03
  • \$\begingroup\$ @Martin this answer is probably close - you might try using an ellipse (use different multipliers for X and Y). \$\endgroup\$ Jan 25, 2012 at 21:32
  • \$\begingroup\$ @JonathanDickinson: yeah, I didn't even think about trying to change multipliers. It works perfectly now (with -18): i.imgur.com/gfT92.png \$\endgroup\$
    – Martin.
    Jan 26, 2012 at 11:41
3
\$\begingroup\$

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

\$\endgroup\$
0
0
\$\begingroup\$

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

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .