Game Development Community

Question: Pushing Objects

by Nicolai Dutka · in Torque Game Engine Advanced · 05/22/2009 (10:57 pm) · 1 replies

I tossed a rigidShapeData object into my level and ran into it... BAM! Like hitting a solid wall! I thought rigidShape data was supposed to have physics and be able to be pushed around? Also, is there a way to make rigidShapeData ignore projectiles? It's crazy that I can't push around a ball, but if I shoot it, it flies about 7 miles away....

#1
05/22/2009 (11:52 pm)
To "bump" into it and move it you'll need to add an onCollision callback. Something like this:
function RigidShapeData::onCollision(%data, %obj, %col, %vec, %speed)
{
    //echo("c4RigidShapeData::onCollision("@ %data.getName()@", "@%obj@", "@%col.getClassName()@", "@%vec@", "@%speed @")");
   	
    // if it is colliding with itself or the terrain then ignore it
    if (%obj == %col || %col.getType() & $TypeMasks::TerrainObjectType || %speed < 1)
        return;
    if (%col.getType() & $TypeMasks::ShapeBaseObjectType)
    {
      	if (%obj.lastCollider = %col)
      	{
            // we just ran into this, lets give it half a second before we apply
            // another impulse
            if (getSimTime() - %obj.lastColTime < 550)
            	return;
      	}
      	%velocity = %col.getVelocity();
      	%normal = vectorDot(%velocity, VectorNormalize(%velocity));
      	if(%normal < 15)
            %multi = 8;
      	else
            %multi = 8 / %normal;
      	%objPos = %obj.getPosition();
      	%colPos = %col.getPosition();
      	%vec = VectorNormalize(VectorSub(%objPos, %colPos));
      	%force = VectorScale(%vec, %col.getDatablock().mass * %multi);
      	%obj.applyImpulse(%objPos, %force);
      	%obj.lastCollider = %col;
      	%obj.lastColTime = getSimTime();
    }	
}
Note that you'll have to carefully tweak your player mass and the rigidshape mass for optimal results. You can also tweak the density of your rigidshape and a waterblock to make it possible for something like a barrel to float/bob in the water.

To disable/reduce the projectile impulse against a rigidshape you could in order of my own preference (low to high)
- 1) Disable radius damage in the projectile.
- 2) $Typemask against rigidshape in the projectile's onCollision and return before doing damage/impulse.
- 4) Add a "noImpulse" check to the radiusDamage function, and then for objects that you don't want to impulse just add "noImpulse = true" to it's datablock
- 3) Scale the damage and impulse for different types of objects.