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

Author Topic: actions happened too fast ??  (Read 2482 times)

0 Members and 1 Guest are viewing this topic.

Flaze07

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
    • Email
actions happened too fast ??
« on: July 30, 2017, 04:22:33 pm »
hi, I have a sfml program where it is comprised of pixel...it is a battle of pixel and it moved according to a set speed...but the way it moved when i run it is instantaneously moved...

main.cpp
#include <TGUI/TGUI.hpp>
#include "pixels/pixels.hpp"
#include <iostream>

int main()
{

    const float speed = 0.01;
    sf::RenderWindow win{sf::VideoMode{450, 400}, "pixel vs pixel"};

    Pixels p1{sf::Color::Red, sf::Vector2f{200, 400}, sf::Vector2f{0, 0}, (200 * 400) / 2, speed};
    Pixels p2{sf::Color::Blue, sf::Vector2f{200, 400}, sf::Vector2f{250, 0}, (200 * 400) / 2, speed};

    while (win.isOpen())
    {
        sf::Event event;
        while (win.pollEvent(event))
        {
            if (event.type == sf::Event::Closed) win.close();
        }

        p1.update(p2);
        p2.update(p1);

        win.clear();
        win.draw(p1);
        win.draw(p2);
        win.display();
    }
}

 

Pixels.hpp
class Pixels : public sf::Drawable, public sf::Transformable
{
private :
    virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
    {
        states.transform *= getTransform();
        states.texture = 0;
        target.draw(m_vertices, states);
    }
    sf::VertexArray m_vertices;
    const float speed = 0;
public :
    Pixels(sf::Color color, sf::Vector2f start, sf::Vector2f end, int numbers, float pspeed);
    void update(Pixels& pixels);
    sf::Vector2f& getPoint(size_t index);
    size_t getPointCount();
};
 

pixels.cpp
#include <cstdlib>
#include <ctime>
#include "pixels.hpp"

Pixels::Pixels(sf::Color color, sf::Vector2f start, sf::Vector2f end, int numbers, float pspeed) :
    speed(pspeed)
{
    srand(time(0));
    m_vertices.setPrimitiveType(sf::PrimitiveType::Points);
    m_vertices.resize((size_t) numbers);
    for (size_t i = 0; i < m_vertices.getVertexCount(); ++i)
    {
        m_vertices[i].color = color;
        float temp_x = (rand() % (int) start.x) + (int) end.x;
        rand();
        float temp_y = (rand() % (int) start.y) + (int) end.y;
        m_vertices[i].position = sf::Vector2f{temp_x, temp_y};
    }
}

void Pixels::update(Pixels& pixels)
{
    for (int i = 0; i < m_vertices.getVertexCount(); ++i)
    {
        if (i >= pixels.getPointCount())
        {
            if (getPoint(i).x < pixels.getPoint(pixels.getPointCount() - 1).x)
            {
                m_vertices[i].position = sf::Vector2f{m_vertices[i].position.x + speed, m_vertices[i].position.x};
            }
            else if (getPoint(i).x > pixels.getPoint(pixels.getPointCount() - 1).x)
            {
                m_vertices[i].position = sf::Vector2f{m_vertices[i].position.x - speed, m_vertices[i].position.x};
            }
            if (getPoint(i).y < pixels.getPoint(pixels.getPointCount() - 1).y)
            {
                m_vertices[i].position = sf::Vector2f{m_vertices[i].position.y, m_vertices[i].position.y + speed};
            }
            else if (getPoint(i).y > pixels.getPoint(pixels.getPointCount() - 1).y)
            {
                m_vertices[i].position = sf::Vector2f{m_vertices[i].position.y, m_vertices[i].position.y - speed};
            }
            continue;
        }
        if (getPoint(i).x < pixels.getPoint(i).x)
        {
             m_vertices[i].position = sf::Vector2f{m_vertices[i].position.x + speed, m_vertices[i].position.x};
        }
        else if (getPoint(i).x > pixels.getPoint(i).x)
        {
            m_vertices[i].position = sf::Vector2f{m_vertices[i].position.x - speed, m_vertices[i].position.x};
        }
        if (getPoint(i).y < pixels.getPoint(i).y)
        {
             m_vertices[i].position = sf::Vector2f{m_vertices[i].position.y, m_vertices[i].position.y + speed};
        }
        else if (getPoint(i).y > pixels.getPoint(i).y)
        {
            m_vertices[i].position = sf::Vector2f{m_vertices[i].position.y, m_vertices[i].position.y - speed};
        }
    }
}

sf::Vector2f& Pixels::getPoint(size_t index)
{
    return m_vertices[index].position;
}

size_t Pixels::getPointCount()
{
    return m_vertices.getVertexCount();
}

 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: actions happened too fast ??
« Reply #1 on: July 30, 2017, 04:27:49 pm »
And what's the question?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Flaze07

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
    • Email
Re: actions happened too fast ??
« Reply #2 on: July 30, 2017, 04:40:58 pm »
it moved too fast...
when I didn't put update... the pixels are in random position
when I put update I expect to see the movement of it...instead it turns into this diagonal line with half red and half blue

Hapax

  • Hero Member
  • *****
  • Posts: 3346
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: actions happened too fast ??
« Reply #3 on: July 30, 2017, 08:05:00 pm »
Is it possible that the speed is just too high?
Considering that it's likely to perform thousands of updates per second, it might just have updated too much.

You can first try to set a framerate limit for the window:
win.setFramerateLimit(60);

To relatively reliably control speed, the speed should be multiplied by a "delta time" - the time that passed since the previous frame.

As for the resulting positions, it's not clear whether these positions (diagonals) are the intended end result or not.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Martin Sand

  • Newbie
  • *
  • Posts: 23
    • View Profile
    • Email
Re: actions happened too fast ??
« Reply #4 on: July 31, 2017, 10:19:27 am »
You should consider to change to a time-based game loop: https://gafferongames.com/post/fix_your_timestep/