You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
883 lines
26 KiB
883 lines
26 KiB
14 years ago
|
/*==LICENSE==*
|
||
|
|
||
|
CyanWorlds.com Engine - MMOG client, server and tools
|
||
13 years ago
|
Copyright (C) 2011 Cyan Worlds, Inc.
|
||
14 years ago
|
|
||
|
This program is free software: you can redistribute it and/or modify
|
||
|
it under the terms of the GNU General Public License as published by
|
||
|
the Free Software Foundation, either version 3 of the License, or
|
||
|
(at your option) any later version.
|
||
|
|
||
|
This program is distributed in the hope that it will be useful,
|
||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 years ago
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 years ago
|
GNU General Public License for more details.
|
||
|
|
||
|
You should have received a copy of the GNU General Public License
|
||
13 years ago
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
|
|
||
|
Additional permissions under GNU GPL version 3 section 7
|
||
|
|
||
|
If you modify this Program, or any covered work, by linking or
|
||
|
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
|
||
|
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
|
||
|
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
|
||
|
(or a modified version of those libraries),
|
||
|
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
|
||
|
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
|
||
|
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
|
||
|
licensors of this Program grant you additional
|
||
|
permission to convey the resulting work. Corresponding Source for a
|
||
|
non-source form of such a combination shall include the source code for
|
||
|
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
|
||
|
work.
|
||
14 years ago
|
|
||
|
You can contact Cyan Worlds, Inc. by email legal@cyan.com
|
||
|
or by snail mail at:
|
||
|
Cyan Worlds, Inc.
|
||
|
14617 N Newport Hwy
|
||
|
Mead, WA 99021
|
||
|
|
||
|
*==LICENSE==*/
|
||
|
#include "plPXPhysicalControllerCore.h"
|
||
|
#include "plSimulationMgr.h"
|
||
|
#include "plPXPhysical.h"
|
||
|
#include "plPXConvert.h"
|
||
|
#include "../pnSceneObject/plSimulationInterface.h"
|
||
|
#include "../pnSceneObject/plSceneObject.h"
|
||
|
#include "../plAvatar/plArmatureMod.h"
|
||
|
#include "../pnSceneObject/plCoordinateInterface.h"
|
||
|
#include "../plDrawable/plDrawableGenerator.h"
|
||
|
#include "../plPhysical/plPhysicalProxy.h"
|
||
|
#include "../pnMessage/plSetNetGroupIDMsg.h"
|
||
|
#include "../plMessage/plCollideMsg.h"
|
||
|
#include "../plModifier/plDetectorLog.h"
|
||
12 years ago
|
|
||
|
#include "../plSurface/hsGMaterial.h" // For our proxy
|
||
|
#include "../plSurface/plLayerInterface.h" // For our proxy
|
||
14 years ago
|
|
||
|
#include "NxPhysics.h"
|
||
|
#include "NxCapsuleController.h"
|
||
|
#include "NxCapsuleShape.h"
|
||
12 years ago
|
#include "ControllerManager.h"
|
||
14 years ago
|
|
||
12 years ago
|
static ControllerManager gControllerMgr;
|
||
|
static std::vector<plPXPhysicalControllerCore*> gControllers;
|
||
|
static bool gRebuildCache = false;
|
||
14 years ago
|
|
||
|
#ifndef PLASMA_EXTERNAL_RELEASE
|
||
|
hsBool plPXPhysicalControllerCore::fDebugDisplay = false;
|
||
|
#endif // PLASMA_EXTERNAL_RELEASE
|
||
|
int plPXPhysicalControllerCore::fPXControllersMax = 0;
|
||
|
|
||
12 years ago
|
#define kCCTSkinWidth 0.1f
|
||
4 years ago
|
#define kCCTStepOffset 0.7f
|
||
12 years ago
|
#define kCCTZOffset ((fRadius + (fHeight / 2)) + kCCTSkinWidth)
|
||
12 years ago
|
#define kPhysHeightCorrection 0.8f
|
||
12 years ago
|
#define kPhysZOffset ((kCCTZOffset + (kPhysHeightCorrection / 2)) - 0.05f)
|
||
|
#define kAvatarMass 200.0f
|
||
14 years ago
|
|
||
12 years ago
|
static class PXControllerHitReport : public NxUserControllerHitReport
|
||
14 years ago
|
{
|
||
|
public:
|
||
|
virtual NxControllerAction onShapeHit(const NxControllerShapeHit& hit)
|
||
|
{
|
||
12 years ago
|
plPXPhysicalControllerCore* controller = (plPXPhysicalControllerCore*)hit.controller->getUserData();
|
||
14 years ago
|
NxActor& actor = hit.shape->getActor();
|
||
|
plPXPhysical* phys = (plPXPhysical*)actor.userData;
|
||
|
hsVector3 normal = plPXConvert::Vector(hit.worldNormal);
|
||
12 years ago
|
|
||
14 years ago
|
#ifndef PLASMA_EXTERNAL_RELEASE
|
||
|
plDbgCollisionInfo info;
|
||
|
info.fNormal = normal;
|
||
|
info.fSO = plSceneObject::ConvertNoRef(phys->GetObjectKey()->ObjectIsLoaded());
|
||
12 years ago
|
|
||
|
NxCapsule capsule;
|
||
|
controller->GetWorldSpaceCapsule(capsule);
|
||
|
info.fOverlap = hit.shape->checkOverlapCapsule(capsule);
|
||
|
|
||
|
controller->fDbgCollisionInfo.Append(info);
|
||
14 years ago
|
#endif PLASMA_EXTERNAL_RELEASE
|
||
12 years ago
|
|
||
14 years ago
|
// If the avatar hit a movable physical, apply some force to it.
|
||
12 years ago
|
if (actor.isDynamic())
|
||
14 years ago
|
{
|
||
12 years ago
|
if (!actor.readBodyFlag(NX_BF_KINEMATIC) && !actor.readBodyFlag(NX_BF_FROZEN))
|
||
14 years ago
|
{
|
||
12 years ago
|
// Don't apply force when standing on top of an object.
|
||
|
if (normal.fZ < 0.85f)
|
||
14 years ago
|
{
|
||
12 years ago
|
hsVector3 velocity = controller->GetLinearVelocity();
|
||
|
velocity.fZ = 0.0f;
|
||
|
float length = velocity.Magnitude();
|
||
|
if (length > 0)
|
||
14 years ago
|
{
|
||
12 years ago
|
// Only allow horizontal pushes for now
|
||
|
NxVec3 hitDir = hit.worldPos - hit.controller->getPosition();
|
||
|
hitDir.z = 0.0f;
|
||
|
hitDir.normalize();
|
||
|
|
||
|
// Get controller speed along the hitDir
|
||
|
float cctProj = velocity.fX * hitDir.x + velocity.fY * hitDir.y;
|
||
|
length = length + cctProj / 2.0f;
|
||
|
|
||
|
// Get hit actors speed along the hitDir
|
||
|
float hitProj = actor.getLinearVelocity().dot(hitDir);
|
||
|
if (hitProj > 0)
|
||
|
length -= hitProj;
|
||
|
|
||
|
length *= kAvatarMass;
|
||
|
|
||
|
hsPoint3 pos((float)hit.worldPos.x, (float)hit.worldPos.y, (float)hit.worldPos.z);
|
||
|
phys->SetHitForce(plPXConvert::Vector(hitDir * length), pos);
|
||
|
controller->AddDynamicHit(phys);
|
||
14 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
else // else if the avatar hit a static
|
||
|
{
|
||
12 years ago
|
controller->fMovementStrategy->AddContactNormals(normal);
|
||
14 years ago
|
return NX_ACTION_NONE;
|
||
|
}
|
||
|
if (phys && phys->GetProperty(plSimulationInterface::kAvAnimPushable))
|
||
|
{
|
||
12 years ago
|
hsQuat inverseRotation = controller->fLocalRotation.Inverse();
|
||
14 years ago
|
hsVector3 normal = plPXConvert::Vector(hit.worldNormal);
|
||
12 years ago
|
controller->SetPushingPhysical(phys);
|
||
|
controller->SetFacingPushingPhysical((inverseRotation.Rotate(&kAvatarForward).InnerProduct(normal) < 0 ? true : false));
|
||
14 years ago
|
}
|
||
|
return NX_ACTION_NONE;
|
||
|
}
|
||
|
virtual NxControllerAction onControllerHit(const NxControllersHit& hit)
|
||
|
{
|
||
|
return NX_ACTION_NONE;
|
||
|
}
|
||
12 years ago
|
} gControllerHitReport;
|
||
14 years ago
|
|
||
12 years ago
|
plPhysicalControllerCore* plPhysicalControllerCore::Create(plKey ownerSO, hsScalar height, hsScalar width, bool human)
|
||
14 years ago
|
{
|
||
12 years ago
|
if (!plPXPhysicalControllerCore::fPXControllersMax || gControllers.size() < plPXPhysicalControllerCore::fPXControllersMax)
|
||
14 years ago
|
{
|
||
12 years ago
|
hsScalar radius = width / 2.0f;
|
||
|
hsScalar realHeight = height - width;
|
||
12 years ago
|
return TRACKED_NEW plPXPhysicalControllerCore(ownerSO, realHeight, radius, human);
|
||
14 years ago
|
}
|
||
|
return nil;
|
||
|
}
|
||
|
|
||
12 years ago
|
plPXPhysicalControllerCore::plPXPhysicalControllerCore(plKey ownerSO, hsScalar height, hsScalar radius, bool human)
|
||
12 years ago
|
: plPhysicalControllerCore(ownerSO, height, radius),
|
||
|
fController(nil),
|
||
|
fActor(nil),
|
||
|
fProxyGen(nil),
|
||
12 years ago
|
fKinematicCCT(true),
|
||
|
fHuman(human)
|
||
12 years ago
|
{
|
||
|
ICreateController(fLocalPosition);
|
||
|
fActor->raiseActorFlag(NX_AF_DISABLE_COLLISION);
|
||
14 years ago
|
gControllers.push_back(this);
|
||
|
}
|
||
|
plPXPhysicalControllerCore::~plPXPhysicalControllerCore()
|
||
|
{
|
||
12 years ago
|
int numControllers = gControllers.size();
|
||
|
for (int i = 0; i < numControllers; ++i)
|
||
14 years ago
|
{
|
||
|
if (gControllers[i] == this)
|
||
|
{
|
||
|
gControllers.erase(gControllers.begin()+i);
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
12 years ago
|
IDeleteController();
|
||
14 years ago
|
|
||
12 years ago
|
// Release any queued messages we may have
|
||
|
int numMsgs = fQueuedCollideMsgs.size();
|
||
|
if (numMsgs)
|
||
14 years ago
|
{
|
||
12 years ago
|
for (int i = 0; i < numMsgs; ++i)
|
||
|
delete fQueuedCollideMsgs[i];
|
||
14 years ago
|
|
||
12 years ago
|
fQueuedCollideMsgs.clear();
|
||
14 years ago
|
}
|
||
|
|
||
12 years ago
|
delete fProxyGen;
|
||
14 years ago
|
}
|
||
|
|
||
|
void plPXPhysicalControllerCore::Enable(bool enable)
|
||
|
{
|
||
|
if (fEnabled != enable)
|
||
|
{
|
||
|
fEnabled = enable;
|
||
|
if (fEnabled)
|
||
12 years ago
|
{
|
||
|
// Defer until the next physics update.
|
||
14 years ago
|
fEnableChanged = true;
|
||
12 years ago
|
}
|
||
14 years ago
|
else
|
||
|
{
|
||
12 years ago
|
if (!fKinematicCCT)
|
||
|
{
|
||
|
// Dynamic controllers are forced kinematic
|
||
|
fActor->raiseBodyFlag(NX_BF_KINEMATIC);
|
||
|
NxShape* shape = fActor->getShapes()[0];
|
||
|
shape->setGroup(plSimDefs::kGroupAvatarKinematic);
|
||
|
}
|
||
14 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void plPXPhysicalControllerCore::SetSubworld(plKey world)
|
||
|
{
|
||
|
if (fWorldKey != world)
|
||
|
{
|
||
|
SimLog("Changing subworlds!");
|
||
12 years ago
|
|
||
|
// Inform detectors in the old world that we are leaving
|
||
|
IInformDetectors(false);
|
||
14 years ago
|
IDeleteController();
|
||
12 years ago
|
|
||
12 years ago
|
// We need our real global location here, not the interpolated location
|
||
|
fLocalRotation.MakeMatrix(&fLastGlobalLoc);
|
||
|
fLastGlobalLoc.SetTranslate(&fLocalPosition);
|
||
|
if (fWorldKey)
|
||
|
{
|
||
|
hsMatrix44 prevSubL2W;
|
||
|
fPrevSubworldW2L.GetInverse(&prevSubL2W);
|
||
|
fLastGlobalLoc = prevSubL2W * fLastGlobalLoc;
|
||
|
}
|
||
|
// Update our scene object so the change isn't wiped out
|
||
|
plSceneObject* so = plSceneObject::ConvertNoRef(fOwner->ObjectIsLoaded());
|
||
|
if (so)
|
||
|
{
|
||
|
hsMatrix44 globalLocInv;
|
||
|
fLastGlobalLoc.GetInverse(&globalLocInv);
|
||
|
so->SetTransform(fLastGlobalLoc, globalLocInv);
|
||
|
so->FlushTransform();
|
||
|
}
|
||
|
|
||
12 years ago
|
// Update Local Position and rotation
|
||
|
fWorldKey = world;
|
||
14 years ago
|
const plCoordinateInterface* subworldCI = GetSubworldCI();
|
||
|
if (subworldCI)
|
||
|
{
|
||
12 years ago
|
fPrevSubworldW2L = subworldCI->GetWorldToLocal();
|
||
|
hsMatrix44 l2s = fPrevSubworldW2L * fLastGlobalLoc;
|
||
14 years ago
|
l2s.GetTranslate(&fLocalPosition);
|
||
|
fLocalRotation.SetFromMatrix44(l2s);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
12 years ago
|
fPrevSubworldW2L.Reset();
|
||
14 years ago
|
fLastGlobalLoc.GetTranslate(&fLocalPosition);
|
||
|
fLocalRotation.SetFromMatrix44(fLastGlobalLoc);
|
||
|
}
|
||
12 years ago
|
|
||
|
fLastLocalPosition = fLocalPosition;
|
||
|
|
||
|
// Create new controller
|
||
|
ICreateController(fLocalPosition);
|
||
12 years ago
|
RebuildCache();
|
||
14 years ago
|
}
|
||
|
}
|
||
12 years ago
|
|
||
14 years ago
|
void plPXPhysicalControllerCore::GetState(hsPoint3& pos, float& zRot)
|
||
|
{
|
||
|
// Temporarily use the position point while we get the z rotation
|
||
|
fLocalRotation.NormalizeIfNeeded();
|
||
|
fLocalRotation.GetAngleAxis(&zRot, (hsVector3*)&pos);
|
||
|
|
||
|
if (pos.fZ < 0)
|
||
|
zRot = (2 * hsScalarPI) - zRot; // axis is backwards, so reverse the angle too
|
||
|
|
||
|
pos = fLocalPosition;
|
||
|
}
|
||
|
void plPXPhysicalControllerCore::SetState(const hsPoint3& pos, float zRot)
|
||
|
{
|
||
|
plSceneObject* so = plSceneObject::ConvertNoRef(fOwner->ObjectIsLoaded());
|
||
|
if (so)
|
||
|
{
|
||
|
hsQuat worldRot;
|
||
|
hsVector3 zAxis(0.f, 0.f, 1.f);
|
||
|
worldRot.SetAngleAxis(zRot, zAxis);
|
||
|
|
||
|
hsMatrix44 l2w, w2l;
|
||
|
worldRot.MakeMatrix(&l2w);
|
||
|
l2w.SetTranslate(&pos);
|
||
|
|
||
|
// Localize new position and rotation to global coords if we're in a subworld
|
||
|
const plCoordinateInterface* ci = GetSubworldCI();
|
||
|
if (ci)
|
||
|
{
|
||
|
const hsMatrix44& subworldL2W = ci->GetLocalToWorld();
|
||
|
l2w = subworldL2W * l2w;
|
||
|
}
|
||
|
l2w.GetInverse(&w2l);
|
||
|
so->SetTransform(l2w, w2l);
|
||
|
so->FlushTransform();
|
||
|
}
|
||
|
}
|
||
12 years ago
|
|
||
|
void plPXPhysicalControllerCore::SetMovementStrategy(plMovementStrategy* strategy)
|
||
14 years ago
|
{
|
||
12 years ago
|
if (fKinematicCCT != strategy->IsKinematic())
|
||
14 years ago
|
{
|
||
12 years ago
|
IDeleteController();
|
||
|
fKinematicCCT = !fKinematicCCT;
|
||
|
ICreateController(fLocalPosition);
|
||
14 years ago
|
}
|
||
12 years ago
|
|
||
|
fMovementStrategy = strategy;
|
||
14 years ago
|
}
|
||
12 years ago
|
|
||
|
void plPXPhysicalControllerCore::SetGlobalLoc(const hsMatrix44& l2w)
|
||
14 years ago
|
{
|
||
12 years ago
|
fLastGlobalLoc = l2w;
|
||
|
|
||
|
// Update our local position and rotation
|
||
12 years ago
|
hsPoint3 prevPosition = fLocalPosition;
|
||
12 years ago
|
const plCoordinateInterface* subworldCI = GetSubworldCI();
|
||
|
if (subworldCI)
|
||
14 years ago
|
{
|
||
12 years ago
|
hsMatrix44 l2s = fPrevSubworldW2L * l2w;
|
||
|
|
||
|
l2s.GetTranslate(&fLocalPosition);
|
||
|
fLocalRotation.SetFromMatrix44(l2s);
|
||
14 years ago
|
}
|
||
12 years ago
|
else
|
||
14 years ago
|
{
|
||
12 years ago
|
l2w.GetTranslate(&fLocalPosition);
|
||
|
fLocalRotation.SetFromMatrix44(l2w);
|
||
|
}
|
||
14 years ago
|
|
||
12 years ago
|
fLastLocalPosition = fLocalPosition;
|
||
|
|
||
|
if (fProxyGen)
|
||
|
{
|
||
|
hsMatrix44 w2l;
|
||
|
l2w.GetInverse(&w2l);
|
||
|
fProxyGen->SetTransform(l2w, w2l);
|
||
|
}
|
||
|
|
||
|
// Update the physical position
|
||
|
if (fKinematicCCT)
|
||
|
{
|
||
12 years ago
|
hsVector3 disp(&fLocalPosition, &prevPosition);
|
||
|
if (disp.Magnitude() > 2.f)
|
||
|
{
|
||
|
// Teleport the underlying actor most of the way
|
||
|
disp.Normalize();
|
||
|
disp *= 0.001f;
|
||
|
|
||
|
hsPoint3 teleportPos = fLocalPosition - disp;
|
||
|
NxVec3 pos(teleportPos.fX, teleportPos.fY, teleportPos.fZ + kPhysZOffset);
|
||
|
fActor->setGlobalPosition(pos);
|
||
|
}
|
||
|
|
||
|
NxExtendedVec3 extPos(fLocalPosition.fX, fLocalPosition.fY, fLocalPosition.fZ + kCCTZOffset);
|
||
|
fController->setPosition(extPos);
|
||
12 years ago
|
}
|
||
|
else
|
||
|
{
|
||
|
NxVec3 pos(fLocalPosition.fX, fLocalPosition.fY, fLocalPosition.fZ + kPhysZOffset);
|
||
|
if (fActor->readBodyFlag(NX_BF_KINEMATIC))
|
||
|
fActor->moveGlobalPosition(pos);
|
||
|
else
|
||
|
fActor->setGlobalPosition(pos);
|
||
14 years ago
|
}
|
||
|
}
|
||
12 years ago
|
|
||
|
void plPXPhysicalControllerCore::GetPositionSim(hsPoint3& pos)
|
||
14 years ago
|
{
|
||
12 years ago
|
if (fKinematicCCT)
|
||
|
{
|
||
|
const NxExtendedVec3& extPos = fController->getPosition();
|
||
|
pos.Set((hsScalar)extPos.x, (hsScalar)extPos.y, (hsScalar)extPos.z - kCCTZOffset);
|
||
|
}
|
||
|
else
|
||
14 years ago
|
{
|
||
12 years ago
|
NxVec3 nxPos = fActor->getGlobalPosition();
|
||
|
pos.Set(nxPos.x, nxPos.y, nxPos.z - kPhysZOffset);
|
||
|
}
|
||
|
}
|
||
14 years ago
|
|
||
12 years ago
|
void plPXPhysicalControllerCore::Move(hsVector3 displacement, unsigned int collideWith, unsigned int &collisionResults)
|
||
|
{
|
||
|
NxU32 colFlags = 0;
|
||
14 years ago
|
|
||
12 years ago
|
fController->move(plPXConvert::Vector(displacement), collideWith, 0.00001f, colFlags);
|
||
|
|
||
|
collisionResults = 0;
|
||
|
if (colFlags & NXCC_COLLISION_DOWN)
|
||
|
collisionResults |= kBottom;
|
||
|
if (colFlags & NXCC_COLLISION_UP)
|
||
|
collisionResults |= kTop;
|
||
|
if (colFlags & NXCC_COLLISION_SIDES)
|
||
|
collisionResults |= kSides;
|
||
14 years ago
|
}
|
||
12 years ago
|
|
||
|
void plPXPhysicalControllerCore::SetLinearVelocitySim(const hsVector3& linearVel)
|
||
14 years ago
|
{
|
||
12 years ago
|
if (!fKinematicCCT)
|
||
14 years ago
|
{
|
||
12 years ago
|
NxVec3 vel = plPXConvert::Vector(linearVel);
|
||
|
fActor->setLinearVelocity(vel);
|
||
14 years ago
|
}
|
||
|
}
|
||
12 years ago
|
|
||
|
int plPXPhysicalControllerCore::SweepControllerPath(const hsPoint3& startPos, const hsPoint3& endPos, hsBool vsDynamics, hsBool vsStatics,
|
||
|
UInt32& vsSimGroups, std::vector<plControllerSweepRecord>& hits)
|
||
14 years ago
|
{
|
||
12 years ago
|
NxSweepQueryHit queryHit[10];
|
||
|
|
||
|
unsigned int flags = NX_SF_ALL_HITS;
|
||
|
if (vsDynamics)
|
||
|
flags |= NX_SF_DYNAMICS;
|
||
|
if (vsStatics)
|
||
|
flags |= NX_SF_STATICS;
|
||
|
|
||
|
NxVec3 vec;
|
||
|
vec.x = endPos.fX - startPos.fX;
|
||
|
vec.y = endPos.fY - startPos.fY;
|
||
|
vec.z = endPos.fZ - startPos.fZ;
|
||
|
|
||
|
NxShape* shape = fActor->getShapes()[0];
|
||
|
NxCapsuleShape* capShape = shape->isCapsule();
|
||
|
float radius = capShape->getRadius();
|
||
|
float height = capShape->getHeight();
|
||
|
|
||
|
NxCapsule capsule;
|
||
|
capsule.p0 = plPXConvert::Point(startPos);
|
||
|
capsule.p0.z = capsule.p0.z + radius;
|
||
|
capsule.radius = radius;
|
||
|
capsule.p1 = capsule.p0;
|
||
|
capsule.p1.z = capsule.p1.z + height;
|
||
|
|
||
|
NxScene *scene = plSimulationMgr::GetInstance()->GetScene(fWorldKey);
|
||
|
int numHits = scene->linearCapsuleSweep(capsule, vec, flags, nil, 10, queryHit, nil, vsSimGroups);
|
||
|
if (numHits)
|
||
|
{
|
||
|
for (int i = 0; i < numHits; ++i)
|
||
14 years ago
|
{
|
||
12 years ago
|
plControllerSweepRecord currentHit;
|
||
|
currentHit.ObjHit = (plPhysical*)queryHit[i].hitShape->getActor().userData;
|
||
|
if (currentHit.ObjHit)
|
||
|
{
|
||
|
currentHit.Point = plPXConvert::Point(queryHit[i].point);
|
||
|
currentHit.Normal = plPXConvert::Vector(queryHit[i].normal);
|
||
|
hits.push_back(currentHit);
|
||
|
}
|
||
14 years ago
|
}
|
||
12 years ago
|
}
|
||
|
|
||
|
return hits.size();
|
||
14 years ago
|
}
|
||
|
|
||
12 years ago
|
void plPXPhysicalControllerCore::LeaveAge()
|
||
14 years ago
|
{
|
||
12 years ago
|
SetPushingPhysical(nil);
|
||
|
if (fWorldKey)
|
||
|
SetSubworld(nil);
|
||
|
|
||
|
// Disable all collisions
|
||
|
fActor->raiseActorFlag(NX_AF_DISABLE_COLLISION);
|
||
14 years ago
|
}
|
||
12 years ago
|
|
||
|
void plPXPhysicalControllerCore::GetWorldSpaceCapsule(NxCapsule& cap) const
|
||
14 years ago
|
{
|
||
12 years ago
|
NxShape* shape = fActor->getShapes()[0];
|
||
|
NxCapsuleShape* capShape = shape->isCapsule();
|
||
|
capShape->getWorldCapsule(cap);
|
||
14 years ago
|
}
|
||
12 years ago
|
|
||
|
plDrawableSpans* plPXPhysicalControllerCore::CreateProxy(hsGMaterial* mat, hsTArray<UInt32>& idx, plDrawableSpans* addTo)
|
||
14 years ago
|
{
|
||
12 years ago
|
// FIXME
|
||
|
plDrawableSpans* myDraw = addTo;
|
||
|
hsBool blended = ((mat->GetLayer(0)->GetBlendFlags() & hsGMatState::kBlendMask));
|
||
|
float radius = fRadius;
|
||
|
myDraw = plDrawableGenerator::GenerateSphericalDrawable(fLocalPosition, radius,
|
||
|
mat, fLastGlobalLoc, blended,
|
||
|
nil, &idx, myDraw);
|
||
14 years ago
|
|
||
12 years ago
|
/*
|
||
|
plSceneObject* so = plSceneObject::ConvertNoRef(fOwner->ObjectIsLoaded());
|
||
|
if (so)
|
||
14 years ago
|
{
|
||
12 years ago
|
hsBool blended = ((mat->GetLayer(0)->GetBlendFlags() & hsGMatState::kBlendMask));
|
||
|
|
||
|
myDraw = plDrawableGenerator::GenerateConicalDrawable(fRadius*10, fHeight*10,
|
||
|
mat, so->GetLocalToWorld(), blended,
|
||
|
nil, &idx, myDraw);
|
||
14 years ago
|
}
|
||
12 years ago
|
*/
|
||
|
return myDraw;
|
||
|
}
|
||
|
|
||
|
void plPXPhysicalControllerCore::AddDynamicHit(plPXPhysical* phys)
|
||
|
{
|
||
|
int numHits = fDynamicHits.size();
|
||
|
for (int i = 0; i < numHits; ++i)
|
||
14 years ago
|
{
|
||
12 years ago
|
if (fDynamicHits[i] == phys)
|
||
|
return;
|
||
14 years ago
|
}
|
||
|
|
||
12 years ago
|
fDynamicHits.push_back(phys);
|
||
14 years ago
|
}
|
||
12 years ago
|
|
||
|
void plPXPhysicalControllerCore::Apply(hsScalar delSecs)
|
||
14 years ago
|
{
|
||
12 years ago
|
plPXPhysicalControllerCore* controller;
|
||
|
int numControllers = gControllers.size();
|
||
|
for (int i = 0; i < numControllers; ++i)
|
||
14 years ago
|
{
|
||
12 years ago
|
controller = gControllers[i];
|
||
|
if (gRebuildCache && controller->fController)
|
||
|
controller->fController->reportSceneChanged();
|
||
|
|
||
|
#ifndef PLASMA_EXTERNAL_RELEASE
|
||
|
controller->fDbgCollisionInfo.SetCount(0);
|
||
|
#endif
|
||
|
|
||
|
controller->IDispatchQueuedMsgs();
|
||
|
controller->IApply(delSecs);
|
||
|
controller->IProcessDynamicHits();
|
||
14 years ago
|
}
|
||
12 years ago
|
|
||
|
gRebuildCache = false;
|
||
|
}
|
||
|
void plPXPhysicalControllerCore::Update(int numSubSteps, hsScalar alpha)
|
||
|
{
|
||
|
gControllerMgr.updateControllers();
|
||
|
|
||
|
plPXPhysicalControllerCore* controller;
|
||
|
int numControllers = gControllers.size();
|
||
|
for (int i = 0; i < numControllers; ++i)
|
||
14 years ago
|
{
|
||
12 years ago
|
controller = gControllers[i];
|
||
|
|
||
|
controller->IUpdate(numSubSteps, alpha);
|
||
|
|
||
|
#ifndef PLASMA_EXTERNAL_RELEASE
|
||
|
if (fDebugDisplay)
|
||
|
controller->IDrawDebugDisplay();
|
||
|
#endif
|
||
14 years ago
|
}
|
||
|
}
|
||
12 years ago
|
void plPXPhysicalControllerCore::UpdateNonPhysical(hsScalar alpha)
|
||
14 years ago
|
{
|
||
12 years ago
|
plPXPhysicalControllerCore* controller;
|
||
|
int numControllers = gControllers.size();
|
||
|
for (int i = 0; i < numControllers; ++i)
|
||
|
{
|
||
|
controller = gControllers[i];
|
||
|
controller->IUpdateNonPhysical(alpha);
|
||
|
}
|
||
14 years ago
|
}
|
||
12 years ago
|
|
||
|
void plPXPhysicalControllerCore::RebuildCache() { gRebuildCache = true; }
|
||
|
|
||
|
plPXPhysicalControllerCore* plPXPhysicalControllerCore::GetController(NxActor& actor)
|
||
14 years ago
|
{
|
||
12 years ago
|
plPXPhysicalControllerCore* controller;
|
||
|
int numControllers = gControllers.size();
|
||
|
for (int i = 0; i < numControllers; ++i)
|
||
|
{
|
||
|
controller = gControllers[i];
|
||
|
if (controller->fActor == &actor)
|
||
|
return controller;
|
||
|
}
|
||
14 years ago
|
|
||
12 years ago
|
return nil;
|
||
|
}
|
||
14 years ago
|
|
||
12 years ago
|
bool plPXPhysicalControllerCore::AnyControllersInThisWorld(plKey world)
|
||
|
{
|
||
|
plPXPhysicalControllerCore* controller;
|
||
|
int numControllers = gControllers.size();
|
||
|
for (int i = 0; i < numControllers; ++i)
|
||
|
{
|
||
|
controller = gControllers[i];
|
||
|
if (controller->GetSubworld() == world)
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
int plPXPhysicalControllerCore::GetNumberOfControllersInThisSubWorld(plKey world)
|
||
|
{
|
||
|
int count = 0;
|
||
|
plPXPhysicalControllerCore* controller;
|
||
|
int numControllers = gControllers.size();
|
||
|
for (int i = 0; i < numControllers; ++i)
|
||
|
{
|
||
|
controller = gControllers[i];
|
||
|
if (controller->GetSubworld() == world)
|
||
|
++count;
|
||
14 years ago
|
}
|
||
|
|
||
12 years ago
|
return count;
|
||
14 years ago
|
}
|
||
12 years ago
|
int plPXPhysicalControllerCore::GetControllersInThisSubWorld(plKey world, int maxToReturn, plPXPhysicalControllerCore** bufferout)
|
||
14 years ago
|
{
|
||
12 years ago
|
int count = 0;
|
||
|
plPXPhysicalControllerCore* controller;
|
||
|
int numControllers = gControllers.size();
|
||
|
for (int i = 0; i < numControllers; ++i)
|
||
14 years ago
|
{
|
||
12 years ago
|
controller = gControllers[i];
|
||
|
if (controller->GetSubworld() == world)
|
||
14 years ago
|
{
|
||
12 years ago
|
if (count < maxToReturn)
|
||
14 years ago
|
{
|
||
12 years ago
|
bufferout[count] = controller;
|
||
|
++count;
|
||
14 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
return count;
|
||
14 years ago
|
}
|
||
|
|
||
12 years ago
|
int plPXPhysicalControllerCore::NumControllers() { return gControllers.size(); }
|
||
|
|
||
|
void plPXPhysicalControllerCore::IHandleEnableChanged()
|
||
14 years ago
|
{
|
||
12 years ago
|
// Defered enable
|
||
|
fEnableChanged = false;
|
||
|
|
||
|
if (!fKinematicCCT)
|
||
14 years ago
|
{
|
||
12 years ago
|
// Restore dynamic controller
|
||
|
fActor->clearBodyFlag(NX_BF_KINEMATIC);
|
||
|
NxShape* shape = fActor->getShapes()[0];
|
||
|
shape->setGroup(plSimDefs::kGroupAvatar);
|
||
14 years ago
|
}
|
||
12 years ago
|
|
||
|
// Enable actor collisions
|
||
|
if (fActor->readActorFlag(NX_AF_DISABLE_COLLISION))
|
||
|
fActor->clearActorFlag(NX_AF_DISABLE_COLLISION);
|
||
14 years ago
|
}
|
||
12 years ago
|
|
||
|
void plPXPhysicalControllerCore::IInformDetectors(bool entering)
|
||
14 years ago
|
{
|
||
12 years ago
|
static const NxU32 kDetectorFlag = 1<<plSimDefs::kGroupDetector;
|
||
|
static const int kNumShapes = 30;
|
||
|
|
||
|
DetectorLog("Informing from plPXPhysicalControllerCore::IInformDetectors");
|
||
|
|
||
|
NxShape* shapes[kNumShapes];
|
||
|
NxCapsule capsule;
|
||
|
GetWorldSpaceCapsule(capsule);
|
||
|
NxScene* scene = plSimulationMgr::GetInstance()->GetScene(fWorldKey);
|
||
|
int numCollided = scene->overlapCapsuleShapes(capsule, NX_ALL_SHAPES, kNumShapes, shapes, NULL, kDetectorFlag, NULL, true);
|
||
|
for (int i = 0; i < numCollided; ++i)
|
||
14 years ago
|
{
|
||
12 years ago
|
plPXPhysical* physical = (plPXPhysical*)shapes[i]->getActor().userData;
|
||
|
if (physical && physical->DoReportOn(plSimDefs::kGroupAvatar))
|
||
|
{
|
||
|
plCollideMsg* msg = TRACKED_NEW plCollideMsg();
|
||
|
msg->fOtherKey = fOwner;
|
||
|
msg->fEntering = entering;
|
||
|
msg->AddReceiver(physical->GetObjectKey());
|
||
|
|
||
|
// Queue until the next sim step
|
||
|
fQueuedCollideMsgs.push_back(msg);
|
||
|
}
|
||
14 years ago
|
}
|
||
12 years ago
|
|
||
|
DetectorLog("Done informing from plPXPhysicalControllerCore::IInformDetectors");
|
||
14 years ago
|
}
|
||
|
|
||
12 years ago
|
void plPXPhysicalControllerCore::ICreateController(const hsPoint3& pos)
|
||
14 years ago
|
{
|
||
12 years ago
|
NxScene* scene = plSimulationMgr::GetInstance()->GetScene(fWorldKey);
|
||
|
|
||
|
if (fKinematicCCT)
|
||
|
{
|
||
|
// Use PhysX character controller
|
||
|
NxCapsuleControllerDesc desc;
|
||
|
desc.position.x = pos.fX;
|
||
|
desc.position.y = pos.fY;
|
||
|
desc.position.z = pos.fZ + kCCTZOffset;
|
||
|
desc.upDirection = NX_Z;
|
||
|
desc.slopeLimit = kSlopeLimit;
|
||
|
desc.skinWidth = kCCTSkinWidth;
|
||
|
desc.stepOffset = kCCTStepOffset;
|
||
|
desc.callback = &gControllerHitReport;
|
||
|
desc.userData = this;
|
||
|
desc.radius = fRadius;
|
||
|
desc.height = fHeight;
|
||
|
desc.interactionFlag = NXIF_INTERACTION_EXCLUDE;
|
||
|
fController = (NxCapsuleController*)gControllerMgr.createController(scene, desc);
|
||
|
fActor = fController->getActor();
|
||
|
|
||
|
// Set the actor group - Dynamics are in group 1 and will report on everything in group 0.
|
||
|
// We don't want notifications
|
||
|
fActor->setGroup(2);
|
||
|
|
||
|
// Set the shape group. Not used by the NxController itself,
|
||
|
// But required for correct group interactions in the simulation.
|
||
|
NxShape* shape = fActor->getShapes()[0];
|
||
|
shape->setGroup(plSimDefs::kGroupAvatarKinematic);
|
||
|
|
||
|
// In PhysX 2, the kinematic actors scale factor isn't exposed.
|
||
|
// It is hardcoded at 0.8 which doesn't suit, so we have to manually adjust its dimensions.
|
||
|
float kineRadius = fRadius + kCCTSkinWidth;
|
||
12 years ago
|
float kineHeight = fHeight;
|
||
12 years ago
|
NxCapsuleShape* capShape = shape->isCapsule();
|
||
12 years ago
|
if (fHuman)
|
||
|
{
|
||
|
kineHeight += kPhysHeightCorrection;
|
||
|
capShape->setLocalPosition(NxVec3(0.0f, (kPhysHeightCorrection / 2.0f), 0.0f));
|
||
|
}
|
||
12 years ago
|
capShape->setDimensions(kineRadius, kineHeight);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
// Use dynamic actor for the character controller
|
||
|
NxCapsuleShapeDesc capDesc;
|
||
|
capDesc.materialIndex = plSimulationMgr::GetInstance()->GetMaterialIdx(scene, 0.0f, 0.0f);
|
||
|
capDesc.radius = fRadius + kCCTSkinWidth;
|
||
|
capDesc.height = fHeight + kPhysHeightCorrection;
|
||
|
|
||
|
NxBodyDesc bodyDesc;
|
||
|
bodyDesc.mass = kAvatarMass;
|
||
|
bodyDesc.flags = NX_BF_DISABLE_GRAVITY;
|
||
|
bodyDesc.flags |= NX_BF_FROZEN_ROT;
|
||
|
|
||
|
if (fEnabled)
|
||
|
capDesc.group = plSimDefs::kGroupAvatar;
|
||
|
else
|
||
|
{
|
||
|
bodyDesc.flags |= NX_BF_KINEMATIC;
|
||
|
capDesc.group = plSimDefs::kGroupAvatarKinematic;
|
||
|
}
|
||
|
|
||
|
NxActorDesc actorDesc;
|
||
|
actorDesc.shapes.pushBack(&capDesc);
|
||
|
actorDesc.body = &bodyDesc;
|
||
|
actorDesc.group = 2;
|
||
|
|
||
|
actorDesc.globalPose.M.rotX(NxHalfPiF32);
|
||
|
actorDesc.globalPose.t.x = pos.fX;
|
||
|
actorDesc.globalPose.t.y = pos.fY;
|
||
|
actorDesc.globalPose.t.z = pos.fZ + kPhysZOffset;
|
||
|
|
||
|
fActor = scene->createActor(actorDesc);
|
||
|
}
|
||
|
|
||
|
fSeeking = false;
|
||
|
|
||
|
// Create proxy for the debug display
|
||
|
/* FIXME
|
||
|
// the avatar proxy doesn't seem to work... not sure why?
|
||
|
hsColorRGBA physColor;
|
||
|
hsScalar opac = 1.0f;
|
||
|
|
||
|
// local avatar is light purple and transparent
|
||
|
physColor.Set(.2f, .1f, .2f, 1.f);
|
||
|
opac = 0.8f;
|
||
|
|
||
|
fProxyGen = TRACKED_NEW plPhysicalProxy(hsColorRGBA().Set(0,0,0,1.f), physColor, opac);
|
||
|
fProxyGen->Init(this);
|
||
|
*/
|
||
14 years ago
|
}
|
||
12 years ago
|
void plPXPhysicalControllerCore::IDeleteController()
|
||
14 years ago
|
{
|
||
12 years ago
|
if (fKinematicCCT)
|
||
|
{
|
||
|
gControllerMgr.releaseController(*fController);
|
||
|
fController = nil;
|
||
|
}
|
||
14 years ago
|
else
|
||
|
{
|
||
12 years ago
|
NxScene* scene = plSimulationMgr::GetInstance()->GetScene(fWorldKey);
|
||
|
scene->releaseActor(*fActor);
|
||
14 years ago
|
}
|
||
|
|
||
12 years ago
|
fActor = nil;
|
||
|
plSimulationMgr::GetInstance()->ReleaseScene(fWorldKey);
|
||
|
}
|
||
14 years ago
|
|
||
12 years ago
|
void plPXPhysicalControllerCore::IDispatchQueuedMsgs()
|
||
14 years ago
|
{
|
||
12 years ago
|
int numMsgs = fQueuedCollideMsgs.size();
|
||
|
if (numMsgs)
|
||
|
{
|
||
|
plSimulationMgr* simMgr = plSimulationMgr::GetInstance();
|
||
|
for (int i = 0; i < numMsgs; ++i)
|
||
|
simMgr->AddCollisionMsg(fQueuedCollideMsgs[i]);
|
||
14 years ago
|
|
||
12 years ago
|
fQueuedCollideMsgs.clear();
|
||
|
}
|
||
|
}
|
||
|
void plPXPhysicalControllerCore::IProcessDynamicHits()
|
||
|
{
|
||
|
int numHits = fDynamicHits.size();
|
||
|
if (numHits)
|
||
14 years ago
|
{
|
||
12 years ago
|
plPXPhysical* phys;
|
||
|
plSimulationMgr* simMgr = plSimulationMgr::GetInstance();
|
||
|
for (int i = 0; i < numHits; ++i)
|
||
|
{
|
||
|
phys = fDynamicHits[i];
|
||
14 years ago
|
|
||
12 years ago
|
// If this is the local avatar, we need to take ownership of this dynamic if we haven't already
|
||
|
if (fLOSDB == plSimDefs::kLOSDBLocalAvatar && !phys->IsLocallyOwned() && !phys->GetProperty(plSimulationInterface::kNoOwnershipChange))
|
||
|
{
|
||
|
plSynchedObject* obj = plSynchedObject::ConvertNoRef(phys->GetObjectKey()->ObjectIsLoaded());
|
||
|
obj->SetNetGroupConstant(plNetGroup::kNetGroupLocalPhysicals);
|
||
|
|
||
|
plSetNetGroupIDMsg* setNetGroupID = TRACKED_NEW plSetNetGroupIDMsg;
|
||
|
setNetGroupID->fId = plNetGroup::kNetGroupRemotePhysicals;
|
||
|
setNetGroupID->SetBCastFlag(plMessage::kNetPropagate | plMessage::kNetForce);
|
||
|
setNetGroupID->SetBCastFlag(plMessage::kLocalPropagate, false);
|
||
|
setNetGroupID->Send(obj->GetKey());
|
||
|
}
|
||
|
|
||
|
phys->ApplyHitForce();
|
||
|
simMgr->ConsiderSynch(phys, nil);
|
||
|
}
|
||
|
fDynamicHits.clear();
|
||
14 years ago
|
}
|
||
|
}
|
||
12 years ago
|
|
||
14 years ago
|
#ifndef PLASMA_EXTERNAL_RELEASE
|
||
|
#include "../plPipeline/plDebugText.h"
|
||
|
|
||
|
void plPXPhysicalControllerCore::IDrawDebugDisplay()
|
||
|
{
|
||
|
plDebugText &debugTxt = plDebugText::Instance();
|
||
|
char strBuf[ 2048 ];
|
||
|
int lineHeight = debugTxt.GetFontSize() + 4;
|
||
|
UInt32 scrnWidth, scrnHeight;
|
||
|
|
||
|
debugTxt.GetScreenSize( &scrnWidth, &scrnHeight );
|
||
|
int y = 10;
|
||
|
int x = 10;
|
||
|
|
||
|
sprintf(strBuf, "Controller Count: %d", gControllers.size());
|
||
|
debugTxt.DrawString(x, y, strBuf);
|
||
|
y += lineHeight;
|
||
|
|
||
|
debugTxt.DrawString(x, y, "Avatar Collisions:");
|
||
|
y += lineHeight;
|
||
|
|
||
|
int i;
|
||
|
for (i = 0; i < fDbgCollisionInfo.GetCount(); i++)
|
||
|
{
|
||
|
hsVector3 normal = fDbgCollisionInfo[i].fNormal;
|
||
|
char *overlapStr = fDbgCollisionInfo[i].fOverlap ? "yes" : "no";
|
||
|
hsScalar angle = hsScalarRadToDeg(hsACosine(normal * hsVector3(0, 0, 1)));
|
||
|
sprintf(strBuf, " Obj: %s, Normal: (%.2f, %.2f, %.2f), Angle(%.1f), Overlap(%3s)",
|
||
|
fDbgCollisionInfo[i].fSO->GetKeyName(),
|
||
|
normal.fX, normal.fY, normal.fZ, angle, overlapStr);
|
||
|
debugTxt.DrawString(x, y, strBuf);
|
||
|
y += lineHeight;
|
||
|
}
|
||
|
}
|
||
|
#endif PLASMA_EXTERNAL_RELEASE
|
||
12 years ago
|
|