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 8013803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:43:59+00:00 2026-06-04T19:43:59+00:00

I currently have an asteroid texture loaded as my test player for the game

  • 0

I currently have an asteroid texture loaded as my “test player” for the game I’m writing. What I’m trying to figure out how to do is get a triangle to shoot from the center of the asteroid, and keep going until it hits the top of the screen. What happens in my case (as you’ll see from the code I’ve posted), is that the triangle will show, however it will either be a long line, or it will just be a single triangle which stays in the same location as the asteroid moving around (that disappears when I stop pressing the space bar), or it simply won’t appear at all. I’ve tried many different methods, but I could use a formula here.

All I’m trying to do is write a space invaders clone for my final in C#. I know how to code fairly well, my formulas just need work is all.

So far, this is what I have:

Main Logic Code

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(ClearOptions.Target, Color.Black, 1, 1);

    mAsteroid.Draw(mSpriteBatch);

    if (mIsFired)
    {
        mPositions.Add(mAsteroid.LastPosition);
        mRay.Fire(mPositions);
        mIsFired = false;
        mRay.Bullets.Clear();
        mPositions.Clear();
    }

    base.Draw(gameTime);
}

Draw Code

public void Draw()
{
    VertexPositionColor[] vertices = new VertexPositionColor[3];

    int stopDrawing = mGraphicsDevice.Viewport.Width / mGraphicsDevice.Viewport.Height;

    for (int i = 0; i < mRayPos.Length(); ++i)
    {
        vertices[0].Position = new Vector3(mRayPos.X, mRayPos.Y + 5f, 10);
        vertices[0].Color = Color.Blue;
        vertices[1].Position = new Vector3(mRayPos.X - 5f, mRayPos.Y - 5f, 10);
        vertices[1].Color = Color.White;
        vertices[2].Position = new Vector3(mRayPos.X + 5f, mRayPos.Y - 5f, 10);
        vertices[2].Color = Color.Red;

        mShader.CurrentTechnique.Passes[0].Apply();
        mGraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleStrip, vertices, 0, 1);

        mRayPos += new Vector2(0, 1f);

        mGraphicsDevice.ReferenceStencil = 1;
    }
}
  • 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-04T19:44:01+00:00Added an answer on June 4, 2026 at 7:44 pm

    This isn’t quite how you’re supposed to be manipulating the location of a model in world space and since you’re creating a new vertex array every single draw frame you’ll find that it performs pretty badly when you come to draw more than a few triangles.

    declare the vertices and index list for your triangle just once in the LoadContent method.

    VertexBuffer triangleVertexBuffer;
    IndexBuffer triangleIndexBuffer;
    
    protected override void LoadContent()
    {
        // Setup a basic effect to draw the triangles with.
        mEffect = new BasicEffect(GraphicsDevice);
    
        // setup the triangle vertext buffer and load up it's content.
        triangleVertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionColor), 3, BufferUsage.WriteOnly);
        triangleVertexBuffer.SetData<VertexPositionColor>(new VertexPositionColor[] 
        {
            new VertexPositionColor (new Vector3 (0f, -1f, 0.0f), Color.Blue),  // Top Point
            new VertexPositionColor (new Vector3 (-1f, 1f, 0.0f), Color.White), // Bottom Left
            new VertexPositionColor (new Vector3 (1f, 1f, 0.0f), Color.Red),    // Bottom Right
        });
    
        // setup an index buffer to join the dots!
        triangleIndexBuffer = new IndexBuffer(GraphicsDevice, IndexElementSize.SixteenBits, 3, BufferUsage.WriteOnly);
        triangleIndexBuffer.SetData<short>(new short[]
        { 
            0, 
            1, 
            2, 
        });
    }
    

    After this assuming your effect takes in to account a world transformation (basic effect does) you can use that parameter to move the triangle.

     protected override void Draw(GameTime gameTime)
            {
                GraphicsDevice.Clear(Color.CornflowerBlue);
    
                for (int i = 0; i < mRayPos.Length ; i++)
                {
                    // This is the line that moves the triangle along your ray.
                    mEffect.World = Matrix.CreateTranslation(new Vector3(mRayPos[i].X, mRayPos[i].Y, mRayPos[i].Z));
                    mEffect.Techniques[0].Passes[0].Apply();
    
                    // These lines tell the graphics card that you want to draw your triangle.
                    GraphicsDevice.SetVertexBuffer(triangleVertexBuffer);
                    GraphicsDevice.Indices = triangleIndexBuffer;
                    GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 3, 0, 1);
                }
    
                base.Draw(gameTime);
            }
    

    If you use this method it becomes a very simple operation to rotate or scale your trangle using Matrix.CreateRotation and Matrix.CreateScale.

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

Sidebar

Related Questions

I currently have: Range(Z1).Interior.Color = RGB(255, 255, 255) But this wipes out the borders
Currently have this to get a value from the registry in TSQL. However, I
Currently have a form with a ListView. Users of role A get an image
I currently have a grid made out of square DIVs (200px x 200px each).
I currently have a game loop that fires off about 20 times per second
We currently have an application that sends out SMS to users, it acts like
I currently have the following code to get a specific value i need from
So I am currently trying to make the game Asteroids for a class. The
I currently have a small socket server that I'm trying to convert to a
I currently have a CSV file that I parse and am trying to insert

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.