Game Development Community

Paddle collision problems

by David Reynolds · in Torque Game Builder · 10/02/2005 (9:06 am) · 12 replies

Hello,

I've been trying to create a pong/breakout style game to get familiar with T2D. I've managed to find some examples to help me out but I'm having problems with the paddle collision. Right now the ball bounces off the walls as expected, but when it hits the paddle it sticks to it and then the ball starts to move only horizontally (same axis the paddle can move along). How can I get the ball to collide properly with the paddle?

Thanks

// --------------------------------------------------------------------
// Setup T2D Scene.
// --------------------------------------------------------------------
function setupT2DScene()
{
	new fxSceneGraph2D(t2dSceneGraph);
	sceneWindow2D.setSceneGraph( t2dSceneGraph );
	sceneWindow2D.setCurrentCameraPosition( "0 0 80 60" );

	// Bouncy Material.
   datablock fxCollisionMaterialDatablock2D(bouncyMaterial)
   {
      friction = 0;
      restitution = 1.0;
      relaxation = 0.5;
      density = 0.01;
      forceScale = 1;
      damping = 0;
   };  
	CreateWalls();
	CreatePlayer();
	CreateBall();
}

function CreatePlayer()
{
	$player = new fxStaticSprite2D() { scenegraph = t2dSceneGraph; };
	$player.setPosition("0 22");
	$player.setSize( "8 2" );
	$player.setImageMap( tileMapImageMap );

	new ActionMap(playerMap);
	playerMap.bindCmd(keyboard, "left", "playerLeft();", "playerLeftStop();");
	playerMap.bindCmd(keyboard, "right", "playerRight();", "playerRightStop();");
	playerMap.push();
	
	$player.setWorldLimit( clamp, "-39 -39 39 39" );
	$player.setGroup( 1 );
	$player.setLayer( 1 );
	$player.setCollisionActive( false, true );
  $player.setCollisionMasks( BIT(1), BIT(1) );
}

function CreateBall()
{
   $ball = new fxStaticSprite2D() { scenegraph = t2dSceneGraph; };
   $ball.setSize( 4 );
   $ball.setImageMap( tileMapImageMap );
   $ball.setCollisionActive( true, false );
   $ball.setCollisionPhysics( true, false );
   $ball.setCollisionMasks( BIT(1), BIT(1) );
   $ball.setCollisionMaterial( bouncyMaterial );
   $ball.setMaxAngularVelocity( 0 );
		$ball.setGroup( 1 );
		$ball.setLayer( 1 );
   // Give it a random direction.
   $ball.setImpulseForcePolar( getRandom()*360, 400 );
}

function playerLeft()
{
	$player.setLinearVelocityX( -25 );
}

function playerLeftStop()
{
	if ( $player.getLinearVelocityX() < 0 )
		$player.setLinearVelocityX( 0 );
}

function playerRight()
{
	$player.setLinearVelocityX( 25 );
}

function playerRightStop()
{
	if ( $player.getLinearVelocityX() > 0 )
		$player.setLinearVelocityX( 0 );
}

function CreateWalls()
{
   // Setup some immovable walls and allow them to receive collisions only...
   
   // Left.
   %sprite = new fxStaticSprite2D() { scenegraph = t2dSceneGraph; };
   %sprite.setPosition( "-40 0" );
   %sprite.setSize( "2 75" );
   %sprite.setImageMap( tileMapImageMap );
   %sprite.setCollisionActive( false, true );
   %sprite.setImmovable();
   %sprite.setGroup( 1 );
	%sprite.setLayer( 1 );
   // Right.
   %sprite = new fxStaticSprite2D() { scenegraph = t2dSceneGraph; };
   %sprite.setPosition( "40 0" );
   %sprite.setSize( "2 75" );
   %sprite.setImageMap( tileMapImageMap );
   %sprite.setCollisionActive( false, true );
   %sprite.setImmovable();
   %sprite.setGroup( 1 );
	%sprite.setLayer( 1 );
   // Top.
   %sprite = new fxStaticSprite2D() { scenegraph = t2dSceneGraph; };
   %sprite.setPosition( "0 -33" );
   %sprite.setSize( "80 2" );
   %sprite.setImageMap( tileMapImageMap );
   %sprite.setCollisionActive( false, true );
   %sprite.setImmovable();
   %sprite.setGroup( 1 );
	%sprite.setLayer( 1 );
   // Bottom.
   %sprite = new fxStaticSprite2D() { scenegraph = t2dSceneGraph; };
   %sprite.setPosition( "0 33" );
   %sprite.setSize( "80 2" );
   %sprite.setImageMap( tileMapImageMap );
   %sprite.setCollisionActive( false, true );
   %sprite.setImmovable();
   %sprite.setGroup( 1 );
	%sprite.setLayer( 1 );
   
}

#1
10/02/2005 (10:37 am)
When the ball hits the paddle, invert the ball Y movement in the onCollision callback.
#2
10/02/2005 (10:43 am)
He's using collisionPhysics, though, so velocity twiddling in onCollision shouldn't be necessary.

Now, I'm not entirely sure, as it's been a while since I messed with any sort of physics interaction in T2D, but I get the feeling that your paddle and ball should be both sending and receiving physics responses. I think.
#3
10/02/2005 (10:51 am)
Thanks for the replies. Teck I'm assuming I'd have to disable the sending and receiving of physics responses in the paddle. How would I go about doing that?
#4
10/02/2005 (12:23 pm)
I added:

$player.setCollisionPhysics(false,true);

which now makes the ball behave like it should, but at the same time my paddle gets knocked off screen. Anyway to prevent the paddle from falling off the screen?
#5
10/05/2005 (10:26 am)
The paddle needs collision physics to send but not receive: (true, false)

The ball needs collision physics to receive but not send: (false, true)


Hope this helps
#6
12/07/2005 (7:29 am)
Related to this...

I'm trying to get a couple of added behaviors and I'm new to the scripting language so haven't found any obvious way to accomplish it.

- First, I want the paddle to move with the mouse, but I want the mouse motion to be translated into velocity, so the faster you move the paddle, the harder you hit the ball in the opposite direction. Where in the physics modeling can I insert this kind of behavior?

- Second, I want to suppress the spinning of the ball, because my graphics have a nice specular highlight on the ball and it looks quite bad when it spins. How can I intercept the collision to prevent this spinning behavior?

Do either of these concepts require subclassing the basic sprite in C++?
#7
12/09/2005 (3:47 am)
Aha. The "MoveTo" demo was very helpful. I ended up doing a variation on it which works quite nicely, and does the things a paddle should. The trickiest part was figuring out in what manner to send and receive physics, but that's just cuz I'm a noob.

I still haven't constructed a means to suppress the spinning behavior, but that might be moot anyhow. I want to have spinning balls in my demo / game but I want to suppress the spinning of the mounted sprite on top of the ball, which is going to contain a specular reflection. I imagine that will be even trickier, and near as I can tell it will require delving into the C++ after all. I just hope it translates well to 1.1....
#8
12/09/2005 (4:22 am)
To suppress spinning, have you tried putting this in your CreatePlayer function:
$player.setMaxAngularVelocity( 0 );
#9
12/09/2005 (5:52 am)
Thanks! That helpful function hadn't caught my attention in my study of the docs so far.

I just had a look at the mount method and everything came together in a flash. Now I allow the ball to spin as it will, but the specular highlight mounted on the object is kept still. It looks really convincing. Now I just need a slightly better looking highlight and I'll be top of the pops. I guess I can still use setMaxAngularVelocity for the ones which have a solid color, since they look the same at all angles....

My tyrant of a designer ;-) now wants me to have the specular highlight always point towards the center, but I think that might look a bit odd with the highlight style we're currently using. Nevertheless, it'll be an interesting challenge to figure out how to make this happen. I suppose I can just attach a schedule to each of the highlights and have them reorient, then set a new schedule, ad infinitum. But I wonder what the overhead cost of this will be. I'll try keeping the frequency relatively low, like 20Hz.

Wow, this thread has gone a bit astray from the original topic....

To get back to the paddle following the mouse, I must say the Mouse Selection and Following tutorial has been a great resource. My code still uses the method laid out in the MoveTo tutorial, but the proposition of using the onUpdateScene callback is interesting. I'll have to give it a try and compare. Time for a check-in!
#10
12/28/2005 (12:49 am)
David -

Where you able to resolve your issue using the setCollision functions?

I have a similiar situation where a ball hits a paddle and have not had any luck.

I tried the suggestions above with no luck. When the ball hits the paddle either the ball clings to the paddle or the paddle drifts away, depending on the collison settings.

Thanks.
#11
12/28/2005 (7:18 am)
Wil I never did get this working. I stopped trying to figure it out as 1.1 was near but haven't had time to get back at it cause of the holidays.
#12
12/29/2005 (9:23 am)
David -

Thanks. I am going to post a new request. Maybe someone can shed some light for us.