Makes vehicles work with the physics plugins.

Makes vehicles create a basic physics body when using one of the physics plugins so that they can collide with other physics-enabled objects.

Based on @rextimmy 's work.
This commit is contained in:
Areloch 2016-06-04 16:47:03 -05:00
parent 5122360552
commit 37e030f8f4
9 changed files with 161 additions and 2 deletions

View file

@ -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 {
@ -203,7 +206,8 @@ VehicleData::VehicleData()
dMemset(waterSound, 0, sizeof(waterSound));
collDamageThresholdVel = 20;
collDamageMultiplier = 0.05f;
collDamageMultiplier = 0.05f;
enablePhysicsRep = true;
}
@ -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++)
@ -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++)
@ -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 "
@ -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()
@ -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()
@ -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++ )
{
@ -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());
}
}