Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Player position  (Read 1735 times)

0 Members and 1 Guest are viewing this topic.

JohnyBro

  • Newbie
  • *
  • Posts: 8
    • View Profile
Player position
« on: May 09, 2017, 11:44:59 am »
Hi

This is my RenderWindow
window = new RenderWindow(new VideoMode(1920, 1080), "SFML");

And when I set the position of my player at 1920/2 x 1080/2 he is at the bottom right of the window and not in the center, why ?

Here is my player code

class Player : Transformable, Updatable, Drawable, HasFloatRect
    {
        Vector2f size;
        Texture texture;
        float speed = 300;
        VertexArray vertexArray;

        public Player()
        {
            texture = RessourceManager.Instance.createTexture("player.png");
            size = new Vector2f(texture.Size.X, texture.Size.Y);
            Origin = new Vector2f(size.X / 2, size.Y / 2);
            vertexArray = new VertexArray(PrimitiveType.Quads, 4);
            Position = new Vector2f(960f, 540f);
        }

        public void Draw(RenderTarget target, RenderStates states)
        {
            states.Transform *= Transform;
            states.Texture = texture;
            target.Draw(vertexArray, states);
        }

        public void Update(Time elapsedTime)
        {
            checkInput(elapsedTime);
            updateVertexArray();
        }

        private void updateVertexArray()
        {
            vertexArray[0] = new Vertex(Position, new Vector2f(0, 0));
            vertexArray[1] = new Vertex(Position + new Vector2f(size.X, 0), new Vector2f(texture.Size.X, 0));
            vertexArray[2] = new Vertex(Position + new Vector2f(size.X, size.Y), new Vector2f(texture.Size.X, texture.Size.Y));
            vertexArray[3] = new Vertex(Position + new Vector2f(0, size.Y), new Vector2f(0, texture.Size.Y));
        }

        private void checkInput(Time elapsedTime)
        {
            Vector2f direction = new Vector2f(0, 0);

            if (Keyboard.IsKeyPressed(Keyboard.Key.W))
            {
                direction += new Vector2f(0, -1);
            }
            if (Keyboard.IsKeyPressed(Keyboard.Key.A))
            {
                direction += new Vector2f(-1, 0);
            }
            if (Keyboard.IsKeyPressed(Keyboard.Key.S))
            {
                direction += new Vector2f(0, 1);
            }
            if (Keyboard.IsKeyPressed(Keyboard.Key.D))
            {
                direction += new Vector2f(1, 0);
            }

            MyMath.normalize(ref direction);
            Position += direction * speed * elapsedTime.AsSeconds();
        }

        public FloatRect getAABB()
        {
            return new FloatRect(Position, new Vector2f(size.X, size.Y));
        }
    }

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Player position
« Reply #1 on: May 09, 2017, 12:57:57 pm »
Are you changing the view? Maybe setting the centre to (0, 0)?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

JohnyBro

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Player position
« Reply #2 on: May 09, 2017, 06:06:36 pm »
No im using the default view.

Here is my github if you need the code https://github.com/JohnyBro/Danmaku

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Player position
« Reply #3 on: May 09, 2017, 10:21:34 pm »
Where does your AABB end up?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

JohnyBro

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Player position
« Reply #4 on: May 09, 2017, 10:48:15 pm »
« Last Edit: May 09, 2017, 10:50:41 pm by JohnyBro »

JohnyBro

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Player position
« Reply #5 on: May 10, 2017, 05:54:25 am »
Ok I found why it wasn't working correctly.

I use a vertexArray for drawing my player and the position of each verticies is the position of the player and in the Draw() method I did this

states.Transform *= Transform;

So the position of each verticies (the player's position) was calculated twice.