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

Author Topic: [SOLVED]Can't get a smooth motion (First Time)  (Read 3138 times)

0 Members and 1 Guest are viewing this topic.

TheBaldPanda

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
[SOLVED]Can't get a smooth motion (First Time)
« on: October 02, 2013, 05:38:23 am »
I am making a pong clone for a first project, but so far the paddle's movement choppy, not laggy, but just skips from one place to another. Try this out and please tell me how to fix this.

#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

float ballSpeed = 400.0f;
float paddleSpeed = 10000.0f;
float ballVelX = 10;
float ballVelY = 10;


sf::Clock cloc;
sf::Time tim;


bool isPlaying = true;

sf::Vector2i screenSize(800,600);

int main()
{

    sf::CircleShape pongBall;
    //sf::CircleShape::
    pongBall.setFillColor(sf::Color::Red);
    pongBall.setRadius(25.0f);
    pongBall.setPosition(400, 300);

    sf::RectangleShape paddle1;
    // sf::RectangleShape::
    paddle1.setFillColor(sf::Color::Yellow);
    paddle1.setSize(sf::Vector2f(50, 250));
    paddle1.setPosition(0, 175);

    sf::RectangleShape paddle2;
    // sf::RectangleShape::
    paddle2.setFillColor(sf::Color::Yellow);
    paddle2.setSize(sf::Vector2f(50, 250));
    paddle2.setPosition(750, 175);

    pongBall.setPosition(400, 300);
    paddle1.setPosition(1, 250);
    paddle2.setPosition(784, 250);

    float t = rand() % 1;
    float w = rand() % 1;



    sf::RenderWindow window(sf::VideoMode(screenSize.x, screenSize.y), "Pro");

    float deltaTime = cloc.restart().asSeconds();

    cloc.restart();

    while(window.isOpen()){
        sf::Event event;

        while(window.pollEvent(event)){
            if(event.type == sf::Event::Closed){
                window.close();
            }
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::W)){
                paddle1.move(0, paddleSpeed * cloc.getElapsedTime().asSeconds());
                if(paddle1.getPosition().y < 0){



                paddle1.setPosition(15, 0);
                }
                if(paddle1.getPosition().y > window.getSize().y){
                    paddle1.setPosition(10, 10);
                }
            }
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)){
                paddle1.move(0,  paddleSpeed * cloc.getElapsedTime().asSeconds());
                if(paddle1.getPosition().y > 491)
                paddle1.setPosition(1, 490);

                cout << paddle1.getPosition().y << endl;
                     if(paddle1.getPosition().y > window.getSize().y){
                    paddle1.setPosition(10, 10);
                }
        }

            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)){


            }

        }
    window.clear();
    window.draw(pongBall);
    window.draw(paddle1);
    window.draw(paddle2);
    window.display();

}




}

 
« Last Edit: October 02, 2013, 10:43:19 pm by TheBaldPanda »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Can't get a smooth motion (First Time)
« Reply #1 on: October 02, 2013, 05:46:34 am »
Don't mix real-time inputs and event handling.
Read the linked tutorials (again), especially pay attention to when you should use what and where you should use what. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

TheBaldPanda

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Can't get a smooth motion (First Time)
« Reply #2 on: October 02, 2013, 05:57:08 am »
Don't mix real-time inputs and event handling.
Read the linked tutorials (again), especially pay attention to when you should use what and where you should use what. :)

Sorry, but I am pretty new. Where would I find that?

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Can't get a smooth motion (First Time)
« Reply #3 on: October 02, 2013, 06:00:32 am »
He literally just linked you to the tutorials.  Just click on his links.

BaneTrapper

  • Full Member
  • ***
  • Posts: 213
  • Do you even see this, i dont need it.
    • View Profile
    • Email
Re: Can't get a smooth motion (First Time)
« Reply #4 on: October 02, 2013, 02:48:24 pm »
The choppy movement can be eliminated simply by passing int for position of sprite,rectangle...
Make two floats
Code: [Select]
float posX, posY;
Instead of updating the sprite directly, update posX, and posY.
Before drawing sprite update its position from posX, posY as following.
Code: [Select]
sprite.setPosition(static_cast<int>(posX), static_cast<int>(posY));
BaneTrapperDev@hotmail.com Programing, Coding
Projects: Not in development(unfinished/playable):
http://en.sfml-dev.org/forums/index.php?topic=11073.msg76266#msg76266
UP and in Development: The Wanderer - Lost in time
http://en.sfml-dev.org/forums/index.php?topic=14563.0

 

anything