Game Development Community

Stop a ball

by Harrison Brock · in Torque Game Builder · 08/21/2006 (6:58 pm) · 3 replies

I have a ball and two pad. When the game start the ball is on pad_1 when the user click on the ball it move to pad_2 and it should stop but the ball just move back to the left.

here is the code:

$yVelocity = 20;
$xVelocity = 25;
$ballAngularVelocity = 60;


function ball::onAdd(%this)
{
%this.setUseMouseEvents(true);

}

function ball::onMouseDown(%this, %modifier, %worldPosition, %mouseClicks)
{
%this.setLinearVelocity($xVelocity,$yVelocity);
%this.setAngularVelocity($ballAngularVelocity);
%this.setForwardMovementOnly(true);



}


I know that I need to set $xVelocity,$yVelocity, and $ballAngularVelocity to 0 when the ball hit the pad. How should I do this.

#1
08/21/2006 (8:32 pm)
This assumes you have a scripted object named pad, and ball is the script name, of class balls. If you do that then you could have multiple balls of class balls and the code would still work when you clicked on them.
function pad::onCollision( %srcObj, %dstObj, %srcRef, %dstRef, %time, %normal,%contactCount, %contacts )
{
if (%dstObj.class$="balls")
   {
     %dstObj.setLinearVelocity(0,0);
     %dstObj.setAngularVelocity(0);
    }
}
#2
08/21/2006 (8:33 pm)
Hmm I hope thats not too confusing. =/
#3
08/21/2006 (8:43 pm)
Alternatively, you could just use

%dstObj.setAtRest();

which set's both the Angular and Linear velocitie's to 0 for you.