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

Author Topic: Animation speed way to fast.  (Read 5721 times)

0 Members and 2 Guests are viewing this topic.

OlivierVanHoudt

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Animation speed way to fast.
« on: November 24, 2017, 12:17:35 pm »
Hi guys! i'm very new to C++ and trying to set up a walk animation but when i run the program my animation is way to fast.

I'm currently following a tutorial how to make an animation, but i dont know how to alter the speed. The maker of the tutorial is not responding to my questions so thats why i'm here ;)

Can you guys help me find the right alteration to my code to make my animation slow down?

My code looks like this:

main.ccp:
int main()
{
        RenderWindow window(VideoMode(600, 400), "first window made in C++", Style::Close | Style::Titlebar | Style::Resize);
       
        RectangleShape player1(Vector2f(50.0f, 50.0f));
        player1.setPosition(150.0f, 300.0f);
        Texture playerTexture;
        playerTexture.loadFromFile("baldricfrontwalksheet_01.png");
        player1.setTexture(&playerTexture);

        Animation animation(&playerTexture, Vector2u(9, 4), 0.3f);

        float deltaTime = 0.0f;
        Clock clock;
       
        while(window.isOpen())
        {
                Event evnt;
                while (window.pollEvent(evnt))
                {
                        deltaTime = clock.restart().asSeconds();

                        switch (evnt.type)
                        {
                        case Event::Closed:
                                window.close();
                                break;

                        case Event::Resized:
                                printf("New window width: %i New window height %i", evnt.size.width, evnt.size.height);
                                break;

                                // Input possible
                        case Event::TextEntered:

                                if (evnt.text.unicode < 128)
                                {
                                        printf("%c", evnt.text.unicode);
                                }

                        }

                        if (evnt.type == evnt.Closed)
                        {
                                window.close();
                        }


                }
                /* Player1 movement */
                if (Keyboard::isKeyPressed(Keyboard::Key::A))
                {
                        player1.move(-0.1f, 0.0f);
                }

                if (Keyboard::isKeyPressed(Keyboard::Key::D))
                {
                        player1.move(0.1f, 0.0f);
                }

                if (Keyboard::isKeyPressed(Keyboard::Key::W))
                {
                        player1.move(0.0f, -0.1f);
                }

                if (Keyboard::isKeyPressed(Keyboard::Key::S))
                {
                        player1.move(0.0f, 0.1f);
                }
               
               

                /*
                if (Mouse::isButtonPressed(Mouse::Left))
                {
                        Vector2i mousePos = Mouse::getPosition(window);
                        player.setPosition(static_cast<float>(mousePos.x), static_cast<float>(mousePos.y));
                }
                */


                animation.Update(3, deltaTime);
                player1.setTextureRect(animation.uvRect);
                window.clear();
                window.draw(player1);
                /*window.draw(player2);*/
                window.display();
        }
       
    return 0;
}

animation.ccp
Animation::Animation(Texture* texture, Vector2u imageCount, float switchTime)
{
        this->imageCount = imageCount;
        this->switchTime = switchTime;
        totalTime = 0.0f;
        currentImage.x = 0;

        uvRect.width = int(texture->getSize().x / float(imageCount.x));
        uvRect.height = int(texture->getSize().y / float(imageCount.y));

}


Animation::~Animation()
{
}

void Animation::Update(int row, float deltaTime)
{
        currentImage.y = row;
        totalTime += deltaTime;

        if (totalTime >= switchTime)
        {
                totalTime -= switchTime;
                currentImage.x++;

                if (currentImage.x >= imageCount.x)
                {
                        currentImage.x = 0;
                }
        }
        uvRect.left = currentImage.x * uvRect.width;
        uvRect.top = currentImage.y * uvRect.height;


}

Animation.h:
class Animation
{
public:
        Animation(Texture* texture, Vector2u imageCount, float switchTime);
        ~Animation();

        void Update(int row, float deltaTime);



public:
        IntRect uvRect;


private:
        Vector2u imageCount;
        Vector2u currentImage;

        float totalTime;
        float switchTime;


};

Greetings,

Olivier
« Last Edit: November 24, 2017, 12:28:45 pm by eXpl0it3r »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Animation speed way to fast.
« Reply #1 on: November 24, 2017, 12:38:03 pm »
Quote
Animation(Texture* texture, Vector2u imageCount, float switchTime);
I didn't check, but it looks so obvious that it is switchTime :P
Laurent Gomila - SFML developer

OlivierVanHoudt

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Animation speed way to fast.
« Reply #2 on: November 24, 2017, 12:53:21 pm »
Quote
Animation(Texture* texture, Vector2u imageCount, float switchTime);
I didn't check, but it looks so obvious that it is switchTime :P

But how can i alter it correctly?

if i change the switchTime from 0.1 to something else and run the program, it wil first run slower but after moving the player (with w,a,d or s) it will start to run fast again.
« Last Edit: November 24, 2017, 01:01:30 pm by OlivierVanHoudt »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Animation speed way to fast.
« Reply #3 on: November 24, 2017, 01:20:01 pm »
Quote
after moving the player (with w,a,d or s) it will start to run fast again.
Is "moving the player" just a call to player1.move? If that's the case, then I can't imagine how moving a sf::RectangleShape may have any effect on the playback speed of an Animation instance.

If there's other code that you're not showing, then please consider showing it ;)
Laurent Gomila - SFML developer

OlivierVanHoudt

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Animation speed way to fast.
« Reply #4 on: November 24, 2017, 01:39:49 pm »
moving the player refers to moving sf::RectangleShape.

Im showing all the code i have :/

when the sf::RectangleShape is 'idle' the animation runs normal speed, but when i move the sf::RectangleShape  it speeds up super fast.

Another question:

How comes my  sf::RectangleShape only moves when i move my mouse. When i press D or A (while not touching the mouse) the  sf::RectangleShape wont move. When im moving my mouse around on the program ( without clicking annything) and then press A or D the  sf::RectangleShape moves.
« Last Edit: November 24, 2017, 02:35:42 pm by OlivierVanHoudt »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Animation speed way to fast.
« Reply #5 on: November 24, 2017, 02:45:59 pm »
Oh, that's because your deltaTime is updated in the event loop. That should be done outside, in the main loop.
Laurent Gomila - SFML developer

OlivierVanHoudt

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Animation speed way to fast.
« Reply #6 on: November 25, 2017, 10:58:20 am »
Thanks your solution worked!

May have another question but its kinda different subject:

i have declared a function called ResizingView but when i use the function in my main it wont recognize the view input.

how can i fix this?

The code looks as followed:

#include "stdafx.h"
#include <SFML\Graphics.hpp>
#include <iostream>
#include "Player.h"
#include "Platform.h"

using namespace sf;
using namespace std;

static const float VIEW_HEIGHT = 200.0f;


/* resize view*/
void ResizingView(const sf::RenderWindow& window, sf::View& view)               <---------- DECLARE VIEW
{
   float aspectRatio = float(window.getSize().x) / float(window.getSize().y);
   view.setSize(VIEW_HEIGHT* aspectRatio, VIEW_HEIGHT);
}


int main()
{
   RenderWindow window(VideoMode(600,600), "Kollhodt The Game", Style::Close | Style::Titlebar | Style::Resize);
   View view1(Vector2f(0.0f, 0.0f), Vector2f(VIEW_HEIGHT, VIEW_HEIGHT));
   
   Texture playerTexture;
   playerTexture.loadFromFile("baldricfrontwalksheet_01.png");
   
   Player player(&playerTexture, Vector2u(9, 4), 0.1f, 60.0f);
   Texture platformTexture;
   platformTexture.loadFromFile("MakingMap2.png");

   Platform platform1(&platformTexture, Vector2f(20.0f, 1200.0f), Vector2f(200.0f, 230.0f));

   float deltaTime = 0.0f;
   Clock clock;
   
   while (window.isOpen())
   {
      deltaTime = clock.restart().asSeconds();

      Event evnt;
      while (window.pollEvent(evnt))
      {
         switch (evnt.type)
         {
         case Event::Closed:
            window.close();
            break;
         case Event::Resized:
            ResizingView(window, view);               <------- IT WONT RECOGNIZE VIEW
            break;

         }
      }

      

   
      player.Update(deltaTime);
      
      platform1.GetCollider().CheckCollider(player.GetCollider(), 0.0f);

      view1.setCenter(platform1.GetPositionPlatform());
      /*
      view2.setCenter(player.GetPosition2());
      */

      window.clear();

      window.setView(view1);
      /*
      window.setView(view2)
      */
      

      player.Draw(window);
      platform1.Draw(window);
      window.display();

      
      
   }
   
   
   return 0;
}
« Last Edit: November 25, 2017, 11:01:55 am by OlivierVanHoudt »

achpile

  • Full Member
  • ***
  • Posts: 231
    • View Profile
    • Achpile's homepage
    • Email
Re: Animation speed way to fast.
« Reply #7 on: November 25, 2017, 11:39:24 am »
because you didn't declared variable called "view"

OlivierVanHoudt

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Animation speed way to fast.
« Reply #8 on: November 25, 2017, 11:48:33 am »
I did right? its a const? how can i properly fix it? because if i call const View& view it will give me an error.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Animation speed way to fast.
« Reply #9 on: November 25, 2017, 12:13:43 pm »
The sf::View instance that you want to pass to the function is named view1. view is the name of the function argument, it is not accessible outside the function.
Laurent Gomila - SFML developer

 

anything