A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
// SPDX-FileCopyrightText: Copyright (c) 2008-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
#include "geometry/PxCapsuleGeometry.h"
#include "PxRigidDynamic.h"
#include "PxShape.h"
#include "characterkinematic/PxController.h"
#include "CctCapsuleController.h"
#include "CctCharacterControllerManager.h"
using namespace physx;
using namespace Cct;
static PX_FORCE_INLINE float CCTtoProxyRadius(float r, PxF32 coeff) { return r * coeff; }
static PX_FORCE_INLINE float CCTtoProxyHeight(float h, PxF32 coeff) { return 0.5f * h * coeff; }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
CapsuleController::CapsuleController(const PxControllerDesc& desc, PxPhysics& sdk, PxScene* s) : Controller(desc, s)
{
mType = PxControllerShapeType::eCAPSULE;
const PxCapsuleControllerDesc& cc = static_cast<const PxCapsuleControllerDesc&>(desc);
mRadius = cc.radius;
mHeight = cc.height;
mClimbingMode = cc.climbingMode;
// Create kinematic actor under the hood
PxCapsuleGeometry capsGeom;
capsGeom.radius = CCTtoProxyRadius(cc.radius, mProxyScaleCoeff);
capsGeom.halfHeight = CCTtoProxyHeight(cc.height, mProxyScaleCoeff);
createProxyActor(sdk, capsGeom, *desc.material, desc.clientID);
}
CapsuleController::~CapsuleController()
{
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CapsuleController::invalidateCache()
{
if(mManager->mLockingEnabled)
mWriteLock.lock();
mCctModule.voidTestCache();
if(mManager->mLockingEnabled)
mWriteLock.unlock();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool CapsuleController::getWorldBox(PxExtendedBounds3& box) const
{
setCenterExtents(box, mPosition, PxVec3(mRadius, mRadius+mHeight*0.5f, mRadius));
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool CapsuleController::setRadius(PxF32 r)
{
// Set radius for CCT volume
mRadius = r;
// Set radius for kinematic proxy
if(mKineActor)
{
PxShape* shape = getKineShape();
const PxGeometry& geom = shape->getGeometry();
PX_ASSERT(geom.getType() == PxGeometryType::eCAPSULE);
PxCapsuleGeometry cg = static_cast<const PxCapsuleGeometry&>(geom);
cg.radius = CCTtoProxyRadius(r, mProxyScaleCoeff);
shape->setGeometry(cg);
}
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool CapsuleController::setHeight(PxF32 h)
{
// Set height for CCT volume
mHeight = h;
// Set height for kinematic proxy
if(mKineActor)
{
PxShape* shape = getKineShape();
const PxGeometry& geom = shape->getGeometry();
PX_ASSERT(geom.getType() == PxGeometryType::eCAPSULE);
PxCapsuleGeometry cg = static_cast<const PxCapsuleGeometry&>(geom);
cg.halfHeight = CCTtoProxyHeight(h, mProxyScaleCoeff);
shape->setGeometry(cg);
}
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
PxCapsuleClimbingMode::Enum CapsuleController::getClimbingMode() const
{
return mClimbingMode;
}
bool CapsuleController::setClimbingMode(PxCapsuleClimbingMode::Enum mode)
{
if(mode>=PxCapsuleClimbingMode::eLAST)
return false;
mClimbingMode = mode;
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
PxExtendedVec3 CapsuleController::getFootPosition() const
{
PxExtendedVec3 groundPosition = mPosition; // Middle of the CCT
sub(groundPosition, mUserParams.mUpDirection * (mUserParams.mContactOffset+mRadius+mHeight*0.5f)); // Ground
return groundPosition;
}
bool CapsuleController::setFootPosition(const PxExtendedVec3& position)
{
PxExtendedVec3 centerPosition = position;
add(centerPosition, mUserParams.mUpDirection * (mUserParams.mContactOffset+mRadius+mHeight*0.5f));
return setPosition(centerPosition);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CapsuleController::getCapsule(PxExtendedCapsule& capsule) const
{
// PT: TODO: optimize this
PxExtendedVec3 p0 = mPosition;
PxExtendedVec3 p1 = mPosition;
const PxVec3 extents = mUserParams.mUpDirection*mHeight*0.5f;
sub(p0, extents);
add(p1, extents);
capsule.p0 = p0;
capsule.p1 = p1;
capsule.radius = mRadius;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CapsuleController::resize(PxReal height)
{
const float oldHeight = getHeight();
setHeight(height);
const float delta = height - oldHeight;
PxExtendedVec3 pos = getPosition();
add(pos, mUserParams.mUpDirection * delta * 0.5f);
setPosition(pos);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////