# pong in C++(irrlicht), problem with ball rolling algorithm

Mostly out of a lack of inspiration I decided to Wright a pong clone, the result of which can be found here(Linux bin in root, source code is included for users of other platforms)

the main reason im positing here is I’ve run into a problem with making the ball roll in the direction its moving. currently all I’ve managed to achieve is the ball wobbling around randomly. douse anyone here know how to make the ball roll correctly?

Could it be possible that you’re doing the rotation in local coordinates instead in world coordinates?

Im using

``````node -&gt; setRotation(vector3df)
``````

which is global as far as im aware, though please correct me if im wrong (node->setPosition() is unafected by setRotation)

hares the code that calculating the rotations, basicly it divides up the circumference into angle steps, and works out how meany steps to rotate the ball on the x and z acsisses.

``````
void movable_ball::move_ball(core::vector3df loc)
{
if(switchh == 0)
switchh = 1;
else
switchh = 0;
// set position
ball-&gt;setPosition(loc);

// calculate movement delta
core::vector3df mov_delta = loc - old_loc;

// calculate rotation for X accsis
if(switchh == 1)
{
for(int angle = -30; angle &lt; 31; angle ++)
{
float dist_moved_min = angle_step * angle;
float dist_moved_max = angle_step * (angle +1);

if(mov_delta.X &gt; dist_moved_min && mov_delta.X &lt; dist_moved_max)
{
// fix speed bug when angle is negative
if(mov_delta.X &lt; 0)
angle += 1;
// apply rotation
core::vector3df ball_rot = ball-&gt;getRotation();
ball-&gt;setRotation(core::vector3df(ball_rot.X,ball_rot.Y,ball_rot.Z -= angle));
break;
}
}
} // end X acsiss rotation

else
{
// calculate rotation for Y accsis
for(int angle = -30; angle &lt; 31; angle ++)
{
float dist_moved_min = angle_step * angle;
float dist_moved_max = angle_step * (angle +1);

if(mov_delta.Z &gt; dist_moved_min && mov_delta.Z &lt; dist_moved_max)
{
// fix speed bug when angle is negative
if(mov_delta.Z &lt; 0)
angle += 1;
// apply rotation
core::vector3df ball_rot = ball-&gt;getRotation();
ball-&gt;setRotation(core::vector3df(ball_rot.X += angle,ball_rot.Y,ball_rot.Z));
break;
}
}
// save old location
old_loc    = loc;
}
}
``````

But I like it

Sorry I can’t help with your problem though…

Just to sort out some terminology:

C and its childs (c++, c#) are not scripting languages… It is an imperative/object oriented compiled/interpreted language

lisp, html, php and so on would be scripting languages.

Ah, okay, It was just a ‘figure of speech’.

Now don’t go ranting about ‘figures of speech’!!

technically speaking HTML is a markup language (hyper text markup language), JavaScript however is a scripting language;)

however, this thread was originally about fixing the problem with my ball rolling algorithm, can we get back on topic?

Can’t get the game to do anything weird (doesn’t respond to my input, and the ball moves fine in the first bit of its trip), so this is a bit of a guess.

Are you using a game engine for this? If so, maybe it’s got a physics engine? This would make the rotations you apply add forces to the ball, moving it around.

j = down, k = up (like vim )

the ball moves fine, but doesn’t roll in the direction its moving, sometimes it actually rolls backwards.