Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8945567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:16:42+00:00 2026-06-15T12:16:42+00:00

public class dgc_Spaceship : DrawableGameComponent { Texture2D shipTexture; Rectangle spriteRectangle; SpriteBatch spriteBatch; // bool

  • 0
public class dgc_Spaceship : DrawableGameComponent
{

    Texture2D shipTexture;
    Rectangle spriteRectangle;
    SpriteBatch spriteBatch;
    //
    bool horizisSelected = false;
    bool rotateSelected = false;
    bool fireisSelected = false;

    SpriteFont Count;

    //Controls for ship
    dgc_TriggerHorizontal triggerHorizantal;



    Vector2 spritePosition;
    Vector2 spriteOrigin;
    Vector2 direction;


    float rotation = 0.1f;
    Random x, y;


   List<Missle> missleCollection = new List<Missle>();
   List<Asteriod> asteriodCollection = new List<Asteriod>();


    Missle missle;

    Asteriod asteriod;

    public dgc_Spaceship(Game game)
        : base(game)
    {
        // TODO: Construct any child components here


        triggerHorizantal = new dgc_TriggerHorizontal(game);
        triggerHorizantal.triggerHorizantal += new EventHandler(triggerHorizantal_Handler);
        triggerHorizantal.triggerRotate +=new EventHandler(triggerHorizantal_triggerRotate);
        triggerHorizantal.triggerFire +=new EventHandler(triggerHorizantal_triggerFire);
        Game.Components.Add(triggerHorizantal);
        missle = new Missle(game);
        asteriod = new Asteriod(game);

        for (int i = 0; i < 2; i++)
        {
            missleCollection.Add(new Missle(game));
        }


        for (int i = 0; i < 2; i++)
        {
            asteriodCollection.Add(new Asteriod(game));
        }


      }




    /// <summary>
    /// Allows the game component to perform any initialization it needs to before starting
    /// to run.  This is where it can query for any required services and load content.
    /// </summary>
    public override void Initialize()
    {
        // TODO: Add your initialization code here

        base.Initialize();
    }

    protected override void LoadContent()
    {

        spriteBatch = new SpriteBatch(GraphicsDevice);
        shipTexture = Game.Content.Load<Texture2D>(@"Sprites\spaceship");
        spritePosition = new Vector2(450,320);
        missle.LoadContent();

        Count = Game.Content.Load<SpriteFont>(@"Sprites\Count");
        //
        x = new Random();
        y = new Random();

        foreach (Asteriod display in asteriodCollection)
        {
            display.LoadContent();

            int xVec = x.Next(0, 200), yVec = y.Next(0, 200);
            display.position = new Vector2(xVec, yVec);
        }





        base.LoadContent();
    }

    /// <summary>
    /// Allows the game component to update itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    public override void Update(GameTime gameTime)
    {
        // TODO: Add your update code here

        spriteRectangle = new Rectangle((int)spritePosition.X, (int)spritePosition.Y, shipTexture.Width, shipTexture.Height);

        spriteOrigin = new Vector2(spriteRectangle.Width / 2, spriteRectangle.Height / 2);

        direction = new Vector2((float)Math.Cos(rotation), (float)Math.Sin(rotation));


        if (horizisSelected)
        {
           spritePosition.X-= 0.5f;
           spritePosition.Y-= 0.5f;

        }


        if (rotateSelected)
        {               
            rotation += 0.1f;
           // missle.rotateSprite(); 
            spritePosition += direction * 20 * gameTime.ElapsedGameTime.Milliseconds;
        }

        if (fireisSelected)
        {
            missle.update();
        }



        base.Update(gameTime);
    }

    public override void Draw(GameTime gameTime)
    {

        spriteBatch.Begin();
      //  missle.draw(spriteBatch,rotation);
        spriteBatch.Draw(shipTexture,spritePosition,null,Color.White,rotation,spriteOrigin,1f,SpriteEffects.None,0);
      //  spriteBatch.DrawString(Count,asteriodCollection.Count.ToString(),new Vector2(100,100),Color.Red);
        foreach (Asteriod display in asteriodCollection)
        {
            display.Draw(spriteBatch);
        }
         spriteBatch.End();

        base.Draw(gameTime);
    }


    public void triggerHorizantal_Handler(object sender, EventArgs e)
    {
        //Do Stuff
        horizisSelected = !horizisSelected;

    }

    public void triggerHorizantal_triggerRotate(object sender, EventArgs e)
    {
        //Do Stuff
        rotateSelected = !rotateSelected;
    }

    public void triggerHorizantal_triggerFire(object sender, EventArgs e)
    {
        //Do Stuff
        fireisSelected = !fireisSelected;
    }




    }
}

My Problem:

I can’t seem to figure this out. I have 2 functional buttons on screen. When clicked, the ship rotates clockwise and the other moves it forward. A little searching around lead me to the formula with the Math.Sin/Cos, but I don’t think I implemented it properly.

What I need it to do is move the ship in the direction of the rotation.

The way it works:
The user will hit the rotate button and it will rotate ship clockwise. When its in a position the user wants, they hit the rotate button to stop it in that particular direction.

The hit the horizontal button and it will move the ship forward in that direction.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:16:43+00:00Added an answer on June 15, 2026 at 12:16 pm

    your code suggests that you move your sprite in the direction desired when rotate is selected, this needs to be done when horizontal is selected instead, if rotate is selected then your direction should be changed (not every frame)

    EDIT: (5 minutes max)

        if (horizisSelected)
        {
           //spritePosition.X-= 0.5f;
           //spritePosition.Y-= 0.5f;
           spritePosition += direction;// * 20 * gameTime.ElapsedGameTime.Milliseconds;
    
        }
    
    
        if (rotateSelected)
        {               
            rotation += 0.1f;
           // missle.rotateSprite(); 
    
            direction = new Vector2((float)Math.Cos(rotation), (float)Math.Sin(rotation));
            //spritePosition += direction * 20 * gameTime.ElapsedGameTime.Milliseconds;
        }
    

    If your ship is flying away its probably because of the elapsed time (milliseconds), you will have to debug into that

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class tryAnimActivity extends Activity { /** * The thread to process splash screen
public class MyClass { public string MyProperty{ get; set; } Now, I would like
public class Test { public static void main(String[] args) { DemoAbstractClass abstractClass = new
public class Test1<Type> { public Type getCompositeMessage(Type... strings) { Type val = (Type) ;
public class Knowing { static final long tooth = 343L; static long doIT(long tooth)
public class A { private A(int param1, String param2) {} public static A createFromCursor(Cursor
public class WeeklyInspection : Activity { WebView view = (WebView) FindViewById(Resource.Id.inspectionWV); view.Settings.JavaScriptEnabled = true;
public class User { public long Id {get;set;} [References(typeof(City))] public long CityId {get;set;} [????]
public class LinkedList { Object contents; LinkedList next = null; public boolean equals(Object item)
public class HttpPostTask extends AsyncTask<Void, Integer, Void> { TextView txtStatus = (TextView)findViewById(R.id.txtStatus); @Override protected

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.