Game Development Community

Collision between a Paddle and a Ball

by Wil Klentzeris · in Torque Game Builder · 12/29/2005 (10:49 am) · 6 replies

I have been looking in the forums for an answer to what seems to be a very straight forward problem.

Simple scenerio:
A ball collides with a paddle (PONG/BREAKOUT).

I would like to know what the physics collision settings should be for the 2 objects so that
- upon collision the paddle stays at its x/y position and
- the ball bounces of as it does when it collides as it does with the worldlimit when it is set to bounce

I have tried the following as others have stated but it does not seem to work:
- $player.setCollisionActive( true, true );
- $player.setCollisionPhysics( true, false );

- $ball.setCollisionActive( true, true );
- $ball.setCollisionPhysics( false, true );


datablock fxCollisionMaterialDatablock2D(ball)
{
friction = 0;
restitution = 1;
relaxation = 0.5;
density = 1;
forceScale = 1;
damping = 0;
};

datablock fxCollisionMaterialDatablock2D(player)
{
friction = 0;
restitution = 1;
relaxation = 0.5;
density = 0.00001;
forceScale = 0;
damping = 0.10;
};

Currently the paddle drifts away and the ball stops.

Thanks.

#1
12/29/2005 (11:43 am)
The only stock collision-response that imparts velocity from one object to another is the rigid-body. I presume you are using this response?

If so, are you sure that your "paddle" actually has either a linear/angular velocity or are you just moving its location directly? If you just move its location then it will simply force an overlap on the two objects causing the "ball" to adjust its position away from the "paddle" whilst at the same time reducing its own velocity.

The difference between moving the position/rotation of an object explicitly and having it move implicitly by altering its linear/angular velocities is extremely important. You can move an object and it have no velocity whatsoever; the "rigid" body response will see a stationary object ("paddle") hitting a "ball".

Providing some details on how the "paddle" is being moved would help here.

Cheers,

- Melv.
#2
12/30/2005 (9:20 am)
Melv -

The movement of the paddle is as follows:
new ActionMap(playerMap);

function playerLeft(%val)
{
$l = %val;
}


function playerRight(%val)
{
$r = %val;
}

playerMap.bind(keyboard, left, "playerLeft");
playerMap.bind(keyboard, right, "playerRight");

playerMap.push();

function moveplayer()
{
%x_speed = 1000;

switch ( $l )
{
case 1:
$player.setLinearVelocityX( -%x_speed );
case 0:
$player.setLinearVelocityX( 0 );
}

switch ( $r )
{
case 1:
$player.setLinearVelocityX( %x_speed );
case 0:
if( $l == 0)
{
$player.setLinearVelocityX( 0 );
}
}

schedule( 50, 0, moveplayer);
}

function createPlayer()
{
...
$player.setWorldLimit(clamp, "-482 290 482 290");
movePlayer();
}

The ball movement is initiated as follows:
function createBall()
{
....
$ball.setWorldLimit(bounce, "-482 -350 482 325");
$ball.setImpulseForcePolar( getRandom()*360, 100000000 );
}

Many thanks.

Wil
#3
12/30/2005 (2:00 pm)
I have a code sample that might clear this up, but so far for me it's less than perfect. I have the paddle movement worked out (direct placement, as above, but via the mouse) and the collision settings are enabled properly, so I guess the big question remaining is: What should the exact materials and settings be so that (A) the ball never loses momentum and (B) the paddle can't be pushed by collisions with the ball?
#4
12/31/2005 (6:59 am)
Oh my; what was I thinking when I posted the above? I got it in my head, somehow, that you were talking about a "paddle" as in PinBall! Go figure!

Anyway, forgiving my moment of complete and utter madness...

I think it's best to understand what the "active" and the "physics" do. The "active" controls whether the object actually sends or can receive collisions. The "physics" controls whether there is an autonomous physics response if a collisions is sent (hits something else) or receives (gets hit by something else).

For instance, it wouldn't matter what you put in "setCollisionPhysics()" if you set something like "setCollisionActive(false, false)" because the object can never send/receive collisions so it never even gets to checking whether the object should adjust the physics for sent/received collisions.

Does that make sense?

You'll probably want your ball to send and receive collisions (which is typically the default for most collisions) and you'll probably want it to react to sent/received collisions as well (which is typically the default for most collisions) so you'd use...
$ball.setCollisionActive( true, true );
$ball.setCollisionPhysics( true, true );
For your player paddle, you'll want it to send and received collisions. This means that if you move the paddle into the ball or the ball moves into the paddle then both will work. Again, this is typically the default for most collisions. The only real choice we've finally come down to is whether we want the paddle to react when it sends/receives collisions, something which you say you don't want to do so we just turn both off like so...
$player.setCollisionActive( true, true );
$player.setCollisionPhysics( false, false );
To make the ball use the "bounce" mode, you'll then use...
$ball.setCollisionResponse( bounce );
You'll also probably want to use the collision-detection mode "circle" instead of "poly" so you get nice perfect collisions (because it is a ball). You'll also want to turn-off the "superscribed" mode so you get a "subscrubed" circle.

Now, onto collision-materials. Don't forget that the collision-material datablock is just a quick way of assigning lots of collision details in one go. The important thing here is that different collision responses use some or all of these fields. The rigid-body uses all of them whereas the "bounce" mode only uses the "resitution" (to control how "bouncy" it is) and "damping" (everything uses damping). Also, as a side-note, "relaxation" doesn't exist anymore as of the alpha releases.

Each and every setting in a collision-material has its own script function so that you can be more explicit so you can use...
$ball.setDamping(0);
$ball.setRestitution(1);
... or you if you want to keep the setting in one place then only assign the fields that you need like so...
datablock fxCollisionMaterialDatablock2D(ball)
{
restitution = 1;
damping = 0;
};

Hope this helps,

- Melv.
#5
12/31/2005 (7:03 am)
I'll just add that there's substantial documentation on its way for physics / collisions.

- Melv.
#6
01/16/2006 (12:33 pm)
Mel -

Thanks for the help.

I have been swamped with some other stuff right now but am looking forward to implementing your suggestions.

Thanks again.

Wil