Latest

Archive

Community news

C++

Communities and Content

Databases

Editorials

Emacs

General

HTML

Java

Notices

PHP

XML

Apache

C++

Database

General

HTML

Java

Javascript

Linux

Object oriented programming

Open source

Perl

PHP

Python

Ruby

SOAP

XML

Suggest a link

Advertise on zez

Contribute

Contact us

About zez


Creating games with QCanvas




BounceCanvasView


The BounceCanvasView has a QTimer that is started when we pick up the ball. It will emit a signal every 10 ms until we drop the ball. The signal is connected to the timeout() slot, which records the current mouse position and place it in a queue of the 10 most recent mouse positions. When the ball is released, we use this queue to calculate an average velocity of the mouse movement for the last 100 ms. Then the ball is thrown using this value. This gives a nice simulation of a throw, we can make the ball go fast or slow, or just drop it.

BounceCanvasView::contentsMouseReleaseEvent


This is what happens when the mouse is released. (See the last page of this article for the full source code.)


void BounceCanvasView::contentsMouseReleaseEvent( QMouseEvent* )
{
    if ( Moving )    // if we are holding the ball
    {
        Timer->stop();    // stop the timer

        if ( MousePosQueue.count() )  // if we have recorded mouse positions...
        {
            // ...then calculate average velocity
            int count = MousePosQueue.count();
            QPoint velocity( 0, 0 );
            QPoint pos;
            QPoint prevPos = *MousePosQueue.head();

            while ( MousePosQueue.count() )
            {
                pos = *MousePosQueue.dequeue();
                velocity += pos - prevPos;
                prevPos = pos;
            }

            // set the balls new velocity
            Moving->setXVelocity( velocity.x() / count );
            Moving->setYVelocity( velocity.y() / count );
        }
        else  // if we haven't recorded anything (fast click)
        {
            // set zero velocity (we drop the ball)
            Moving->setXVelocity( 0 );
            Moving->setYVelocity( 0 );
        }

        Moving->setZ( 1 );
        canvas()->setAdvancePeriod( 10 );  // restart the animation
        Moving = 0;     // clear the pointer, we are no longer holding the ball
    }
}


Attached files:


<< Previous page | 1 | 2 | < 3 > | 4 | 5 | 6 | Next page >> | Printer-friendly page |

Comment List


Topic: Author:
Time:
Very much Informative Riphat satti 29.08.2007 10:47

I think this is very useful exapme to learn about qcanvas..

each and every thing describe in full detail.
a layman can easily understand this example.


Bounce Canvas Demo Dr M 15.05.2005 05:57

Bounce is a very nicely written demo.
I wish QT would also distribute it
on their CD as an example on how to
write a QCanvas Application.

The example given in the QT book is
way too much for a newcomer.


Very usefull Arun Sivakumar 01.12.2001 17:18

The article featuring the QCanvas was really usefull and simple to understand. Hope you release more article covering various other features of Qt.

Please do inform if there are any other site with equally good articles on Qt.

Thank you
s_arunn@yahoo.com




Forgot your password?

Register a new user

Results

Polls