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

  • Home
  • SEARCH
  • 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 8124803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:35:05+00:00 2026-06-06T06:35:05+00:00

I’m trying to draw a square in XNA and simply move it around with

  • 0

I’m trying to draw a square in XNA and simply move it around with the keyboard, based on user input. Unfortunately, I haven’t even been able to get the square to show – I’m positive I’m missing something here, but I can’t figure out exactly what it is. I plan to create a complicated hierarchy of primitive shapes which I can use in my game. The main components of the Shape class are initialized, and the deriving class (in this case Rect) utilizes these components for further use, and configures them.

What am I doing wrong here?

Code

Shape.cs

public abstract class Shape
    {
        public Vector3 Center;
        public Color[] Colors;
        public bool SetColorsOnUpdate;

        public virtual Rectangle BoundingBox { get; set; }
        public float Radius { get { return mRadius; } }

        protected VertexBuffer mVertexBuf;
        protected IndexBuffer mIndexBuf;
        protected float mRadius;
        protected BasicEffect mShader;

        public Shape(Vector3 center, Color[] colors, int numVertices, float radius)
        {
            Colors = colors;
            Center = center;
            mVertexBuf = new VertexBuffer(Graphics.MainDevice, typeof(VertexPositionColor), numVertices, BufferUsage.WriteOnly);
            mIndexBuf = new IndexBuffer(Graphics.MainDevice, IndexElementSize.SixteenBits, numVertices, BufferUsage.WriteOnly);
            SetColorsOnUpdate = true;
            mShader = new BasicEffect(Graphics.MainDevice);
            mRadius = radius;
        }

        public abstract void Update();
        public abstract void Draw();
    }

Rect.cs

public class Rect : Shape
{
    public override Rectangle BoundingBox
    {
        get
        { 
            int x = (int)Center.X, y = (int)Center.Y;
            int diameter = (int)mRadius * 2;

            return new Rectangle(
                x, y,
                x + diameter, 
                y + diameter
            );
        }
    }

    const float TestRectZCoordinate = 0;
    const int NumVertices = 4;

    public Rect(Vector3 center, Color[] colors, float radius)
        : base(center, colors, NumVertices, radius)
    {
        if (colors.Length < NumVertices)
            throw new IndexOutOfRangeException(string.Format("Color array passed to Rect constructor MUST have an element index size of 4. Current length passed is {0}", colors.Length));

        mShader.VertexColorEnabled = true;

        mVertexBuf.SetData<VertexPositionColor>(
                new VertexPositionColor[]
                {
                    new VertexPositionColor(new Vector3(Center.X + mRadius, Center.Y + mRadius, TestRectZCoordinate), Colors[0]),
                    new VertexPositionColor(new Vector3(Center.X + mRadius, Center.Y - mRadius, TestRectZCoordinate), Colors[1]),
                    new VertexPositionColor(new Vector3(Center.X - mRadius, Center.Y - mRadius, TestRectZCoordinate), Colors[2]),
                    new VertexPositionColor(new Vector3(Center.X - mRadius, Center.Y + mRadius, TestRectZCoordinate), Colors[3])
                }
            );

        mIndexBuf.SetData<short>(new short[] { 0, 1, 2, 3 });
    }

    public override void Update()
    {
        //TODO
    }

    public override void Draw()
    {
        mShader.World = Matrix.CreateWorld(Center, Vector3.Forward, Vector3.Up);
        mShader.CurrentTechnique.Passes[0].Apply();

        Graphics.MainDevice.SetVertexBuffer(mVertexBuf);
        Graphics.MainDevice.Indices = mIndexBuf;

        Graphics.MainDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, NumVertices, 0, 1);
    }
}

Update

Here is the latest Rect.Draw() implementation.

public override void Draw()
    {
        Viewport viewport = Graphics.MainDevice.Viewport;

        mShader.World = Matrix.CreateWorld(Center, Vector3.Forward, Vector3.Up);
        mShader.View = Matrix.CreateLookAt(new Vector3(viewport.Width / 2, viewport.Height / 2, -5f), Center, Vector3.Up);
        mShader.CurrentTechnique.Passes[0].Apply();

        Graphics.MainDevice.SetVertexBuffer(mVertexBuf);
        Graphics.MainDevice.Indices = mIndexBuf;

        Graphics.MainDevice.DrawIndexedPrimitives(PrimitiveType.LineStrip, 0, 0, NumVertices, 0, 2);
    }
  • 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-06T06:35:08+00:00Added an answer on June 6, 2026 at 6:35 am

    Ah – I think I see at least one of your problem(s). You’re drawing a TriangleList (see PrimitiveType) and you need 6 indices, not 4.

    Got it. It looks like your vertex/index order was wrong. I also didn’t see a projection matrix.

    Here’s the code that worked great for me – all I did was to add a projection matrix, reorder the vertices, change the indices and fix the primitive count.

    http://pastebin.com/tMKCxBLL

    Hope this helps!

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

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I need to clean up various Word 'smart' characters in user input, including but
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I have a small JavaScript validation script that validates inputs based on Regex. I
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group

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.