0
\$\begingroup\$

im creating a platform shooter. the gun can be rotated using the thumbsticks. i want to use a line of fire newBullet.bulletVel variable as a Vector 2. this variable will be used to send the bullet in the direction that the barrel of the weapon is pointed at. How could i do this?

for gun rotation i have declared a float named rotation and use spritebatch.draw method to show that the variable is for rotation of the image.

public float WMrotation;

public void UpdateMe(GameTime gameTime)
    {                                 
        if (GamePad.GetState(PlayerIndex.One).ThumbSticks.Right.Y < -0.1)
        {
            WMrotation += 0.03f;
        }
        if (GamePad.GetState(PlayerIndex.One).ThumbSticks.Right.Y > 0.1)
        {
            WMrotation -= 0.03f;
        }          
    }

public void DrawMe(SpriteBatch sb)
    {
        sb.Draw(WMtxr, WMpos, null, Color.White, WMrotation, WMorigin, 1f, SpriteEffects.None, 0);
    }

ive set the bullet position when fired to be at the tip of the gun.

newBullet.bulletPos.X = player1.playerPosition.X + 15;
newBullet.bulletPos.Y = player1.playerPosition.Y + 15;

i think i need to use newBullet.bulletVel to calculate the vector in which the bullet travels

newBullet.bulletVel = i don't know what to put in here

Diagram of desired vector

Any help would be appreciated.

\$\endgroup\$
2
\$\begingroup\$

You can use trigonometry:

x = cos(angle)
y = sin(angle)

Make sure angle is in radian

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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