Game Development Community

Breakout Physics

by Lennart Steinke · in Torque Game Builder · 03/14/2005 (2:25 am) · 6 replies

Hi!

I'm currently trying to get something similar to breakout physics (ie. perfect reflection, no speed loss on impact).
My problem is: Whenever the ball hits a tile, the tile disappears (good) and the ball gets slower (bad).

$player.setMaxAngularVelocity(0);  
    $player.setMinAngularVelocity(0);
    $player.setCollisionCallback( true );
    $player.setCollisionMasks( BIT(2), BIT(2) );
    $player.setCollisionMaterial( standardMaterial );
    $player.setCollisionPhysics(true, false);
    $player.setCollisionActive(true, true);
    $player.setRestitution(1);
    $player.setDamping(0);
    $player.setFriction(0);

    //--------------------
    %blockLayer.setCollisionActive(true, true);
    %blockLayer.setCollisionMaterial(immovableMaterial);
    %blockLayer.setCollisionMasks(BIT(2), BIT(2));
    %blockLayer.setCollisionCallback(true);
    %blockLayer.setCollisionPhysics(true, false);
    %blockLayer.setDebugOn( BIT(1) | BIT(5) );
    %blockLayer.setCollisionMasks( BIT(1), BIT(1) );
    
    %blockLayer.setRestitution(1);


//
// and here the collision function
function fxSceneObject2D::onCollision( %srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts ) {
     if (%srcObj != $player) {
        %srcObj.clearTile(%srcRef);
    }
}

Any idea what could cause the slowdown of the ball?

Thanks in advance,

Lenny

#1
03/14/2005 (2:35 am)
Well the default immovableMaterial looks like this:

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

I think you have to modify the values. I'm not sure what they all do, but you could try setting relaxation to 0.0!
#2
03/14/2005 (3:37 am)
Lennart,

I thought it might be easier if I just give you a simple code example of something, so here it is...

function pongBounce()
{
	// 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();
	// Right.
	%sprite = new fxStaticSprite2D() { scenegraph = t2dSceneGraph; };
	%sprite.setPosition( "40 0" );
	%sprite.setSize( "2 75" );
	%sprite.setImageMap( tileMapImageMap );
	%sprite.setCollisionActive( false, true );
	%sprite.setImmovable();
	// Top.
	%sprite = new fxStaticSprite2D() { scenegraph = t2dSceneGraph; };
	%sprite.setPosition( "0 -33" );
	%sprite.setSize( "80 2" );
	%sprite.setImageMap( tileMapImageMap );
	%sprite.setCollisionActive( false, true );
	%sprite.setImmovable();
	// Bottom.
	%sprite = new fxStaticSprite2D() { scenegraph = t2dSceneGraph; };
	%sprite.setPosition( "0 33" );
	%sprite.setSize( "80 2" );
	%sprite.setImageMap( tileMapImageMap );
	%sprite.setCollisionActive( false, true );
	%sprite.setImmovable();
	
	// Bouncy Material.
	datablock fxCollisionMaterialDatablock2D(bouncyMaterial)
	{
		friction = 0;
		restitution = 1.0;
		relaxation = 0.5;
		density = 0.01;
		forceScale = 1;
		damping = 0;
	};	
	
	// Create Bouncy Ball.
	%ball = new fxStaticSprite2D() { scenegraph = t2dSceneGraph; };
	%ball.setSize( 4 );
	%ball.setImageMap( tileMapImageMap );
	%ball.setCollisionActive( true, false );
	%ball.setCollisionPhysics( true, false );
	%ball.setCollisionMasks( BIT(0), BIT(0) );
	%ball.setCollisionMaterial( bouncyMaterial );
	%ball.setMaxAngularVelocity( 0 );
	
	// Give it a random direction.
	%ball.setImpulseForcePolar( getRandom()*360, 400 );
}

Hope this helps,

- Melv.
#3
03/14/2005 (4:32 am)
@Lennart

er actually the ball should vary in speed depending on the speed the bat is moving at when it hits the ball. Play Arkanoids some time to see what I mean.....

@Melv

What physics would you recommend for that type of speed increase, decrease?
#4
03/14/2005 (4:40 am)
Quote:Lennart

er actually the ball should vary in speed depending on the speed the bat is moving at when it hits the ball. Play Arkanoids some time to see what I mean....
I'm not actually coding a breakout clone. Right now there is just a ball and some tiles that are supposed to disappear. I just want to get a feel for the physics and the tile engine at the moment.

@Melv: Thanks a lot! :)
I guess I can setup my tilemap in the same way as you did set-up the walls?
#5
03/14/2005 (5:02 am)
Absolutely but there are some steps you can skip.

The difference with tiles is that they are immovable by their very nature so you don't need to do anything special there apart from just have their collisions on and the collision-polygons defined. The physics system (currently) uses the source-objects material details to calculate the reaction. The exception being the point at which the reaction energy is distributed. This is when the immovable (density = 0) property is handled. What this means is that when your object collides with a tile, it's your source object (ball?) which determines the exact response.

The important properties in creating a 'perfect' rebound are:-

Source Restution = 1
Source Friction = 0
Destination Density = 0 (Immovable)

NOTE:- Later in the year, we want to look at a new method for handling tiles so that they become another virtual scene and tiles can react just like any scene object would. This would lead to movable tiles e.g. bidirectional collisions and lots of other good stuff. Bit of a pipe dream at the moment but it is all possible and definatley a nice to have. The old rigid handling of tiles is getting pretty dated these days and it'd be nice to see if we couldn't bring something new to the table. We'll see. I guess we should get this one working perfectly first. ;)

Hopefully I've not missed anything here, I'm pretty tired today.

- Melv.
#6
03/14/2005 (2:30 pm)
Ooooo interesting stuff, which I was wanting to look into for myself, but you explained it so nicely I may just play around with this instead =-)