Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved Help with QPainter arc math

    Game Development
    1
    2
    108
    Loading More Posts
    • 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.
    • S
      sammonius last edited by

      I'm trying to make a ball-character that shows scrolling arcs when it moves to create the illusion of a 3d ball. The ball is drawn as an 80x80 pixmap with this code, (f is a global variable that refers to the frame count):

      QPixmap *Character::getPixmap(){
      	QPixmap *pixmap = new QPixmap(80, 80);
      	pixmap->fill(Qt::transparent);
      	
      	QPainter painter(pixmap);
      	painter.setRenderHint(QPainter::Antialiasing);
      	painter.setRenderHint(QPainter::SmoothPixmapTransform);
      	
      	painter.setBrush(QBrush(QColor(255, 255, 255));
      	painter.drawEllipse(QRectF(0, 0, 79, 79));
      	
      	for(int i = (f % 10); i <= 80; i += 10){
      		if(i<50) painter.drawArc(0, i, 79, 80-i*1.5, 0, 180*16);
      		else painter.drawArc(0, 50-(i-50), 79, (i-55)*2, 180*16, 180*16);
      	}
      	
      	painter.end();
      	return pixmap;
      }
      

      The main loop draws a green background and then the character at a rate of about 30fps, and the result looks like this: <YouTube link>

      Can anyone help me adjust the arc animation? (It doesn't really matter how many arcs there are, although a version that uses a macro to determine the arc number would be very appreciated!)

      1 Reply Last reply Reply Quote 0
      • S
        sammonius last edited by sammonius

        Sorry, I should have provided some code to test it.

        #include <QtWidgets>
        
        unsigned long f = 0;
        
        class Character : public QWidget {
        public:
        	Character();
        	~Character();
        	
        	QPixmap *getPixmap();
        protected:
        	void paintEvent(QPaintEvent *e);
        };
        
        Character::Character() : QWidget(){}
        Character::~Character(){}
        
        void Character::paintEvent(QPaintEvent *event){
        	Q_UNUSED(event);
        
        	QPainter painter(this);
        	painter.setRenderHint(QPainter::Antialiasing);
        	painter.setRenderHint(QPainter::SmoothPixmapTransform);
        	
        	painter.setBrush(QBrush(QColor(255, 255, 255)));
        	painter.drawEllipse(QRectF(0, 0, 79, 79));
        	
        	for(int i = (f % 10); i <= 80; i += 10){
        		if(i<50) painter.drawArc(0, i, 79, 80-i*1.5, 0, 180*16);
        		else painter.drawArc(0, 50-(i-50), 79, (i-55)*2, 180*16, 180*16);
        	}
        	
        	painter.end();
        	f++;
        }
        
        int main(int argc, char *argv[]){
        	QApplication app(argc, argv);
        	Character *c = new Character();
        	
        	QTimer t;
        	QObject::connect(&t, &QTimer::timeout, c, static_cast<void(QWidget::*)()>(&QWidget::update));
        	t.start(30);
        	
        	c->show();
        	return app.exec();
        }
        
        1 Reply Last reply Reply Quote 0
        • First post
          Last post