CUGL 2.5
Cornell University Game Library
|
#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 > ¶m) |
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< NetPhysicsController > | alloc (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 |
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.
|
strong |
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.
|
inline |
Deletes this physics controller, disposing of all resources.
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.
obs | the obstacle to acquire |
duration | number of physics steps to hold ownership |
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.
factoryID | The id of the obstacle factory to use |
bytes | The serialized parameters taken by the obstacle factory |
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.
obj | The obstacle to interpolate |
param | The target parameters for interpolation |
|
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.
world | The physics world instance to manage |
shortUID | The shortUID assigned by NetEventController |
isHost | Whether this connection is the game host |
linkFunc | Function for linking obstacle to scene node |
|
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.
void cugl::physics2::net::NetPhysicsController::dispose | ( | ) |
Disposes the physics controller, releasing all resources.
This controller can be safely reinitialized
|
inline |
Returns the vector of generated events to be sent.
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.
world | The physics world instance to manage |
shortUID | The shortUID assigned by NetEventController |
isHost | Whether this connection is the game host |
linkFunc | Function for linking obstacle to scene node |
|
protected |
Returns the result of linear object interpolation.
Formula: (target-source)/stepsLeft + source
|
inline |
Returns true if the given obstacle is being interpolated.
obs | the obstacle to query |
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.
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.
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.
type | the type of synchronization |
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.
event | The event to be processed |
void cugl::physics2::net::NetPhysicsController::processPhysSyncEvent | ( | const std::shared_ptr< PhysSyncEvent > & | event | ) |
Processes a physics synchronization event.
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.
obs | the obstacle to release |
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.
obs | the obstacle to remove |
void cugl::physics2::net::NetPhysicsController::reset | ( | ) |
Resets the physics controller.
void cugl::physics2::net::NetPhysicsController::updateSimulation | ( | ) |
Updates the physics controller.
|
protected |
Cache of all on-ogoing interpolations
|
protected |
Temporary cache for removal after traversal
|
protected |
Whether this instance acts as host.
|
protected |
Total number of interpolations done
|
protected |
Whether to display debug information for the interpolation
|
protected |
The current interpolation method
|
protected |
Function for linking newly added obstacle to a scene node
|
protected |
The next available obstacle ID
|
protected |
Vector of attached obstacle factories for obstacle creation
|
protected |
Vector of generated events to be sent
|
protected |
Total number of overriden interpolations
|
protected |
Local map from added obstacles to scene nodes
|
protected |
Total number of steps interpolated
|
protected |
The physics world instance