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. Help with QPainter arc math
Qt 6.11 is out! See what's new in the release blog

Help with QPainter arc math

Scheduled Pinned Locked Moved Unsolved Game Development
2 Posts 1 Posters 740 Views
  • 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 Offline
    S Offline
    sammonius
    wrote on last edited by
    #1

    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
    0
    • S Offline
      S Offline
      sammonius
      wrote on last edited by sammonius
      #2

      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
      0

      • Login

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