mirror of
https://github.com/TorqueGameEngines/Torque3D.git
synced 2026-07-14 08:04:40 +00:00
physics shape and physics debris
This commit is contained in:
parent
5d641929cf
commit
2b973abdcf
4 changed files with 47 additions and 43 deletions
|
|
@ -74,7 +74,12 @@ PhysicsDebrisData::PhysicsDebrisData()
|
||||||
lifetime = 5.0f;
|
lifetime = 5.0f;
|
||||||
lifetimeVariance = 0.0f;
|
lifetimeVariance = 0.0f;
|
||||||
|
|
||||||
INIT_ASSET(Shape);
|
mShapeAsset.registerRefreshNotify(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
PhysicsDebrisData::~PhysicsDebrisData()
|
||||||
|
{
|
||||||
|
mShapeAsset.unregisterRefreshNotify();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PhysicsDebrisData::onAdd()
|
bool PhysicsDebrisData::onAdd()
|
||||||
|
|
@ -96,12 +101,12 @@ bool PhysicsDebrisData::preload( bool server, String &errorStr )
|
||||||
{
|
{
|
||||||
// Create a dummy shape to force the generation of shaders and materials
|
// Create a dummy shape to force the generation of shaders and materials
|
||||||
// during the level load and not during gameplay.
|
// during the level load and not during gameplay.
|
||||||
TSShapeInstance *pDummy = new TSShapeInstance( mShape, !server );
|
TSShapeInstance *pDummy = new TSShapeInstance( getShape(), !server);
|
||||||
delete pDummy;
|
delete pDummy;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
errorStr = String::ToString("PhysicsDebrisData::load: Couldn't load shape asset \"%s\"", mShapeAssetId);
|
errorStr = String::ToString("PhysicsDebrisData::load: Couldn't load shape asset \"%s\"", _getShapeAssetId());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -113,12 +118,9 @@ void PhysicsDebrisData::initPersistFields()
|
||||||
docsURL;
|
docsURL;
|
||||||
addGroup( "Shapes" );
|
addGroup( "Shapes" );
|
||||||
|
|
||||||
addProtectedField( "shapeFile", TypeShapeFilename, Offset( mShapeName, PhysicsDebrisData ), &_setShapeData, &defaultProtectedGetFn,
|
INITPERSISTFIELD_SHAPEASSET_REFACTOR(Shape, PhysicsDebrisData, "@brief Shape to use with this debris.\n\n"
|
||||||
"@brief Path to the .DAE or .DTS file to use for this shape.\n\n"
|
"Compatable with Live-Asset Reloading.");
|
||||||
"Compatable with Live-Asset Reloading.", AbstractClassRep::FIELD_HideInInspectors);
|
|
||||||
|
|
||||||
INITPERSISTFIELD_SHAPEASSET(Shape, PhysicsDebrisData, "@brief Shape to use with this debris.\n\n"
|
|
||||||
"Compatable with Live-Asset Reloading.");
|
|
||||||
endGroup( "Shapes" );
|
endGroup( "Shapes" );
|
||||||
|
|
||||||
addGroup("Rendering");
|
addGroup("Rendering");
|
||||||
|
|
@ -216,7 +218,7 @@ void PhysicsDebrisData::packData(BitStream* stream)
|
||||||
stream->write( waterDampingScale );
|
stream->write( waterDampingScale );
|
||||||
stream->write( buoyancyDensity );
|
stream->write( buoyancyDensity );
|
||||||
|
|
||||||
PACKDATA_ASSET(Shape);
|
PACKDATA_ASSET_REFACTOR(Shape);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PhysicsDebrisData::unpackData(BitStream* stream)
|
void PhysicsDebrisData::unpackData(BitStream* stream)
|
||||||
|
|
@ -237,7 +239,7 @@ void PhysicsDebrisData::unpackData(BitStream* stream)
|
||||||
stream->read( &waterDampingScale );
|
stream->read( &waterDampingScale );
|
||||||
stream->read( &buoyancyDensity );
|
stream->read( &buoyancyDensity );
|
||||||
|
|
||||||
UNPACKDATA_ASSET(Shape);
|
UNPACKDATA_ASSET_REFACTOR(Shape);
|
||||||
}
|
}
|
||||||
|
|
||||||
DefineEngineMethod( PhysicsDebrisData, preload, void, (), ,
|
DefineEngineMethod( PhysicsDebrisData, preload, void, (), ,
|
||||||
|
|
@ -248,7 +250,7 @@ DefineEngineMethod( PhysicsDebrisData, preload, void, (), ,
|
||||||
{
|
{
|
||||||
String errorStr;
|
String errorStr;
|
||||||
|
|
||||||
object->_setShape(object->getShape());
|
object->_setShape(object->_getShapeAssetId());
|
||||||
|
|
||||||
if( !object->preload( false, errorStr ) )
|
if( !object->preload( false, errorStr ) )
|
||||||
Con::errorf( "PhsysicsDebrisData::preload - error: %s", errorStr.c_str() );
|
Con::errorf( "PhsysicsDebrisData::preload - error: %s", errorStr.c_str() );
|
||||||
|
|
@ -362,7 +364,7 @@ bool PhysicsDebris::onAdd()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setup our bounding box
|
// Setup our bounding box
|
||||||
mObjBox = mDataBlock->mShape->mBounds;
|
mObjBox = mDataBlock->getShape()->mBounds;
|
||||||
resetWorldBox();
|
resetWorldBox();
|
||||||
|
|
||||||
// Add it to the client scene.
|
// Add it to the client scene.
|
||||||
|
|
@ -625,7 +627,7 @@ void PhysicsDebris::_createFragments()
|
||||||
if ( !mWorld )
|
if ( !mWorld )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
TSShape *shape = mDataBlock->mShape;
|
TSShape *shape = mDataBlock->getShape();
|
||||||
|
|
||||||
mShapeInstance = new TSShapeInstance( shape, true );
|
mShapeInstance = new TSShapeInstance( shape, true );
|
||||||
mShapeInstance->animate();
|
mShapeInstance->animate();
|
||||||
|
|
@ -699,7 +701,7 @@ void PhysicsDebris::_findNodes( U32 colNode, Vector<U32> &nodeIds )
|
||||||
// 1. Visible mesh nodes are siblings of the collision node under a common parent dummy node
|
// 1. Visible mesh nodes are siblings of the collision node under a common parent dummy node
|
||||||
// 2. Collision node is a child of its visible mesh node
|
// 2. Collision node is a child of its visible mesh node
|
||||||
|
|
||||||
TSShape *shape = mDataBlock->mShape;
|
TSShape *shape = mDataBlock->getShape();
|
||||||
S32 itr = shape->nodes[colNode].parentIndex;
|
S32 itr = shape->nodes[colNode].parentIndex;
|
||||||
itr = shape->nodes[itr].firstChild;
|
itr = shape->nodes[itr].firstChild;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,7 @@ class TSShape;
|
||||||
//**************************************************************************
|
//**************************************************************************
|
||||||
// Debris Data
|
// Debris Data
|
||||||
//**************************************************************************
|
//**************************************************************************
|
||||||
class PhysicsDebrisData : public GameBaseData
|
class PhysicsDebrisData : public GameBaseData, protected AssetPtrCallback
|
||||||
{
|
{
|
||||||
typedef GameBaseData Parent;
|
typedef GameBaseData Parent;
|
||||||
|
|
||||||
|
|
@ -86,10 +86,10 @@ public:
|
||||||
/// Is rendererd during shadow passes.
|
/// Is rendererd during shadow passes.
|
||||||
bool castShadows;
|
bool castShadows;
|
||||||
|
|
||||||
DECLARE_SHAPEASSET(PhysicsDebrisData, Shape, onShapeChanged);
|
DECLARE_SHAPEASSET_REFACTOR(PhysicsDebrisData, Shape)
|
||||||
DECLARE_ASSET_SETGET(PhysicsDebrisData, Shape);
|
|
||||||
|
|
||||||
PhysicsDebrisData();
|
PhysicsDebrisData();
|
||||||
|
virtual ~PhysicsDebrisData();
|
||||||
|
|
||||||
bool onAdd() override;
|
bool onAdd() override;
|
||||||
bool preload( bool server, String &errorStr ) override;
|
bool preload( bool server, String &errorStr ) override;
|
||||||
|
|
@ -97,13 +97,14 @@ public:
|
||||||
void packData( BitStream *stream ) override;
|
void packData( BitStream *stream ) override;
|
||||||
void unpackData( BitStream *stream ) override;
|
void unpackData( BitStream *stream ) override;
|
||||||
|
|
||||||
void onShapeChanged()
|
DECLARE_CONOBJECT( PhysicsDebrisData );
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void onAssetRefreshed(AssetPtrBase* pAssetPtrBase) override
|
||||||
{
|
{
|
||||||
reloadOnLocalClient();
|
reloadOnLocalClient();
|
||||||
}
|
}
|
||||||
|
|
||||||
DECLARE_CONOBJECT( PhysicsDebrisData );
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -78,11 +78,12 @@ PhysicsShapeData::PhysicsShapeData()
|
||||||
buoyancyDensity( 0.0f ),
|
buoyancyDensity( 0.0f ),
|
||||||
simType( SimType_ClientServer )
|
simType( SimType_ClientServer )
|
||||||
{
|
{
|
||||||
INIT_ASSET(Shape);
|
mShapeAsset.registerRefreshNotify(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
PhysicsShapeData::~PhysicsShapeData()
|
PhysicsShapeData::~PhysicsShapeData()
|
||||||
{
|
{
|
||||||
|
mShapeAsset.unregisterRefreshNotify();
|
||||||
}
|
}
|
||||||
|
|
||||||
void PhysicsShapeData::initPersistFields()
|
void PhysicsShapeData::initPersistFields()
|
||||||
|
|
@ -90,7 +91,7 @@ void PhysicsShapeData::initPersistFields()
|
||||||
docsURL;
|
docsURL;
|
||||||
addGroup("Shapes");
|
addGroup("Shapes");
|
||||||
|
|
||||||
INITPERSISTFIELD_SHAPEASSET(Shape, PhysicsShapeData, "@brief Shape asset to be used with this physics object.\n\n"
|
INITPERSISTFIELD_SHAPEASSET_REFACTOR(Shape, PhysicsShapeData, "@brief Shape asset to be used with this physics object.\n\n"
|
||||||
"Compatable with Live-Asset Reloading. ")
|
"Compatable with Live-Asset Reloading. ")
|
||||||
|
|
||||||
addField( "debris", TYPEID< SimObjectRef<PhysicsDebrisData> >(), Offset( debris, PhysicsShapeData ),
|
addField( "debris", TYPEID< SimObjectRef<PhysicsDebrisData> >(), Offset( debris, PhysicsShapeData ),
|
||||||
|
|
@ -180,7 +181,7 @@ void PhysicsShapeData::packData( BitStream *stream )
|
||||||
{
|
{
|
||||||
Parent::packData( stream );
|
Parent::packData( stream );
|
||||||
|
|
||||||
PACKDATA_ASSET(Shape);
|
PACKDATA_ASSET_REFACTOR(Shape);
|
||||||
|
|
||||||
stream->write( mass );
|
stream->write( mass );
|
||||||
stream->write( dynamicFriction );
|
stream->write( dynamicFriction );
|
||||||
|
|
@ -204,7 +205,7 @@ void PhysicsShapeData::unpackData( BitStream *stream )
|
||||||
{
|
{
|
||||||
Parent::unpackData(stream);
|
Parent::unpackData(stream);
|
||||||
|
|
||||||
UNPACKDATA_ASSET(Shape);
|
UNPACKDATA_ASSET_REFACTOR(Shape);
|
||||||
|
|
||||||
stream->read( &mass );
|
stream->read( &mass );
|
||||||
stream->read( &dynamicFriction );
|
stream->read( &dynamicFriction );
|
||||||
|
|
@ -249,19 +250,19 @@ void PhysicsShapeData::_onResourceChanged( const Torque::Path &path )
|
||||||
if ( path != Path(mShapeAsset->getShapeFilePath()) )
|
if ( path != Path(mShapeAsset->getShapeFilePath()) )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
_setShape(getShape());
|
_setShape(_getShapeAssetId());
|
||||||
|
|
||||||
// Reload the changed shape.
|
// Reload the changed shape.
|
||||||
PhysicsCollisionRef reloadcolShape;
|
PhysicsCollisionRef reloadcolShape;
|
||||||
|
|
||||||
if ( !mShape )
|
if ( !getShape())
|
||||||
{
|
{
|
||||||
Con::warnf( ConsoleLogEntry::General, "PhysicsShapeData::_onResourceChanged: Could not reload %s.", path.getFileName().c_str() );
|
Con::warnf( ConsoleLogEntry::General, "PhysicsShapeData::_onResourceChanged: Could not reload %s.", path.getFileName().c_str() );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reload the collision shape.
|
// Reload the collision shape.
|
||||||
reloadcolShape = mShape->buildColShape( false, Point3F::One );
|
reloadcolShape = getShape()->buildColShape( false, Point3F::One );
|
||||||
|
|
||||||
if ( bool(reloadcolShape))
|
if ( bool(reloadcolShape))
|
||||||
colShape = reloadcolShape;
|
colShape = reloadcolShape;
|
||||||
|
|
@ -286,31 +287,31 @@ bool PhysicsShapeData::preload( bool server, String &errorBuffer )
|
||||||
|
|
||||||
if (mShapeAsset.notNull())
|
if (mShapeAsset.notNull())
|
||||||
{
|
{
|
||||||
if (bool(mShape) == false)
|
if (bool(getShape()) == false)
|
||||||
{
|
{
|
||||||
errorBuffer = String::ToString("PhysicsShapeData: Couldn't load shape \"%s\"", mShapeAssetId);
|
errorBuffer = String::ToString("PhysicsShapeData: Couldn't load shape \"%s\"", _getShapeAssetId());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!server && !mShape->preloadMaterialList(mShape.getPath()) && NetConnection::filesWereDownloaded())
|
if (!server && !getShape()->preloadMaterialList(getShape().getPath()) && NetConnection::filesWereDownloaded())
|
||||||
shapeError = true;
|
shapeError = true;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare the shared physics collision shape.
|
// Prepare the shared physics collision shape.
|
||||||
if ( !colShape && mShape)
|
if ( !colShape && getShape())
|
||||||
{
|
{
|
||||||
colShape = mShape->buildColShape( false, Point3F::One );
|
colShape = getShape()->buildColShape( false, Point3F::One );
|
||||||
|
|
||||||
// If we got here and didn't get a collision shape then
|
// If we got here and didn't get a collision shape then
|
||||||
// we need to fail... can't have a shape without collision.
|
// we need to fail... can't have a shape without collision.
|
||||||
if ( !colShape )
|
if ( !colShape )
|
||||||
{
|
{
|
||||||
//no collision so we create a simple box collision shape from the shapes bounds and alert the user
|
//no collision so we create a simple box collision shape from the shapes bounds and alert the user
|
||||||
Con::warnf( "PhysicsShapeData::preload - No collision found for shape '%s', auto-creating one", mShapeAssetId);
|
Con::warnf( "PhysicsShapeData::preload - No collision found for shape '%s', auto-creating one", _getShapeAssetId());
|
||||||
Point3F halfWidth = mShape->mBounds.getExtents() * 0.5f;
|
Point3F halfWidth = getShape()->mBounds.getExtents() * 0.5f;
|
||||||
colShape = PHYSICSMGR->createCollision();
|
colShape = PHYSICSMGR->createCollision();
|
||||||
MatrixF centerXfm(true);
|
MatrixF centerXfm(true);
|
||||||
centerXfm.setPosition(mShape->mBounds.getCenter());
|
centerXfm.setPosition(getShape()->mBounds.getCenter());
|
||||||
colShape->addBox(halfWidth, centerXfm);
|
colShape->addBox(halfWidth, centerXfm);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
@ -703,11 +704,11 @@ bool PhysicsShape::_createShape()
|
||||||
mAmbientSeq = -1;
|
mAmbientSeq = -1;
|
||||||
|
|
||||||
PhysicsShapeData *db = getDataBlock();
|
PhysicsShapeData *db = getDataBlock();
|
||||||
if ( !db || !db->mShape)
|
if ( !db || !db->getShape())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Set the world box.
|
// Set the world box.
|
||||||
mObjBox = db->mShape->mBounds;
|
mObjBox = db->getShape()->mBounds;
|
||||||
resetWorldBox();
|
resetWorldBox();
|
||||||
|
|
||||||
// If this is the server and its a client only simulation
|
// If this is the server and its a client only simulation
|
||||||
|
|
@ -721,11 +722,11 @@ bool PhysicsShape::_createShape()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the shape instance.
|
// Create the shape instance.
|
||||||
mShapeInst = new TSShapeInstance( db->mShape, isClientObject() );
|
mShapeInst = new TSShapeInstance( db->getShape(), isClientObject() );
|
||||||
|
|
||||||
if ( isClientObject() )
|
if ( isClientObject() )
|
||||||
{
|
{
|
||||||
mAmbientSeq = db->mShape->findSequence( "ambient" );
|
mAmbientSeq = db->getShape()->findSequence( "ambient" );
|
||||||
_initAmbient();
|
_initAmbient();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -51,7 +51,7 @@ class PhysicsDebrisData;
|
||||||
class ExplosionData;
|
class ExplosionData;
|
||||||
|
|
||||||
|
|
||||||
class PhysicsShapeData : public GameBaseData
|
class PhysicsShapeData : public GameBaseData, protected AssetPtrCallback
|
||||||
{
|
{
|
||||||
typedef GameBaseData Parent;
|
typedef GameBaseData Parent;
|
||||||
|
|
||||||
|
|
@ -74,8 +74,7 @@ public:
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
DECLARE_SHAPEASSET(PhysicsShapeData, Shape, onShapeChanged);
|
DECLARE_SHAPEASSET_REFACTOR(PhysicsShapeData, Shape)
|
||||||
DECLARE_ASSET_SETGET(PhysicsShapeData, Shape);
|
|
||||||
|
|
||||||
/// The shared unscaled collision shape.
|
/// The shared unscaled collision shape.
|
||||||
PhysicsCollisionRef colShape;
|
PhysicsCollisionRef colShape;
|
||||||
|
|
@ -135,7 +134,8 @@ public:
|
||||||
SimObjectRef< ExplosionData > explosion;
|
SimObjectRef< ExplosionData > explosion;
|
||||||
SimObjectRef< PhysicsShapeData > destroyedShape;
|
SimObjectRef< PhysicsShapeData > destroyedShape;
|
||||||
|
|
||||||
void onShapeChanged()
|
protected:
|
||||||
|
void onAssetRefreshed(AssetPtrBase* pAssetPtrBase) override
|
||||||
{
|
{
|
||||||
reloadOnLocalClient();
|
reloadOnLocalClient();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue