Skip to content

Commit

Permalink
Merge pull request #1629 from Areloch/Veheekel_Feezeeks2
Browse files Browse the repository at this point in the history
Makes vehicles work with the physics plugins.
  • Loading branch information
Areloch committed Jun 5, 2016
2 parents 5122360 + 37e030f commit d2161e5
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 2 deletions.
22 changes: 22 additions & 0 deletions Engine/source/T3D/physics/bullet/btBody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,3 +378,25 @@ void BtBody::setSimulationEnabled( bool enabled )

mIsEnabled = enabled;
}

void BtBody::moveKinematicTo(const MatrixF &transform)
{
AssertFatal(mActor, "BtBody::moveKinematicTo - The actor is null!");

U32 bodyflags = mActor->getCollisionFlags();
const bool isKinematic = bodyflags & BF_KINEMATIC;
if (!isKinematic)
{
Con::errorf("BtBody::moveKinematicTo is only for kinematic bodies.");
return;
}

if (mCenterOfMass)
{
MatrixF xfm;
xfm.mul(transform, *mCenterOfMass);
mActor->setCenterOfMassTransform(btCast<btTransform>(xfm));
}
else
mActor->setCenterOfMassTransform(btCast<btTransform>(transform));
}
2 changes: 2 additions & 0 deletions Engine/source/T3D/physics/bullet/btBody.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ class BtBody : public PhysicsBody
F32 staticFriction );
virtual void applyCorrection( const MatrixF &xfm );
virtual void applyImpulse( const Point3F &origin, const Point3F &force );
virtual void moveKinematicTo(const MatrixF &xfm);

};

#endif // _T3D_PHYSICS_BTBODY_H_
4 changes: 4 additions & 0 deletions Engine/source/T3D/physics/physicsBody.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ class PhysicsBody : public PhysicsObject

///
virtual void applyImpulse( const Point3F &origin, const Point3F &force ) = 0;

///
virtual void moveKinematicTo(const MatrixF &xfm) = 0;

};


Expand Down
19 changes: 19 additions & 0 deletions Engine/source/T3D/physics/physx3/px3Body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,3 +417,22 @@ void Px3Body::applyImpulse( const Point3F &origin, const Point3F &force )

}

void Px3Body::moveKinematicTo(const MatrixF &transform)
{
AssertFatal(mActor, "Px3Body::moveKinematicTo - The actor is null!");

const bool isKinematic = mBodyFlags & BF_KINEMATIC;
if (!isKinematic)
{
Con::errorf("Px3Body::moveKinematicTo is only for kinematic bodies.");
return;
}

mWorld->lockScene();

physx::PxRigidDynamic *actor = mActor->is<physx::PxRigidDynamic>();
actor->setKinematicTarget(px3Cast<physx::PxTransform>(transform));

mWorld->unlockScene();
}

2 changes: 2 additions & 0 deletions Engine/source/T3D/physics/physx3/px3Body.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ class Px3Body : public PhysicsBody
F32 staticFriction );
virtual void applyCorrection( const MatrixF &xfm );
virtual void applyImpulse( const Point3F &origin, const Point3F &force );
virtual void moveKinematicTo(const MatrixF &xfm);

};

#endif // _PX3BODY_H_
59 changes: 58 additions & 1 deletion Engine/source/T3D/physics/physx3/px3World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ Px3World::Px3World(): mScene( NULL ),
mIsEnabled( false ),
mEditorTimeScale( 1.0f ),
mAccumulator( 0 ),
mControllerManager( NULL )
mControllerManager(NULL),
mIsSceneLocked(false)
{
}

Expand Down Expand Up @@ -335,6 +336,62 @@ void Px3World::releaseWriteLock()
//AssertFatal( mScene->isWritable(), "PhysX3World::releaseWriteLock() - We should have been writable now!" );
}

void Px3World::lockScenes()
{
Px3World *world = dynamic_cast<Px3World*>(PHYSICSMGR->getWorld("server"));

if (world)
world->lockScene();

world = dynamic_cast<Px3World*>(PHYSICSMGR->getWorld("client"));

if (world)
world->lockScene();
}

void Px3World::unlockScenes()
{
Px3World *world = dynamic_cast<Px3World*>(PHYSICSMGR->getWorld("server"));

if (world)
world->unlockScene();

world = dynamic_cast<Px3World*>(PHYSICSMGR->getWorld("client"));

if (world)
world->unlockScene();
}

void Px3World::lockScene()
{
if (!mScene)
return;

if (mIsSceneLocked)
{
Con::printf("Px3World: Attempting to lock a scene that is already locked.");
return;
}

mScene->lockWrite();
mIsSceneLocked = true;
}

void Px3World::unlockScene()
{
if (!mScene)
return;

if (!mIsSceneLocked)
{
Con::printf("Px3World: Attempting to unlock a scene that is not locked.");
return;
}

mScene->unlockWrite();
mIsSceneLocked = false;
}

bool Px3World::castRay( const Point3F &startPnt, const Point3F &endPnt, RayInfo *ri, const Point3F &impulse )
{

Expand Down
5 changes: 5 additions & 0 deletions Engine/source/T3D/physics/physx3/px3World.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class Px3World : public PhysicsWorld
bool mIsEnabled;
bool mIsSimulating;
bool mIsServer;
bool mIsSceneLocked;
U32 mTickCount;
ProcessList *mProcessList;
F32 mEditorTimeScale;
Expand Down Expand Up @@ -96,11 +97,15 @@ class Px3World : public PhysicsWorld
void releaseWriteLock();
bool isServer(){return mIsServer;}
physx::PxController* createController( physx::PxControllerDesc &desc );
void lockScene();
void unlockScene();
//static
static bool restartSDK( bool destroyOnly = false, Px3World *clientWorld = NULL, Px3World *serverWorld = NULL );
static void releaseWriteLocks();
static physx::PxCooking *getCooking();
static void setTiming(F32 stepTime,U32 maxIterations);
static void lockScenes();
static void unlockScenes();
};

#endif // _PX3WORLD_H_
43 changes: 42 additions & 1 deletion Engine/source/T3D/vehicles/vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
#include "gfx/primBuilder.h"
#include "gfx/gfxDrawUtil.h"
#include "materials/materialDefinition.h"
#include "T3D/physics/physicsPlugin.h"
#include "T3D/physics/physicsBody.h"
#include "T3D/physics/physicsCollision.h"


namespace {
Expand Down Expand Up @@ -203,7 +206,8 @@ VehicleData::VehicleData()
dMemset(waterSound, 0, sizeof(waterSound));

collDamageThresholdVel = 20;
collDamageMultiplier = 0.05f;
collDamageMultiplier = 0.05f;
enablePhysicsRep = true;
}


Expand Down Expand Up @@ -315,6 +319,7 @@ void VehicleData::packData(BitStream* stream)
stream->write(softSplashSoundVel);
stream->write(medSplashSoundVel);
stream->write(hardSplashSoundVel);
stream->write(enablePhysicsRep);

// write the water sound profiles
for(i = 0; i < MaxSounds; i++)
Expand Down Expand Up @@ -411,6 +416,7 @@ void VehicleData::unpackData(BitStream* stream)
stream->read(&softSplashSoundVel);
stream->read(&medSplashSoundVel);
stream->read(&hardSplashSoundVel);
stream->read(&enablePhysicsRep);

// write the water sound profiles
for(i = 0; i < MaxSounds; i++)
Expand Down Expand Up @@ -465,6 +471,11 @@ void VehicleData::unpackData(BitStream* stream)

void VehicleData::initPersistFields()
{
addGroup("Physics");
addField("enablePhysicsRep", TypeBool, Offset(enablePhysicsRep, VehicleData),
"@brief Creates a representation of the object in the physics plugin.\n");
endGroup("Physics");

addField( "jetForce", TypeF32, Offset(jetForce, VehicleData),
"@brief Additional force applied to the vehicle when it is jetting.\n\n"
"For WheeledVehicles, the force is applied in the forward direction. For "
Expand Down Expand Up @@ -682,6 +693,8 @@ Vehicle::Vehicle()
mWorkingQueryBox.minExtents.set(-1e9f, -1e9f, -1e9f);
mWorkingQueryBox.maxExtents.set(-1e9f, -1e9f, -1e9f);
mWorkingQueryBoxCountDown = sWorkingQueryBoxStaleThreshold;

mPhysicsRep = NULL;
}

U32 Vehicle::getCollisionMask()
Expand All @@ -695,6 +708,25 @@ Point3F Vehicle::getVelocity() const
return mRigid.linVelocity;
}

void Vehicle::_createPhysics()
{
SAFE_DELETE(mPhysicsRep);

if (!PHYSICSMGR || !mDataBlock->enablePhysicsRep)
return;

TSShape *shape = mShapeInstance->getShape();
PhysicsCollision *colShape = NULL;
colShape = shape->buildColShape(false, getScale());

if (colShape)
{
PhysicsWorld *world = PHYSICSMGR->getWorld(isServerObject() ? "server" : "client");
mPhysicsRep = PHYSICSMGR->createBody();
mPhysicsRep->init(colShape, 0, PhysicsBody::BF_KINEMATIC, this, world);
mPhysicsRep->setTransform(getTransform());
}
}
//----------------------------------------------------------------------------

bool Vehicle::onAdd()
Expand Down Expand Up @@ -776,11 +808,15 @@ bool Vehicle::onAdd()
mConvex.box.maxExtents.convolve(mObjScale);
mConvex.findNodeTransform();

_createPhysics();

return true;
}

void Vehicle::onRemove()
{
SAFE_DELETE(mPhysicsRep);

U32 i=0;
for( i=0; i<VehicleData::VC_NUM_DUST_EMITTERS; i++ )
{
Expand Down Expand Up @@ -880,6 +916,11 @@ void Vehicle::processTick(const Move* move)
setPosition(mRigid.linPosition, mRigid.angPosition);
setMaskBits(PositionMask);
updateContainer();

//TODO: Only update when position has actually changed
//no need to check if mDataBlock->enablePhysicsRep is false as mPhysicsRep will be NULL if it is
if (mPhysicsRep)
mPhysicsRep->moveKinematicTo(getTransform());
}
}

Expand Down
7 changes: 7 additions & 0 deletions Engine/source/T3D/vehicles/vehicle.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ struct VehicleData: public ShapeBaseData
F32 splashFreqMod;
F32 splashVelEpsilon;

bool enablePhysicsRep;

//
VehicleData();
bool preload(bool server, String &errorStr);
Expand All @@ -142,6 +144,7 @@ struct VehicleData: public ShapeBaseData


//----------------------------------------------------------------------------
class PhysicsBody;

class Vehicle: public ShapeBase
{
Expand Down Expand Up @@ -177,6 +180,8 @@ class Vehicle: public ShapeBase
Point3F cameraRotVec;
};

PhysicsBody *mPhysicsRep;

StateDelta mDelta;
S32 mPredictionCount; ///< Number of ticks to predict
VehicleData* mDataBlock;
Expand Down Expand Up @@ -262,6 +267,8 @@ class Vehicle: public ShapeBase
bool onAdd();
void onRemove();

void _createPhysics();

/// Interpolates between move ticks @see processTick
/// @param dt Change in time between the last call and this call to the function
void interpolateTick(F32 dt);
Expand Down

0 comments on commit d2161e5

Please sign in to comment.