Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Game Development
  4. How to animate chracter ?
Forum Updated to NodeBB v4.3 + New Features

How to animate chracter ?

Scheduled Pinned Locked Moved Unsolved Game Development
3 Posts 3 Posters 622 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hello guys I have a game screen and have a chracter. I want make animate moving my chracter but how ? Can anyone share their experience with this?

    alt text

    JKSHJ 1 Reply Last reply
    0
    • ? A Former User

      Hello guys I have a game screen and have a chracter. I want make animate moving my chracter but how ? Can anyone share their experience with this?

      alt text

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      @NullByte said in How to animate chracter ?:

      I want make animate moving my chracter but how ?

      By using Sprites: https://doc.qt.io/qt-5/qtquick-effects-sprites.html

      Here's an example app: https://doc.qt.io/qt-5/qtdoc-demos-maroon-example.html

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      1
      • 8Observer88 Offline
        8Observer88 Offline
        8Observer8
        wrote on last edited by
        #3

        Sprite animation using QPainter:

        You can download the "sprites-cat-running.png" sprite sheet here: https://plnkr.co/edit/zjYT0KTfj50MejT9?preview

        main.cpp

        #include <QtWidgets/QApplication>
        #include <QtWidgets/QWidget>
        #include <QtGui/QPainter>
        #include <QtGui/QImage>
        #include <QtCore/QTimer>
        #include <QtCore/QElapsedTimer>
        #include <QtCore/QDebug>
        
        #define N_FRAMES 8
        
        class Window : public QWidget
        {
            Q_OBJECT
        public:
            Window()
            {
                setWindowTitle("Qt C++");
                resize(512, 256);
                // Download a sprite sheet here:
                // https://plnkr.co/edit/zjYT0KTfj50MejT9?preview
                m_spriteSheet.load(":/Sprites/sprites-cat-running.png");
                int index = 0;
                for (int i = 0; i < 2; i++ )
                {
                    for (int j = 0; j < 4; j++)
                    {
                        m_frames[index] = QPoint(j * m_sw, i * m_sh);
                        qDebug() << m_frames[index];
                        index++;
                    }
                }
                connect(&m_timer, &QTimer::timeout, this, &Window::animationLoop);
                m_timer.start(1000.f/60.f);
                m_elapsedTimer.start();
            }
        private:
            QTimer m_timer;
            QElapsedTimer m_elapsedTimer;
            float m_deltaTime;
            float m_animationTime = 0.f;
            const float m_animationSpeed = 100.f;
            QImage m_spriteSheet;
            QPoint m_frames[N_FRAMES];
            int m_frameIndex = 0;
            int m_x, m_y;
            const int m_sw = 512, m_sh = 256; // Source image width and height
        private slots:
            void animationLoop()
            {
                m_deltaTime = m_elapsedTimer.elapsed();
                m_elapsedTimer.restart();
        
                m_animationTime += m_deltaTime;
        
                if (m_animationTime > m_animationSpeed)
                {
                    m_animationTime = 0;
                    m_x = m_frames[m_frameIndex].x();
                    m_y = m_frames[m_frameIndex].y();
                    m_frameIndex++;
                    if (m_frameIndex >= N_FRAMES)
                    {
                        m_frameIndex = 0;
                    }
                }
        
                update();
            }
        private:
            void paintEvent(QPaintEvent *event)
            {
                Q_UNUSED(event);
                QPainter qp(this);
                qp.drawImage(0, 0, m_spriteSheet, m_x, m_y, m_sw, m_sh);
            }
        };
        
        #include "main.moc"
        
        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
            Window w;
            w.show();
            return a.exec();
        }
        

        a302b780-34ce-4ae6-9219-ad76c844ee66.gif

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved