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

#include <CUNetWorld.h>

Inheritance diagram for cugl::physics2::net::NetWorld:
cugl::physics2::ObstacleWorld

Public Member Functions

 NetWorld ()
 
 ~NetWorld ()
 
void dispose ()
 
bool initWithUUID (const Rect bounds, std::string uuid)
 
bool initWithUUID (const Rect bounds, const Vec2 gravity, std::string uuid)
 
std::string getUUID ()
 
Uint32 getshortUID ()
 
void setshortUID (Uint32 sid)
 
Uint64 initObstacle (const std::shared_ptr< Obstacle > &obj)
 
Uint64 placeObstacle (const std::shared_ptr< Obstacle > &obj)
 
void addObstacle (const std::shared_ptr< Obstacle > &obj) override
 
void removeObstacle (const std::shared_ptr< Obstacle > &obj) override
 
Uint64 initJoint (const std::shared_ptr< Joint > &joint)
 
Uint64 placeJoint (const std::shared_ptr< Joint > &joint)
 
virtual void addJoint (const std::shared_ptr< Joint > &joint) override
 
virtual void removeJoint (const std::shared_ptr< Joint > &joint) override
 
std::shared_ptr< ObstaclegetNextObstacle ()
 
std::shared_ptr< ObstaclegetObstacle (Uint64 oid)
 
Sint64 getObstacleId (const std::shared_ptr< Obstacle > &obs)
 
std::shared_ptr< JointgetJoint (Uint64 jid)
 
Sint64 getJointId (const std::shared_ptr< Joint > &joint)
 
const std::unordered_map< Uint64, std::shared_ptr< physics2::Obstacle > > & getObstacleMap ()
 
const std::unordered_map< std::shared_ptr< physics2::Obstacle >, Uint64 > & getObstacleIds ()
 
std::unordered_map< std::shared_ptr< physics2::Obstacle >, Uint64 > & getOwnedObstacles ()
 
const std::unordered_map< Uint64, std::shared_ptr< physics2::Joint > > & getJointMap ()
 
const std::unordered_map< std::shared_ptr< physics2::Joint >, Uint64 > & getJointIds ()
 
std::unordered_map< std::shared_ptr< physics2::Joint >, Uint64 > & getOwnedJoints ()
 
void SayGoodbye (b2Joint *joint) override
 
- Public Member Functions inherited from cugl::physics2::ObstacleWorld
 ObstacleWorld ()
 
 ~ObstacleWorld ()
 
void dispose ()
 
bool init (const Rect bounds)
 
bool init (const Rect bounds, const Vec2 gravity)
 
b2World * getWorld ()
 
bool isLockStep () const
 
void setLockStep (bool flag)
 
float getStepsize () const
 
void setStepsize (float step)
 
int getVelocityIterations () const
 
void setVelocityIterations (int velocity)
 
int getPositionIterations () const
 
void setPositionIterations (int position)
 
const Vec2 getGravity () const
 
void setGravity (const Vec2 gravity)
 
void update (float dt)
 
const Rect getBounds () const
 
bool inBounds (Obstacle *obj)
 
const std::unordered_set< std::shared_ptr< Obstacle > > & getObstacles () const
 
virtual void addObstacle (const std::shared_ptr< Obstacle > &obj)
 
virtual void removeObstacle (const std::shared_ptr< Obstacle > &obj)
 
const std::unordered_set< std::shared_ptr< Joint > > getJoints () const
 
virtual void addJoint (const std::shared_ptr< Joint > &joint)
 
virtual void removeJoint (const std::shared_ptr< Joint > &joint)
 
void garbageCollect ()
 
void clear ()
 
void activateCollisionCallbacks (bool flag)
 
bool enabledCollisionCallbacks () const
 
void BeginContact (b2Contact *contact) override
 
void EndContact (b2Contact *contact) override
 
void PreSolve (b2Contact *contact, const b2Manifold *oldManifold) override
 
void PostSolve (b2Contact *contact, const b2ContactImpulse *impulse) override
 
void activateFilterCallbacks (bool flag)
 
bool enabledFilterCallbacks () const
 
bool ShouldCollide (b2Fixture *fixtureA, b2Fixture *fixtureB) override
 
void activateDestructionCallbacks (bool flag)
 
bool enabledDestructionCallbacks () const
 
virtual void SayGoodbye (b2Joint *joint) override
 
virtual void SayGoodbye (b2Fixture *fixture) override
 
void queryAABB (std::function< bool(b2Fixture *fixture)> callback, const Rect aabb) const
 
void rayCast (std::function< float(b2Fixture *fixture, const Vec2 point, const Vec2 normal, float fraction)> callback, const Vec2 point1, const Vec2 point2) const
 

Static Public Member Functions

static std::shared_ptr< NetWorldalloc (const Rect bounds)
 
static std::shared_ptr< NetWorldalloc (const Rect bounds, const Vec2 gravity)
 
static std::shared_ptr< NetWorldallocWithUUID (const Rect bounds, std::string UUID)
 
static std::shared_ptr< NetWorldallocWithUUID (const Rect bounds, const Vec2 gravity, std::string UUID)
 
- Static Public Member Functions inherited from cugl::physics2::ObstacleWorld
static std::shared_ptr< ObstacleWorldalloc (const Rect bounds)
 
static std::shared_ptr< ObstacleWorldalloc (const Rect bounds, const Vec2 gravity)
 

Protected Member Functions

void activateObstacle (Uint64 oid, const std::shared_ptr< Obstacle > &obj)
 
void activateJoint (Uint64 jid, const std::shared_ptr< Joint > &joint)
 

Protected Attributes

std::string _uuid
 
Uint32 _shortUID
 
std::unordered_map< std::shared_ptr< physics2::Obstacle >, Uint64 > _obsToId
 
std::unordered_map< Uint64, std::shared_ptr< physics2::Obstacle > > _idToObs
 
std::unordered_map< std::shared_ptr< physics2::Obstacle >, Uint64 > _ownedObs
 
std::unordered_set< std::shared_ptr< physics2::Obstacle > >::iterator _nextObstacle
 
std::unordered_map< std::shared_ptr< physics2::Joint >, Uint64 > _jntToId
 
std::unordered_map< Uint64, std::shared_ptr< physics2::Joint > > _idToJnt
 
std::unordered_map< std::shared_ptr< physics2::Joint >, Uint64 > _ownedJoints
 
Uint32 _nextInitObj
 
Uint32 _nextSharedObj
 
Uint32 _nextInitJoint
 
Uint32 _nextSharedJoint
 
- Protected Attributes inherited from cugl::physics2::ObstacleWorld
b2World * _world
 
bool _lockstep
 
float _stepssize
 
int _itvelocity
 
int _itposition
 
Vec2 _gravity
 
std::unordered_set< std::shared_ptr< Obstacle > > _obstacles
 
std::unordered_map< b2Joint *, std::shared_ptr< Joint > > _joints
 
Rect _bounds
 
bool _collide
 
bool _filters
 
bool _destroy
 

Friends

class NetPhysicsController
 

Additional Inherited Members

- Public Attributes inherited from cugl::physics2::ObstacleWorld
std::function< void(b2Contact *contact)> onBeginContact
 
std::function< void(b2Contact *contact)> onEndContact
 
std::function< void(b2Contact *contact, const b2Manifold *oldManifold)> beforeSolve
 
std::function< void(b2Contact *contact, const b2ContactImpulse *impulse)> afterSolve
 
std::function< bool(b2Fixture *fixtureA, b2Fixture *fixtureB)> shouldCollide
 
std::function< void(b2Fixture *fixture)> destroyFixture
 
std::function< void(b2Joint *joint)> destroyJoint
 

Detailed Description

This class represents a shared, networked physics world.

This class is an extension of ObstacleWorld to support networked physics. Its primary purpose is to assign id numbers to obstacles and joints for the purposes of pointer swizzling. However, it does not explicitly synchronize objects across the network. That is done by other classes.

Constructor & Destructor Documentation

◆ NetWorld()

cugl::physics2::net::NetWorld::NetWorld ( )

Creates a new degenerate NetWorld on the stack.

The scene has no backing Box2d world and must be initialized.

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

◆ ~NetWorld()

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

Deletes this world, disposing all resources

Member Function Documentation

◆ activateJoint()

void cugl::physics2::net::NetWorld::activateJoint ( Uint64  jid,
const std::shared_ptr< Joint > &  joint 
)
protected

Activates a joint in the shared physics world

This method will activate the underlying physics. The will link its associated obstacles. In addition, the physics world will include the joint in its next call to update. Finally, the joint will be assigned an identifier for the purpose of sharing cross-network.

The joint will be retained by this world, preventing it from being garbage collected.

param jid The joint identifier param joint The joint to add

◆ activateObstacle()

void cugl::physics2::net::NetWorld::activateObstacle ( Uint64  oid,
const std::shared_ptr< Obstacle > &  obj 
)
protected

Activates this obstacle in the shared physics world

This method will activate the underlying physics. The obstacle will now have a body. The physics world will include the obstacle in its next call to update. In addition, the obstacle will be assigned an identifier for the purpose of sharing cross-network.

The obstacle will be retained by this world, preventing it from being garbage collected.

param oid The obstacle identifier param obj The obstacle to add

◆ addJoint()

virtual void cugl::physics2::net::NetWorld::addJoint ( const std::shared_ptr< Joint > &  joint)
overridevirtual

Immediately adds a joint to the physics world

This method will fail if the joint obstacles are not in this world. The joint will be activated so that it contains those two obstacles. The physics world will include the joint in its next call to update.

The joint will be retained by this world, preventing it from being garbage collected. This obstacle will also be assigned an object id, using the rules of placeJoint. You can use the method getJointId to find this id.

Parameters
jointThe joint to add

Reimplemented from cugl::physics2::ObstacleWorld.

◆ addObstacle()

void cugl::physics2::net::NetWorld::addObstacle ( const std::shared_ptr< Obstacle > &  obj)
overridevirtual

Immediately adds the obstacle to the physics world

Adding an obstacle activates the underlying physics. It will now have a body. The physics world will include the obstacle in its next call to update.

The obstacle will be retained by this world, preventing it from being garbage collected. This obstacle will also be assigned an object id, using the rules of placeObstacle. You can use the method getObstacleId to find this id.

Parameters
objThe obstacle to add

Reimplemented from cugl::physics2::ObstacleWorld.

◆ alloc() [1/2]

static std::shared_ptr< NetWorld > cugl::physics2::net::NetWorld::alloc ( const Rect  bounds)
inlinestatic

Returns a newly allocated physics world

The specified bounds are in terms of the Box2d world, not the screen. A view attached to this Box2d world should have ways to convert between the coordinate systems.

This constructor will use the default gravitational value. In addition, it will assign the physics world a fresh UUID.

Parameters
boundsThe game bounds in Box2d coordinates
Returns
a newly allocated physics world

◆ alloc() [2/2]

static std::shared_ptr< NetWorld > cugl::physics2::net::NetWorld::alloc ( const Rect  bounds,
const Vec2  gravity 
)
inlinestatic

Returns a newly allocated physics world

The specified bounds are in terms of the Box2d world, not the screen. A view attached to this Box2d world should have ways to convert between the coordinate systems.

This constructor will assign the physics world a fresh UUID.

Parameters
boundsThe game bounds in Box2d coordinates
gravityThe gravitational force on this Box2d world
Returns
a newly allocated physics world

◆ allocWithUUID() [1/2]

static std::shared_ptr< NetWorld > cugl::physics2::net::NetWorld::allocWithUUID ( const Rect  bounds,
const Vec2  gravity,
std::string  UUID 
)
inlinestatic

Returns a newly allocated physics world

The specified bounds are in terms of the Box2d world, not the screen. A few attached to this Box2d world should have ways to convert between the coordinate systems.

Parameters
boundsThe game bounds in Box2d coordinates
gravityThe gravitational force on this Box2d world
UUIDThe UUID of the netcode connection that established the world.
Returns
a newly allocated physics world

◆ allocWithUUID() [2/2]

static std::shared_ptr< NetWorld > cugl::physics2::net::NetWorld::allocWithUUID ( const Rect  bounds,
std::string  UUID 
)
inlinestatic

Returns a newly allocated physics world

The specified bounds are in terms of the Box2d world, not the screen. A view attached to this Box2d world should have ways to convert between the coordinate systems.

This constructor will use the default gravitational value. It will also assign the world a fresh UUID.

Parameters
boundsThe game bounds in Box2d coordinates
UUIDThe UUID of the netcode connection that established the world.
Returns
a newly allocated physics world

◆ dispose()

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

Disposes all of the resources used by this world.

A disposed NetWorld can be safely reinitialized. Any obstacles owned by this world will be deactivated. They will be deleted if no other object owns them.

◆ getJoint()

std::shared_ptr< Joint > cugl::physics2::net::NetWorld::getJoint ( Uint64  jid)
inline

Returns the joint for the given id.

This method returns nullptr if there is no such joint.

Parameters
jidThe joint id
Returns
the joint for the given id.

◆ getJointId()

Sint64 cugl::physics2::net::NetWorld::getJointId ( const std::shared_ptr< Joint > &  joint)
inline

Returns id for the given joint.

This method returns -1 if there is no such joint.

Parameters
jointThe joint to query
Returns
id for the given joint.

◆ getJointIds()

const std::unordered_map< std::shared_ptr< physics2::Joint >, Uint64 > & cugl::physics2::net::NetWorld::getJointIds ( )
inline

Returns the map from joints to their ids

Returns
the map from joints to their ids

◆ getJointMap()

const std::unordered_map< Uint64, std::shared_ptr< physics2::Joint > > & cugl::physics2::net::NetWorld::getJointMap ( )
inline

Returns the map from joint ids to the objects

Returns
the map from joint ids to the objects

◆ getNextObstacle()

std::shared_ptr< Obstacle > cugl::physics2::net::NetWorld::getNextObstacle ( )

Returns the next obstacle for synchronization

This goes around the obstacle set in a round-robin fashion.

Returns
the next obstacle for synchronization

◆ getObstacle()

std::shared_ptr< Obstacle > cugl::physics2::net::NetWorld::getObstacle ( Uint64  oid)
inline

Returns the obstacle for the given id.

This method returns nullptr if there is no such joint.

Parameters
oidThe obstacle id
Returns
the obstacle for the given id.

◆ getObstacleId()

Sint64 cugl::physics2::net::NetWorld::getObstacleId ( const std::shared_ptr< Obstacle > &  obs)
inline

Returns id for the given obstacle.

This method returns -1 if there is no such obstacle.

Parameters
obsThe obstacle to query
Returns
id for the given obstacle.

◆ getObstacleIds()

const std::unordered_map< std::shared_ptr< physics2::Obstacle >, Uint64 > & cugl::physics2::net::NetWorld::getObstacleIds ( )
inline

Returns the map from obstacles to their ids

Returns
the map from obstacles to their ids

◆ getObstacleMap()

const std::unordered_map< Uint64, std::shared_ptr< physics2::Obstacle > > & cugl::physics2::net::NetWorld::getObstacleMap ( )
inline

Returns the map from obstacle ids to the objects

Returns
the map from obstacle ids to the objects

◆ getOwnedJoints()

std::unordered_map< std::shared_ptr< physics2::Joint >, Uint64 > & cugl::physics2::net::NetWorld::getOwnedJoints ( )
inline

Returns the map of joints owned by this shared physics world.

The keys are the joints pointers, while the values are the ownership duration. If the value is 0, then this joint is permanently owned by this copy of the world.

Returns
the map of joints owned by this shared physics world.

◆ getOwnedObstacles()

std::unordered_map< std::shared_ptr< physics2::Obstacle >, Uint64 > & cugl::physics2::net::NetWorld::getOwnedObstacles ( )
inline

Returns the map of obstacles owned by this shared physics world.

The keys are the obstacle pointers, while the values are the ownership duration. If the value is 0, then this obstacle is permanently owned by this copy of the world.

Returns
the map of obstacles owned by this shared physics world.

◆ getshortUID()

Uint32 cugl::physics2::net::NetWorld::getshortUID ( )
inline

Returns the short id for this network session.

Returns
the short id for this network session.

◆ getUUID()

std::string cugl::physics2::net::NetWorld::getUUID ( )
inline

Returns the UUID of the netcode connection that created this world.

Returns
the UUID of the netcode connection that created this world.

◆ initJoint()

Uint64 cugl::physics2::net::NetWorld::initJoint ( const std::shared_ptr< Joint > &  joint)

Adds an initial joint to the physics world.

This method is for joints that are created at the start of the simulation, before any networking is underway.

Adding an joints activates the underlying physics. It will now connect its two obstacles. The physics world will include the joint in its next call to update.

Parameters
jointThe joint to add
Returns
the joint id

◆ initObstacle()

Uint64 cugl::physics2::net::NetWorld::initObstacle ( const std::shared_ptr< Obstacle > &  obj)

Adds an initial obstacle to the physics world.

This method is for obstacles that are created at the start of the simulation, before any networking is underway.

Adding an obstacle activates the underlying physics. It will now have a body. The physics world will include the obstacle in its next call to update.

Parameters
objThe obstacle to add
Returns
the obstacle id

◆ initWithUUID() [1/2]

bool cugl::physics2::net::NetWorld::initWithUUID ( const Rect  bounds,
const Vec2  gravity,
std::string  uuid 
)

Initializes a new physics world

The specified bounds are in terms of the Box2d world, not the screen. A few attached to this Box2d world should have ways to convert between the coordinate systems.

Parameters
boundsThe game bounds in Box2d coordinates
gravityThe gravitational force on this Box2d world
uuidThe UUID of the netcode connection that established the world.
Returns
true if the controller is initialized properly, false otherwise.

◆ initWithUUID() [2/2]

bool cugl::physics2::net::NetWorld::initWithUUID ( const Rect  bounds,
std::string  uuid 
)

Initializes a new networked world

The specified bounds are in terms of the Box2d world, not the screen. A view attached to this Box2d world should have ways to convert between the coordinate systems.

This constructor will use the default gravitational value.

Parameters
boundsThe game bounds in Box2d coordinates
uuidThe UUID of the netcode connection that established the world.
Returns
true if the controller is initialized properly, false otherwise.

◆ placeJoint()

Uint64 cugl::physics2::net::NetWorld::placeJoint ( const std::shared_ptr< Joint > &  joint)

Adds a joint to the physics world.

This method is for joints that are created while the simulation is ongoing.

Adding an joints activates the underlying physics. It will now connect its two obstacles. The physics world will include the joint in its next call to update.

Parameters
jointThe joint to add
Returns
the joint id

◆ placeObstacle()

Uint64 cugl::physics2::net::NetWorld::placeObstacle ( const std::shared_ptr< Obstacle > &  obj)

Adds an obstacle to the physics world.

This method is for obstacles that are created while the simulation is ongoing.

Adding an obstacle activates the underlying physics. It will now have a body. The physics world will include the obstacle in its next call to update.

Parameters
objThe obstacle to add
Returns
the obstacle id

◆ removeJoint()

virtual void cugl::physics2::net::NetWorld::removeJoint ( const std::shared_ptr< Joint > &  joint)
overridevirtual

Immediately removes a joint from the physics world

The joint will be released immediately. The physics will be deactivated and it will be removed from the Box2D world. Note that only the joint is removed. The bodies attached to the joint will still be present.

This method of removing joints is very heavy weight, and should only be used for single joint removal. If you want to remove multiple joints, then you should mark them for removal and call garbageCollect.

Removing a joint does not automatically delete the joint itself. However, this world releases ownership, which may lead to it being garbage collected.

Parameters
jointThe joint to remove

Reimplemented from cugl::physics2::ObstacleWorld.

◆ removeObstacle()

void cugl::physics2::net::NetWorld::removeObstacle ( const std::shared_ptr< Obstacle > &  obj)
overridevirtual

Immediately removes an obstacle from the physics world

The obstacle will be released immediately. The physics will be deactivated and it will be removed from the Box2D world. This method of removing obstacles is very heavy weight, and should only be used for single object removal. If you want to remove multiple obstacles, then you should mark them for removal and call garbageCollect.

Removing an obstacle does not automatically delete the obstacle itself. However, this world releases ownership, which may lead to it being garbage collected.

Parameters
objThe obstacle to remove

Reimplemented from cugl::physics2::ObstacleWorld.

◆ SayGoodbye()

void cugl::physics2::net::NetWorld::SayGoodbye ( b2Joint *  joint)
overridevirtual

Called when a joint is about to be destroyed.

This function is only called when the destruction is the result of the destruction of one of its attached bodies.

Parameters
jointthe joint to be destroyed

Reimplemented from cugl::physics2::ObstacleWorld.

◆ setshortUID()

void cugl::physics2::net::NetWorld::setshortUID ( Uint32  sid)
inline

Sets the short id for this network session.

Parameters
sidthe short id for this network session.

Member Data Documentation

◆ _idToJnt

std::unordered_map<Uint64, std::shared_ptr<physics2::Joint> > cugl::physics2::net::NetWorld::_idToJnt
protected

Map from ids to joint pointers (for pointer swizzling)

◆ _idToObs

std::unordered_map<Uint64, std::shared_ptr<physics2::Obstacle> > cugl::physics2::net::NetWorld::_idToObs
protected

Map from ids to obstacle pointers (for pointer swizzling)

◆ _jntToId

std::unordered_map<std::shared_ptr<physics2::Joint>,Uint64> cugl::physics2::net::NetWorld::_jntToId
protected

Map from joint pointers to ids (for pointer swizzling)

◆ _nextInitJoint

Uint32 cugl::physics2::net::NetWorld::_nextInitJoint
protected

The next available id for initial joints

◆ _nextInitObj

Uint32 cugl::physics2::net::NetWorld::_nextInitObj
protected

The next available id for initial objects

◆ _nextObstacle

std::unordered_set<std::shared_ptr<physics2::Obstacle>>::iterator cugl::physics2::net::NetWorld::_nextObstacle
protected

An iterator to keep track of obstacles for queueing purposes

◆ _nextSharedJoint

Uint32 cugl::physics2::net::NetWorld::_nextSharedJoint
protected

The next available id for shared joints

◆ _nextSharedObj

Uint32 cugl::physics2::net::NetWorld::_nextSharedObj
protected

The next available id for shared objects

◆ _obsToId

std::unordered_map<std::shared_ptr<physics2::Obstacle>,Uint64> cugl::physics2::net::NetWorld::_obsToId
protected

Map from obstacle pointers to ids (for pointer swizzling)

◆ _ownedJoints

std::unordered_map<std::shared_ptr<physics2::Joint>,Uint64> cugl::physics2::net::NetWorld::_ownedJoints
protected

A reference counter to the number of joint owners

◆ _ownedObs

std::unordered_map<std::shared_ptr<physics2::Obstacle>,Uint64> cugl::physics2::net::NetWorld::_ownedObs
protected

A reference counter to the number of obstacle owners

◆ _shortUID

Uint32 cugl::physics2::net::NetWorld::_shortUID
protected

A shortened version of the identifer for this session

◆ _uuid

std::string cugl::physics2::net::NetWorld::_uuid
protected

UUID of the NetcodeConnection that established this world


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