Click here to Skip to main content
15,890,947 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Code is in C# and the IDE is Visual Studio2017.my code and assets are at diliupg / Space_Battles — Bitbucket[^].

My Space ship rotation code has something wrong cause when I fire the projectile shoots from the middle of the spaceship. There are no other errors. How can I get the spaceship to fire from the front?

What I have tried:

I create the lazer shot when I press the space by like this:
<pre>

if (keyboard.IsKeyDown(Keys.Space) && shoot)
{
    shoot = false;
    //is.fireInst.Play();
    this.fire.Play(.2f, .9f, 0);
    lazerX = (float)(spritePosX - (0) * Math.Cos(rotationAngle));
    lazerY = (float)(spritePosY - (0) * Math.Sin(rotationAngle));
 
    Projectile projectile = new Projectile(heroLazer, "heroLazer",
        lazerX, lazerY, rotationAngle);
 
    Game1.AddProjectile(projectile);
}
// then in the projectile class:

public Projectile(Texture2D lazerSprite, string spriteName,
    float posx, float posy, float heading)
{
    projectileSprite = lazerSprite;
    type = spriteName;
    spriteRotOrigin = new Vector2(projectileSprite.Width / 2, projectileSprite.Height / 2);
    spriteHeading = heading;
    spritePosX = posx; // the x position of the lazer
    spritePosY = posy; // the y position of the lazer
 
    spritePos = new Vector2(spritePosX, spritePosY);
    drawRectangle = new Rectangle((int)spritePosX, (int)spritePosY,
        projectileSprite.Width / 2, projectileSprite.Height / 2);
 
}
// then in the update method

<pre>public void update(GameTime gameTime, KeyboardState keyboard)
{
    //projectile active or not?
    if (active == true)
    {
        projectileAge += gameTime.ElapsedGameTime.Milliseconds;
 
        spritePosX += (float)(Math.Sin(spriteHeading) *
            gameTime.ElapsedGameTime.TotalMilliseconds) * ProjectileMoveAmount;
        spritePosY -= (float)(Math.Cos(spriteHeading) *
            gameTime.ElapsedGameTime.TotalMilliseconds) * ProjectileMoveAmount;
 
        spritePos = new Vector2(spritePosX, spritePosY);
    }
    if (projectileAge > projectileLife)
    {
        active = false;
    }
}
// then in the draw method like this
<pre lang="c#">public void Draw(SpriteBatch spriteBatch)
 {
     //spriteBatch.Draw(projectileSprite, drawRectangle, Color.White);
 
     spriteBatch.Draw(projectileSprite, spritePos, null,
         Color.FromNonPremultiplied(255, 255, 255, 255), spriteHeading,
         spriteRotOrigin, 1.0f, SpriteEffects.None, 0f);
 
 }
Posted
Updated 23-Nov-17 18:02pm
v2

1 solution

I solved the problem with the following code
if (keyboard.IsKeyDown(Keys.Space) && shoot)
 {
     shoot = false;

     this.fire.Play(.2f, .9f, 0);

     // the code below will position a projectile at the top
     // of any rotating sprite facing the sprite direction

     lazerX = spritePosX + HeroSprite.Width / 2 * (float)Math.Sin(rotationAngle);
     lazerY = spritePosY - HeroSprite.Height / 2 * (float)Math.Cos(rotationAngle);

     Projectile projectile = new Projectile(heroLazer, "heroLazer",
         lazerX, lazerY, rotationAngle);

     Game1.AddProjectile(projectile);
 }


and then with this code

public void update(GameTime gameTime, KeyboardState keyboard)
 {
     //projectile active or not?
     if (active == true)
     {
         projectileAge += gameTime.ElapsedGameTime.Milliseconds;

         ProjectilePosX += (float)(Math.Sin(spriteHeading) *
             gameTime.ElapsedGameTime.TotalMilliseconds) * ProjectileMoveAmount;
         ProjectilePosY -= (float)(Math.Cos(spriteHeading) *
             gameTime.ElapsedGameTime.TotalMilliseconds) * ProjectileMoveAmount;

         spritePos = new Vector2(ProjectilePosX, ProjectilePosY);
     }
     if (projectileAge > projectileLife)
     {
         active = false;
     }
 }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900