Skip to content

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.

4.6 KB
Raw Blame
// 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

#ifndef DY_DYNAMICS_BASE_H
#define DY_DYNAMICS_BASE_H

#include "DyContext.h"
#include "DyThreadContext.h"
#include "DyConstraint.h"
#include "PxvNphaseImplementationContext.h"
#include "PxsIslandManagerTypes.h"
#include "PxsIslandSim.h"
#include "solver/PxSolverDefs.h"

namespace physx
{
namespace Cm
{
	class FlushPool;
}

namespace IG
{
	class SimpleIslandManager;
}

namespace Dy
{
struct ConstraintLess
{
	bool operator()(const PxSolverConstraintDesc& left, const PxSolverConstraintDesc& right) const
	{
		return reinterpret_cast<Constraint*>(left.constraint)->index > reinterpret_cast<Constraint*>(right.constraint)->index;
	}
};

// PT: base class containing code and data shared between PGS and TGS. Ideally this would just be named "DynamicsContext" and the PGS
// context would have been renamed "DynamicsPGSContext" (to match DynamicsTGSContext) but let's limit the gratuitous changes for now.
class DynamicsContextBase : public Context
{
	PX_NOCOPY(DynamicsContextBase)
public:
	DynamicsContextBase(PxcNpMemBlockPool* memBlockPool,
						Cm::FlushPool& taskPool,
						PxvSimStats& simStats,
						Cm::VirtualAllocatorCallback& allocator,
						PxsMaterialManager* materialManager,
						IG::SimpleIslandManager& islandManager,
						PxU64 contextID,
						PxReal maxBiasCoefficient,
						PxReal lengthScale,
						PxSceneFlags sceneFlags);

	virtual	~DynamicsContextBase();

	/**
	\brief Allocates and returns a thread context object.
	\return A thread context.
	*/
	PX_FORCE_INLINE ThreadContext*		getThreadContext()	{ return mThreadContextPool.get();	}

	/**
	\brief Returns a thread context to the thread context pool.
	\param[in] context The thread context to return to the thread context pool.
	*/
	PX_FORCE_INLINE void				putThreadContext(ThreadContext* context)	{ mThreadContextPool.put(context);	}

	PX_FORCE_INLINE ThresholdStream&	getThresholdStream()			{ return mThresholdStream; }

	PX_FORCE_INLINE PxvSimStats&		getSimStats()					{ return mSimStats;			}
	PX_FORCE_INLINE Cm::FlushPool&		getTaskPool()					{ return mTaskPool;			}
	PX_FORCE_INLINE	PxU32				getKinematicCount()		const	{ return mKinematicCount;	}

	PxcThreadCoherentCache<ThreadContext, PxcNpMemBlockPool> mThreadContextPool;	// A thread context pool

	PxsMaterialManager*					mMaterialManager;
	Cm::FlushPool&						mTaskPool;
	PxsContactManagerOutputIterator		mOutputIterator;

	PxArray<PxConstraintBatchHeader>	mContactConstraintBatchHeaders;	// An array of contact constraint batch headers
	PxArray<Cm::SpatialVector>			mMotionVelocityArray;			// Array of motion velocities for all bodies in the scene.
	PxArray<PxsBodyCore*>				mBodyCoreArray;					// Array of body core pointers for all bodies in the scene.
	PxArray<PxsRigidBody*>				mRigidBodyArray;				// Array of rigid body pointers for all bodies in the scene.
	PxArray<FeatherstoneArticulation*>	mArticulationArray;				// Array of articulation pointers for all articulations in the scene.

	ThresholdStream						mExceededForceThresholdStream[2]; // this store previous and current exceeded force thresholdStream	
	PxArray<PxU32>						mExceededForceThresholdStreamMask;
	PxArray<PxU32>						mSolverBodyRemapTable;	// Remaps from the "active island" index to the index within a solver island
	PxArray<PxU32>						mNodeIndexArray;		// island node index
	PxArray<PxsIndexedContactManager>	mContactList;

	PxU32	mKinematicCount;		// The total number of kinematic bodies in the scene
	PxI32	mThresholdStreamOut;	// Atomic counter for the number of threshold stream elements.
	PxU32	mCurrentIndex;			// this is the index point to the current exceeded force threshold stream

protected:
	PxU32	reserveSharedSolverConstraintsArrays(const IG::IslandSim& islandSim, PxU32 maxArticulationLinks);

	bool	updateShared(PxvNphaseImplementationContext* nphase, PxReal dt, const PxVec3& gravity);

	PxU32	iterateIslandsContactEdges(	const PxsIslandIndices& counts, PxU32 nbIslands, const IG::IslandId* PX_RESTRICT const islandIds,
										PxsIndexedContactManager* PX_RESTRICT indexedManagers, const PxU32* PX_RESTRICT bodyRemapTable);
	void	iterateIslandsNodes(const PxsIslandIndices& counts, PxU32 nbIslands, const IG::IslandId* PX_RESTRICT const islandIds,
								PxsBodyCore** PX_RESTRICT bodyArrayPtr, PxsRigidBody** PX_RESTRICT rigidBodyPtr,
								FeatherstoneArticulation** PX_RESTRICT articulationPtr,
								PxU32* PX_RESTRICT bodyRemapTable, PxU32* PX_RESTRICT nodeIndexArray);
};

}
}

#endif