1 Replies - 1016 Views - Last Post: 09 April 2017 - 01:08 PM

#1 AnonymousUser1508  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 29-April 16

Move a list of vertices using Keyboard xna

Posted 31 March 2017 - 02:23 AM

I know this might seem extremely simple but I am having difficulty moving a sprite that has been drawn through vertices. Every time I click on the arrow key it moves by the desired amount but then moves straight back. I was wondering if the way I am trying to move the vertiices can be done. I want movement for the shape before I start tackling SAT. The thing it does at the moment is it recognizes the keyboard click and the output does say it has been moved but as soon as I release the key it moves back to the position before it was clicked.
I have a polygon class which is responsible for getting edges and it contains the list for the vertices etc but nothing responsible for the movement.

Thanks in advance for any help.

Game1 class

 


        Vector2 position;
        KeyboardState previousKey;
        Vector3 velocity;
        Polygon p1;
        private void buildTestShape()
        {
            p1.testAddVertex(new VertexPositionColor(new Vector3(60, 60, 0), Color.Red));
            p1.testAddVertex(new VertexPositionColor(new Vector3(10, 60, 0), Color.Red));
            p1.testAddVertex(new VertexPositionColor(new Vector3(10, 10, 0), Color.Red));
            p1.testAddVertex(new VertexPositionColor(new Vector3(60, 10, 0), Color.Red));
                
            polygons.Add(p1);
        }

        public void move(KeyboardState ks)
        {
                for (int i = 0; i < p1.Vertice.Count; i++)
                {
                    Vector2 _pos;
                    _pos.X = p1.Vertice[i].Position.X;
                    _pos.Y = p1.Vertice[i].Position.Y;
                    position = _pos;
                }

                if (ks.IsKeyDown(Keys.Left) == true)
                {

                    position.X -= 2;
                }
                if (ks.IsKeyDown(Keys.Right) == true)
                {

                    position.X += 10;
                }
                if (ks.IsKeyDown(Keys.Down) == true)
                {

                    position.Y += 2;
                }
                if (ks.IsKeyDown(Keys.Up) == true)
                {

                    position.Y -= 2;
                }
            
          }

    protected override void Update(GameTime gameTime)
    {

          KeyboardState currentKey = Keyboard.GetState();
          move(currentKey);
          for (int i = 0; i < p1.Vertice.Count; i++)
          {
             var pos = p1.Vertice[i].Position;
             pos += velocity * (float)gameTime.ElapsedGameTime.TotalMilliseconds / 10;
          }
    previousKey = currentKey;
    }




Then they are being drawn in the draw method using BasicEffect

Is This A Good Question/Topic? 0
  • +

Replies To: Move a list of vertices using Keyboard xna

#2 basteagui  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 60
  • Joined: 08-September 13

Re: Move a list of vertices using Keyboard xna

Posted 09 April 2017 - 01:08 PM

this is a bit odd my friend.

let me ask you why you are trying to move the vertices?

you should probably be using matrix.createtranslation for that.

you can move the vertices as well but it's unnecessary. and if i were doing it, it would give me a headache.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1