Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Collision detection using CPeriodic to control movement

From Wiki
Jump to: navigation, search
Article Metadata
Tested with
Devices(s): Nokia E61i
Nokia E90 Communicator
Nokia N95 8GB
Keywords: CPeriodic, Math::Random(), TRect::Intersects()
Created: tapiolaitinen (20 Mar 2008)
Last edited: hamishwillee (19 Aug 2012)



This snippet demonstrates how to implement the logic for basic collision detection in Symbian C++, which is useful, for example, in simple games. In the snippet, five blocks are drawn onto the screen and sent moving. Collisions against walls and other blocks are detected. Moving is handled with CPeriodic.

In this snippet, the code is implemented in a container control named CAppView. The control is derived from CCoeControl.

Header file

const TInt KBlocks = 5;
const TInt KBlockWidth = 10;
const TInt KBlockHeight = 10;
const TInt KBlockMaxSpeed = 3;
const TInt KDelay = 10000;
const TInt KInterval = 10000;
* Static callback function for timer, called periodically.
* @param aPtr pointer to this class
* @return one of S60 error codes

static TInt TimerCallBack(TAny* aPtr);
* Timer function called from TimerCallBack.
* Takes care of moving the blocks.
* @return one of S60 error codes

TInt DoTimer();
// Data
TPoint iPosition[KBlocks]; // positions for blocks
TPoint iSpeed[KBlocks]; // speeds for blocks
TRgb iColor[KBlocks]; // colors for blocks
CPeriodic* iTimer;

Source file

#include <e32math.h>
void CAppView::ConstructL(const TRect& aRect)
// Create a window for this application view
// Create a periodic timer to move the blocks
iTimer = CPeriodic::NewL(EPriorityNormal);
// Initialize the blocks
TInt i;
for (i = 0; i < KBlocks; i++)
iSpeed[i] = TPoint(
Math::Random() % (2 * KBlockMaxSpeed + 1) - KBlockMaxSpeed,
Math::Random() % (2 * KBlockMaxSpeed + 1) - KBlockMaxSpeed);
iPosition[i] = TPoint(
Math::Random() % aRect.Width(),
Math::Random() % aRect.Height());
iColor[i] = TRgb(
Math::Random() % 256,
Math::Random() % 256,
Math::Random() % 256);
// Start the timer
iTimer->Start(KDelay, KInterval, TCallBack(TimerCallBack, this));
// Set the window size
// Activate the window, which makes it ready to be drawn
TInt CAppView::TimerCallBack(TAny* aPtr)
return ((CAppView*)aPtr)->DoTimer();
TInt CAppView::DoTimer()
// Get gc to draw with
CWindowGc& gc = SystemGc();
// Handle each block in turn
for (TInt i = 0; i < KBlocks; i++)
TPoint& position = iPosition[i];
TPoint& speed = iSpeed[i];
// Move blocks
position = position + speed;
TRect rect = Rect();
// Detect block colliding against walls
if ((position.iX + KBlockWidth >= rect.iBr.iX) ||
(position.iX <= rect.iTl.iX))
speed.iX = -speed.iX;
position = position + speed;
if ((position.iY + KBlockHeight >= rect.iBr.iY) ||
(position.iY <= rect.iTl.iY))
speed.iY = -speed.iY;
position = position + speed;
// Detect blocks colliding against each other
TRect boxRect(position, TSize(KBlockWidth, KBlockHeight));
for (TInt j = 0; j < KBlocks; j++)
TRect boxRect2(iPosition[j], TSize(KBlockWidth, KBlockHeight));
if ((i != j) && (boxRect.Intersects(boxRect2)))
speed.iY = -speed.iY;
speed.iX = -speed.iX;
// Draw current block
return KErrNone;


Five blocks are bouncing on the screen.

Version Hint

Windows Phone: [[Category:Windows Phone]]
[[Category:Windows Phone 7.5]]
[[Category:Windows Phone 8]]

Nokia Asha: [[Category:Nokia Asha]]
[[Category:Nokia Asha Platform 1.0]]

Series 40: [[Category:Series 40]]
[[Category:Series 40 1st Edition]] [[Category:Series 40 2nd Edition]]
[[Category:Series 40 3rd Edition (initial release)]] [[Category:Series 40 3rd Edition FP1]] [[Category:Series 40 3rd Edition FP2]]
[[Category:Series 40 5th Edition (initial release)]] [[Category:Series 40 5th Edition FP1]]
[[Category:Series 40 6th Edition (initial release)]] [[Category:Series 40 6th Edition FP1]] [[Category:Series 40 Developer Platform 1.0]] [[Category:Series 40 Developer Platform 1.1]] [[Category:Series 40 Developer Platform 2.0]]

Symbian: [[Category:Symbian]]
[[Category:S60 1st Edition]] [[Category:S60 2nd Edition (initial release)]] [[Category:S60 2nd Edition FP1]] [[Category:S60 2nd Edition FP2]] [[Category:S60 2nd Edition FP3]]
[[Category:S60 3rd Edition (initial release)]] [[Category:S60 3rd Edition FP1]] [[Category:S60 3rd Edition FP2]]
[[Category:S60 5th Edition]]
[[Category:Symbian^3]] [[Category:Symbian Anna]] [[Category:Nokia Belle]]

This page was last modified on 19 August 2012, at 23:50.
72 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.