CUGL 2.5
Cornell University Game Library
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
cugl::physics2::net::NetPhysicsController Class Reference

#include <CUNetPhysicsController.h>

Classes

class  TargetParams
 

Public Types

enum class  SyncType : int { OVERRIDE_FULL_SYNC , FULL_SYNC , PRIO_SYNC }
 

Public Member Functions

 NetPhysicsController ()
 
 ~NetPhysicsController ()
 
void dispose ()
 
bool init (std::shared_ptr< NetWorld > &world, Uint32 shortUID, bool isHost, ObstacleLink linkFunc=nullptr)
 
Uint32 attachFactory (std::shared_ptr< ObstacleFactory > fact)
 
ObstacleScene addSharedObstacle (Uint32 factoryID, std::shared_ptr< std::vector< std::byte > > bytes)
 
void removeSharedObstacle (std::shared_ptr< physics2::Obstacle > obs)
 
void acquireObs (std::shared_ptr< physics2::Obstacle > obs, Uint64 duration)
 
void releaseObs (std::shared_ptr< physics2::Obstacle > obs)
 
void ownAll ()
 
bool isInSync (std::shared_ptr< physics2::Obstacle > obs)
 
void addSyncObject (std::shared_ptr< physics2::Obstacle > obj, const std::shared_ptr< TargetParams > &param)
 
std::vector< std::shared_ptr< NetEvent > > & getOutEvents ()
 
void updateSimulation ()
 
void processPhysObstEvent (const std::shared_ptr< PhysObstEvent > &event)
 
void processPhysSyncEvent (const std::shared_ptr< PhysSyncEvent > &event)
 
void packPhysSync (SyncType type)
 
void packPhysObj ()
 
void reset ()
 

Static Public Member Functions

static std::shared_ptr< NetPhysicsControlleralloc (std::shared_ptr< NetWorld > &world, Uint32 shortUID, bool isHost, ObstacleLink linkFunc=nullptr)
 

Protected Member Functions

float interpolate (int stepsLeft, float target, float source)
 

Protected Attributes

Uint32 _itprMethod
 
bool _itprDebug
 
long _itprCount
 
long _ovrdCount
 
long _stepSum
 
bool _isHost
 
Uint64 _objRotation
 
std::shared_ptr< NetWorld_world
 
std::unordered_map< std::shared_ptr< physics2::Obstacle >, std::shared_ptr< TargetParams > > _cache
 
std::vector< std::shared_ptr< physics2::Obstacle > > _deleteCache
 
std::vector< std::shared_ptr< ObstacleFactory > > _obstacleFacts
 
ObstacleLink _linkSceneToObsFunc
 
std::unordered_map< std::shared_ptr< physics2::Obstacle >, std::shared_ptr< scene2::SceneNode > > _sharedObsToNodeMap
 
std::vector< std::shared_ptr< NetEvent > > _outEvents
 

Detailed Description

This class is the physics controller for the networked physics library.

This class holds a reference to a NetWorld instance. It is built on top of that class, and is responsible for all networked physics synchronization and object management.

Member Enumeration Documentation

◆ SyncType

The event types for physics synchronization.

Enumerator
OVERRIDE_FULL_SYNC 

Synchronize all objects (shared or unshared) in the world/

Objects that other clients do not recognize will be ignored.

FULL_SYNC 

Synchronize all shared objects in the world

PRIO_SYNC 

Prioritize syncing volatile objects

Constructor & Destructor Documentation

◆ NetPhysicsController()

cugl::physics2::net::NetPhysicsController::NetPhysicsController ( )

Creates a degenerate physics controller with default values.

NEVER USE A CONSTRUCTOR WITH NEW. If you want to allocate an asset on the heap, use one of the static constructors instead.

◆ ~NetPhysicsController()

cugl::physics2::net::NetPhysicsController::~NetPhysicsController ( )
inline

Deletes this physics controller, disposing of all resources.

Member Function Documentation

◆ acquireObs()

void cugl::physics2::net::NetPhysicsController::acquireObs ( std::shared_ptr< physics2::Obstacle obs,
Uint64  duration 
)

Acquires the ownership of the object for an amount of time

This method is used for one client to obtain ownership of the obstacle. This ownership can be temporary, measured in terms of physics steps. If the duration is 0, ownership will last until it is released. When this method is called by the host, the duration is permenant.

Normally, the host would own all objects upon their creation. This method allows any client to be the owner of an obstacle, therefore potentially reducing response time for client controlled objects.

WARNING: only one client should call this method on an object within a period of time to avoid race conditions.

Parameters
obsthe obstacle to acquire
durationnumber of physics steps to hold ownership

◆ addSharedObstacle()

ObstacleScene cugl::physics2::net::NetPhysicsController::addSharedObstacle ( Uint32  factoryID,
std::shared_ptr< std::vector< std::byte > >  bytes 
)

Adds a shared obstacle to the physics world.

This method is used to add a shared obstacle across all clients. Users can uses the returned references to manually link the obstacles to scene graphs, or for custom obstacle setups.

Parameters
factoryIDThe id of the obstacle factory to use
bytesThe serialized parameters taken by the obstacle factory
Returns
A pair of the added obstacle and its corresponding scene node

◆ addSyncObject()

void cugl::physics2::net::NetPhysicsController::addSyncObject ( std::shared_ptr< physics2::Obstacle obj,
const std::shared_ptr< TargetParams > &  param 
)

Adds an object to interpolate with the given target parameters.

This method is used for error smoothing.

Parameters
objThe obstacle to interpolate
paramThe target parameters for interpolation

◆ alloc()

static std::shared_ptr< NetPhysicsController > cugl::physics2::net::NetPhysicsController::alloc ( std::shared_ptr< NetWorld > &  world,
Uint32  shortUID,
bool  isHost,
ObstacleLink  linkFunc = nullptr 
)
inlinestatic

Returns a newly allocated physics controller with the given values.

If the function linkFunc is provided, the controller will automatically link newly added obstacles to their corresponding scene nodes by calling this function. The controller will also handle removal of scenes nodes when removing obstacles.

On the other hand, if linkFunc is nullptr, the user will be responsible for linking obstacles to scene nodes. In that case, the user is recommended to use custom NetEvent types to handle obstacle creation without the use of the physics controller.

Parameters
worldThe physics world instance to manage
shortUIDThe shortUID assigned by NetEventController
isHostWhether this connection is the game host
linkFuncFunction for linking obstacle to scene node
Returns
a newly allocated physics controller with the given values.

◆ attachFactory()

Uint32 cugl::physics2::net::NetPhysicsController::attachFactory ( std::shared_ptr< ObstacleFactory fact)
inline

Add a custom obstacle factory to the controller.

This method allows users to leverage automatic object synchronization to add obstacles to the physics world. See ObstacleFactory for how to implement a custom obstacle factory.

Returns
The id of the added obstacle factory

◆ dispose()

void cugl::physics2::net::NetPhysicsController::dispose ( )

Disposes the physics controller, releasing all resources.

This controller can be safely reinitialized

◆ getOutEvents()

std::vector< std::shared_ptr< NetEvent > > & cugl::physics2::net::NetPhysicsController::getOutEvents ( )
inline

Returns the vector of generated events to be sent.

Returns
the vector of generated events to be sent.

◆ init()

bool cugl::physics2::net::NetPhysicsController::init ( std::shared_ptr< NetWorld > &  world,
Uint32  shortUID,
bool  isHost,
ObstacleLink  linkFunc = nullptr 
)

Initializes a new physics controller with the given values.

If the function linkFunc is provided, the controller will automatically link newly added obstacles to their corresponding scene nodes by calling this function. The controller will also handle removal of scenes nodes when removing obstacles.

On the other hand, if linkFunc is nullptr, the user will be responsible for linking obstacles to scene nodes. In that case, the user is recommended to use custom NetEvent types to handle obstacle creation without the use of the physics controller.

Parameters
worldThe physics world instance to manage
shortUIDThe shortUID assigned by NetEventController
isHostWhether this connection is the game host
linkFuncFunction for linking obstacle to scene node
Returns
true if the controller was initialized successfully

◆ interpolate()

float cugl::physics2::net::NetPhysicsController::interpolate ( int  stepsLeft,
float  target,
float  source 
)
protected

Returns the result of linear object interpolation.

Formula: (target-source)/stepsLeft + source

Returns
the result of linear object interpolation.

◆ isInSync()

bool cugl::physics2::net::NetPhysicsController::isInSync ( std::shared_ptr< physics2::Obstacle obs)
inline

Returns true if the given obstacle is being interpolated.

Parameters
obsthe obstacle to query
Returns
true if the given obstacle is being interpolated.

◆ ownAll()

void cugl::physics2::net::NetPhysicsController::ownAll ( )

Makes this client the owner of all objects in the simulation.

This method does not actually send any information to the other clients on the network. It should be used for initial objects only.

◆ packPhysObj()

void cugl::physics2::net::NetPhysicsController::packPhysObj ( )

Packs any changed object information

This method checks the world for any dirty objects (e.g. objects that have changed state outside of the simulation). If so, it packages that information as an event to send out to other machines on the network.

◆ packPhysSync()

void cugl::physics2::net::NetPhysicsController::packPhysSync ( SyncType  type)

Packs object data for synchronization.

This data will be added to getOutEvents, which is the queue of information to be sent over the network.

This method can be used to prompt the physics controller to synchronize objects. It is called automatically by NetEventController, but additional calls to it can help fix potential desyncing.

Parameters
typethe type of synchronization

◆ processPhysObstEvent()

void cugl::physics2::net::NetPhysicsController::processPhysObstEvent ( const std::shared_ptr< PhysObstEvent > &  event)

Processes a physics object synchronization event.

This method is called automatically by the NetEventController.

Parameters
eventThe event to be processed

◆ processPhysSyncEvent()

void cugl::physics2::net::NetPhysicsController::processPhysSyncEvent ( const std::shared_ptr< PhysSyncEvent > &  event)

Processes a physics synchronization event.

◆ releaseObs()

void cugl::physics2::net::NetPhysicsController::releaseObs ( std::shared_ptr< physics2::Obstacle obs)

Releases the ownership of the object.

This method is the opposite of acquireObs. When called by a client, it will return ownership to the host. This method has no effect if the client does not have ownership of that obstacle or if it is called by the host.

Parameters
obsthe obstacle to release

◆ removeSharedObstacle()

void cugl::physics2::net::NetPhysicsController::removeSharedObstacle ( std::shared_ptr< physics2::Obstacle obs)

Removes a shared obstacle from the physics world.

If a linking function was provided, the scene node will also be removed.

Parameters
obsthe obstacle to remove

◆ reset()

void cugl::physics2::net::NetPhysicsController::reset ( )

Resets the physics controller.

◆ updateSimulation()

void cugl::physics2::net::NetPhysicsController::updateSimulation ( )

Updates the physics controller.

Member Data Documentation

◆ _cache

std::unordered_map<std::shared_ptr<physics2::Obstacle>,std::shared_ptr<TargetParams> > cugl::physics2::net::NetPhysicsController::_cache
protected

Cache of all on-ogoing interpolations

◆ _deleteCache

std::vector<std::shared_ptr<physics2::Obstacle> > cugl::physics2::net::NetPhysicsController::_deleteCache
protected

Temporary cache for removal after traversal

◆ _isHost

bool cugl::physics2::net::NetPhysicsController::_isHost
protected

Whether this instance acts as host.

◆ _itprCount

long cugl::physics2::net::NetPhysicsController::_itprCount
protected

Total number of interpolations done

◆ _itprDebug

bool cugl::physics2::net::NetPhysicsController::_itprDebug
protected

Whether to display debug information for the interpolation

◆ _itprMethod

Uint32 cugl::physics2::net::NetPhysicsController::_itprMethod
protected

The current interpolation method

◆ _linkSceneToObsFunc

ObstacleLink cugl::physics2::net::NetPhysicsController::_linkSceneToObsFunc
protected

Function for linking newly added obstacle to a scene node

◆ _objRotation

Uint64 cugl::physics2::net::NetPhysicsController::_objRotation
protected

The next available obstacle ID

◆ _obstacleFacts

std::vector<std::shared_ptr<ObstacleFactory> > cugl::physics2::net::NetPhysicsController::_obstacleFacts
protected

Vector of attached obstacle factories for obstacle creation

◆ _outEvents

std::vector<std::shared_ptr<NetEvent> > cugl::physics2::net::NetPhysicsController::_outEvents
protected

Vector of generated events to be sent

◆ _ovrdCount

long cugl::physics2::net::NetPhysicsController::_ovrdCount
protected

Total number of overriden interpolations

◆ _sharedObsToNodeMap

std::unordered_map<std::shared_ptr<physics2::Obstacle>, std::shared_ptr<scene2::SceneNode> > cugl::physics2::net::NetPhysicsController::_sharedObsToNodeMap
protected

Local map from added obstacles to scene nodes

◆ _stepSum

long cugl::physics2::net::NetPhysicsController::_stepSum
protected

Total number of steps interpolated

◆ _world

std::shared_ptr<NetWorld> cugl::physics2::net::NetPhysicsController::_world
protected

The physics world instance


The documentation for this class was generated from the following file: