CUGL 2.5
Cornell University Game Library
|
#include <CUNetWorld.h>
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< Obstacle > | getNextObstacle () |
std::shared_ptr< Obstacle > | getObstacle (Uint64 oid) |
Sint64 | getObstacleId (const std::shared_ptr< Obstacle > &obs) |
std::shared_ptr< Joint > | getJoint (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< NetWorld > | alloc (const Rect bounds) |
static std::shared_ptr< NetWorld > | alloc (const Rect bounds, const Vec2 gravity) |
static std::shared_ptr< NetWorld > | allocWithUUID (const Rect bounds, std::string UUID) |
static std::shared_ptr< NetWorld > | allocWithUUID (const Rect bounds, const Vec2 gravity, std::string UUID) |
Static Public Member Functions inherited from cugl::physics2::ObstacleWorld | |
static std::shared_ptr< ObstacleWorld > | alloc (const Rect bounds) |
static std::shared_ptr< ObstacleWorld > | alloc (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 |
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.
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.
|
inline |
Deletes this world, disposing all resources
|
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
|
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
|
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.
joint | The joint to add |
Reimplemented from cugl::physics2::ObstacleWorld.
|
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.
obj | The obstacle to add |
Reimplemented from cugl::physics2::ObstacleWorld.
|
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.
bounds | The game bounds in Box2d coordinates |
|
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.
bounds | The game bounds in Box2d coordinates |
gravity | The gravitational force on this Box2d world |
|
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.
bounds | The game bounds in Box2d coordinates |
gravity | The gravitational force on this Box2d world |
UUID | The UUID of the netcode connection that established the world. |
|
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.
bounds | The game bounds in Box2d coordinates |
UUID | The UUID of the netcode connection that established the world. |
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.
|
inline |
Returns the joint for the given id.
This method returns nullptr if there is no such joint.
jid | The joint id |
|
inline |
Returns id for the given joint.
This method returns -1 if there is no such joint.
joint | The joint to query |
|
inline |
Returns the map from joints to their ids
|
inline |
Returns the map from joint ids to the objects
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.
|
inline |
Returns the obstacle for the given id.
This method returns nullptr if there is no such joint.
oid | The obstacle id |
|
inline |
Returns id for the given obstacle.
This method returns -1 if there is no such obstacle.
obs | The obstacle to query |
|
inline |
Returns the map from obstacles to their ids
|
inline |
Returns the map from obstacle ids to the objects
|
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.
|
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.
|
inline |
Returns the short id for this network session.
|
inline |
Returns the UUID of the netcode connection that created this world.
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.
joint | The joint to add |
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.
obj | The obstacle to add |
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.
bounds | The game bounds in Box2d coordinates |
gravity | The gravitational force on this Box2d world |
uuid | The UUID of the netcode connection that established the world. |
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.
bounds | The game bounds in Box2d coordinates |
uuid | The UUID of the netcode connection that established the world. |
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.
joint | The joint to add |
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.
obj | The obstacle to add |
|
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.
joint | The joint to remove |
Reimplemented from cugl::physics2::ObstacleWorld.
|
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.
obj | The obstacle to remove |
Reimplemented from cugl::physics2::ObstacleWorld.
|
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.
joint | the joint to be destroyed |
Reimplemented from cugl::physics2::ObstacleWorld.
|
inline |
Sets the short id for this network session.
sid | the short id for this network session. |
|
protected |
Map from ids to joint pointers (for pointer swizzling)
|
protected |
Map from ids to obstacle pointers (for pointer swizzling)
|
protected |
Map from joint pointers to ids (for pointer swizzling)
|
protected |
The next available id for initial joints
|
protected |
The next available id for initial objects
|
protected |
An iterator to keep track of obstacles for queueing purposes
|
protected |
The next available id for shared joints
|
protected |
The next available id for shared objects
|
protected |
Map from obstacle pointers to ids (for pointer swizzling)
|
protected |
A reference counter to the number of joint owners
|
protected |
A reference counter to the number of obstacle owners
|
protected |
A shortened version of the identifer for this session
|
protected |
UUID of the NetcodeConnection that established this world