Game Development Community

Bouncing ball like a pong pall?

by Max Thomas · in Torque Game Engine · 11/30/2004 (7:42 am) · 5 replies

Ok,

I don't want to use the rigid shape class to start off. I want to have a ball that will bounce around in an interior at a fairlt high speed and when it hits you it will kill you dead. I have managed to get a few things working but the ball allways crashes the game at some point or it just reacts to it's surondings wierdly. Please help, I am in dire need of this to be working.

Max :)

#1
12/01/2004 (9:51 am)
Anyone?

This is very inportant... Help would be greatly appreciated.

Thanks, Max
#2
12/01/2004 (12:28 pm)
Max,

You could use an Item and have the onCollsion() method do the killing.

Create it as a non-static:
function ItemData::create(%DB)
{
    echo("ItemData::create("@%DB@")");
    %obj = new Item() 	{
        dataBlock	= %DB;
        collideable = true;
        static		= false; 
        rotate	= false;
    };
    return %obj;
}

Make a bouncyBall definition:
datablock ItemData( bouncyBall ) {
    //-------------------------------------------------------------------------
    // ItemData Fields
    //-------------------------------------------------------------------------
    lightType      = "None";
    sticky           = false; 
    mass            = 1.0;
    elasticity       = 1.0;
    maxVelocity  = 1000; // Play with this to get desired effect
    friction          = 0.0;
    //-------------------------------------------------------------------------
    // From ShapeBase
    //-------------------------------------------------------------------------
    shapeFile      = "~/data/Shapes/ path to ball shape file";
};

Add an onCollision() method:
// ******************************************************************
//			onCollision() Callback
// ******************************************************************
function bouncyBall::onCollision( %colliderDB , %colliderObj , %collidedObj, %vec, %speed) {
    %callerDBName = %colliderDB.getName();
    echo("bouncyBall::onCollision( " @%callerDBName @" , " @ %colliderObj @ " , " @%collidedObj @ " , \"" @%vec @ "\" , " @%speed @ " )");

    // 
    // %collidedObj is a player kill it or damage it.
    //
    if( "Player" $= %collidedObj.getClassName() )    {
        %colliderDB.dosomething <- code here isup to you.
    }
}
#3
12/01/2004 (1:03 pm)
Thank you very very much. One more question though. I want to have the ball start off at a certain speed and and just keep going that speed and never stop. I tried changing the mass to 0 and 0.000001 but when I shoot the ground near the box it crashes the game. The projectile has impulse applyed to it so that is prolly what is causing it.

Thanks, Max
#4
12/01/2004 (2:39 pm)
Max,

You've got a few different things going on there:

1. Regarding impulses and instantaneous changes in velocity. The engine does not handle (well) changes where the magnitude of the change is greater than 4000 (or is it 40000, can't recall right now, but this is in the guide). That is, if you try to apply to apply an impulse with too great a value, you will hang or crash the engine. Its a math thing.

2. Really small masses. You can't apply an impulse or have a moving object make a collision if it has zero mass. This will crash the engine. Thus, your mass of 0.000001 is the problem. You probably ran into a rounding error that cause the mass to round to 0, or you may have just gone below the 'satisfactory' threshold. One can't know without pulling out the debugger and stepping into the code.

3. Getting a shape to move at the same velocity continuously....

You've got a couple of hacky solutions available to you and third better solution:

a. As a child of shapeBase/Data, you may get and set the velocity of the item directly with getVelocity() and setVelocity(). Now, once the item is in motion, you could periodically (say on each collision), get the velocity vector, convert it to a unit vector, and then scale it so that the magnitude is your required velocity.
- Is this a good idea? No, not really, but it might work.

b. Each item has an elasticity field. This field is NOT limited to 1.0 It can be 2, 3, etc. Thus, any easier solution would be to make this value something like 2.0. Then, with a friction of 0.0 this shape should continue at maxVelocity (perhaps losing some, although it should not) till the next collision, where the elasticity factor would increase total velocity capping at the value we set for maxVelocity...ad infinitum.
- Is this a good idea? Sorta, but its hacky.

c. Use the settings I have in my original post, with the addition of drag = 0.0;. This should solve your loss of velocity issue. With zero drag and zero friction, there is nothing to slow the item. A 1.0 velocity actually creates a rising velocity on rebound due to rounding errors, but the velocity limit will fix that. This should do it for you.


Note: All of this and much more is available/documented/demonstrated in the upcoming guide: EGTGE


[HOW]EdM|EGTGE
#5
12/01/2004 (2:45 pm)
Elasticity if how the disc fusior in T2 allowed Discs to be skipped across water, very cool FX indeed