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




The Ball


This class contains the physical data of the ball (mass and resistance), the hit() function, which determines whether or not we have hit a transparent pixel, and the advance(int) function, which works in two phases. In the first phase, we apply resistance and gravity, then we check whether the ball has hit a wall, and reverse it's velocity it if it has. Finally it is moved to be exactly adjacent to the wall, and further movement in this direction is disabled, since we don't want it to move through the wall. The second phase moves the ball a single step. You don't have to worry about calling advance(int) with the correct arguments, this is done by the canvas.

Ball::advance



void Ball::advance( int phase )
{

    if ( phase == 0 ) // stuff to be done before we move
    {
        // apply resistance to slow down the ball
        setXVelocity( xVelocity() * Air_resistance );
        setYVelocity( yVelocity() * Air_resistance );

        // apply gravity
        setYVelocity( yVelocity() + Mass );

        // right border hit check
        if ( !canvas()->onCanvas( x() + width()/2 + xVelocity(), 1 ) )
        {
            MoveX = false;
            move( canvas()->width() - width()/2, y() );
            setXVelocity( xVelocity() * -1 * Bump_resistance );
        }
        // left border hit check
        else if ( !canvas()->onCanvas( x() - width()/2 + xVelocity(), 1 ) )
        {
            MoveX = false;
            move( 0 + width()/2, y() );
            setXVelocity( xVelocity() * -1 * Bump_resistance );
        }

        // lower border hit check
        if ( !canvas()->onCanvas( 1, y() + height()/2 + yVelocity() ) )
        {
            MoveY = false;
            move( x(), canvas()->height() - height()/2 );
            setYVelocity( yVelocity() * -1 * Bump_resistance );
        }
        // upper border hit check
        else if ( !canvas()->onCanvas( 1, y() - height()/2 + yVelocity() ) )
        {
            MoveY = false;
            move( x(), 0 + height()/2 );
            setYVelocity( yVelocity() * -1 * Bump_resistance );
        }
    }
    else    // time to move
    {
        if ( MoveX && MoveY )
            move( x() + xVelocity(), y() + yVelocity() );
        else if ( MoveX )
            move( x() + xVelocity(), y() );
        else if ( MoveY )
            move( x(), y() + yVelocity() );

        MoveX = MoveY = true;
    }
}


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