Game Development Community

Follow the bouncing ball

by Jon Jorajuria · in Torque Game Builder · 07/13/2006 (8:02 am) · 2 replies

I have a slight issue. I want to create a ball that has a consistant bounce to it. Currently the ball uses physics and bounces correctly until I move it right or left. When I move the ball right or left, the ball sometimes loses height. I also have a freeze function where I can stop the ball from bouncing, but it too has the problem of losing height.

Is there any way return the ball to a consistant height? The following is myball code:

function playerBall::onLevelLoaded(%this, %scenegraph)
{
//set the player's ball name to the instance
$pBall = %this;
moveMap.bindCmd(keyboard, "a", "pBallLeft();", "pBallLeftStop();");
moveMap.bindCmd(keyboard, "d", "pBallRight();", "pBallRightStop();");
moveMap.bindCmd(keyboard, "space", "pBallFreeze();", "pBallFreezeStop();");
}

function playerBall::updateMovement(%this)
{
   if(%this.moveLeft)
   {
   %this.setLinearVelocityX( -$pBall.hSpeed );
   }
   if(%this.moveRight)
   {
   %this.setLinearVelocityX( $pBall.hSpeed );
   }   
   if(%this.freeze)
   {
   %this.setLinearVelocityY( 0 );
   %this.setConstantForceY( 0 );
   }      
   if(!%this.moveLeft && !%this.moveRight)
   {
   %this.setLinearVelocityX( 0 );
   }   
   if(!%this.freeze)
   {
   %this.setLinearVelocityY( 0 );
   %this.setConstantForceY( 200 );
   }
}

function pBallLeft()
{
$pBall.moveLeft = true;
$pBall.updateMovement();
}
function pBallRight()
{
$pBall.moveRight = true;
$pBall.updateMovement();
}
function pBallLeftStop()
{
$pBall.moveLeft = false;
$pBall.updateMovement();
}
function pBallRightStop()
{
$pBall.moveRight = false;
$pBall.updateMovement();
}
function pBallFreeze()
{
$pBall.freeze = true;
$pBall.updateMovement();
}
function pBallFreezeStop()
{
$pBall.freeze = false;
$pBall.updateMovement();
}

#1
07/13/2006 (11:26 am)
It might be possible to achieve with some proper settings, but the advanced physics aren't meant for reliable replication. Youl'd be better off writing your own little custom physics that bounce the ball around according to some simplistic gravity and reflection, or setting the velocities based on predefined heights. That's something you could pull off entirely in script too, it's not a huge deal.
#2
07/16/2006 (8:58 am)
I'm working on something that uses bouncing at a consistent height, too. Make sure the friction, damping etc are all set to zero so that they don't affect the velocity of your bouncing object. One thing I seem to be doing differently to you is that I actually set a vertical force upwards on the object when it hits the ground or a platform. This makes sure it will always bounce the same height each time.