Bullet - Added support to multiple spaces

This commit is contained in:
AndreaCatania 2017-11-05 18:50:39 +01:00
parent fb4871c919
commit 2c69f3d0d2
2 changed files with 23 additions and 19 deletions

View file

@ -81,7 +81,7 @@ void BulletPhysicsServer::_bind_methods() {
BulletPhysicsServer::BulletPhysicsServer() BulletPhysicsServer::BulletPhysicsServer()
: PhysicsServer(), : PhysicsServer(),
active(true), active(true),
activeSpace(NULL) {} active_spaces_count(0) {}
BulletPhysicsServer::~BulletPhysicsServer() {} BulletPhysicsServer::~BulletPhysicsServer() {}
@ -162,27 +162,28 @@ RID BulletPhysicsServer::space_create() {
} }
void BulletPhysicsServer::space_set_active(RID p_space, bool p_active) { void BulletPhysicsServer::space_set_active(RID p_space, bool p_active) {
SpaceBullet *space = space_owner.get(p_space);
ERR_FAIL_COND(!space);
if (space_is_active(p_space) == p_active) {
return;
}
if (p_active) { if (p_active) {
if (activeSpace) { ++active_spaces_count;
// There is another space and this cannot be activated active_spaces.push_back(space);
ERR_PRINT("There is another space, before activate new one deactivate the current space.");
} else {
SpaceBullet *space = space_owner.get(p_space);
if (space) {
activeSpace = space;
} else {
ERR_PRINT("The passed RID is not a valid space. Please provide a RID with SpaceBullet type.");
}
}
} else { } else {
if (!space_is_active(p_space)) { --active_spaces_count;
activeSpace = NULL; active_spaces.erase(space);
}
} }
} }
bool BulletPhysicsServer::space_is_active(RID p_space) const { bool BulletPhysicsServer::space_is_active(RID p_space) const {
return NULL != activeSpace && activeSpace == p_space.get_data(); SpaceBullet *space = space_owner.get(p_space);
ERR_FAIL_COND_V(!space, false);
return -1 != active_spaces.find(space);
} }
void BulletPhysicsServer::space_set_param(RID p_space, SpaceParameter p_param, real_t p_value) { void BulletPhysicsServer::space_set_param(RID p_space, SpaceParameter p_param, real_t p_value) {
@ -1296,8 +1297,10 @@ void BulletPhysicsServer::step(float p_deltaTime) {
return; return;
BulletPhysicsDirectBodyState::singleton_setDeltaTime(p_deltaTime); BulletPhysicsDirectBodyState::singleton_setDeltaTime(p_deltaTime);
if (activeSpace) {
activeSpace->step(p_deltaTime); for (int i = 0; i < active_spaces_count; ++i) {
active_spaces[i]->step(p_deltaTime);
} }
} }

View file

@ -47,7 +47,8 @@ class BulletPhysicsServer : public PhysicsServer {
friend class BulletPhysicsDirectSpaceState; friend class BulletPhysicsDirectSpaceState;
bool active; bool active;
SpaceBullet *activeSpace; char active_spaces_count;
Vector<SpaceBullet *> active_spaces;
mutable RID_Owner<SpaceBullet> space_owner; mutable RID_Owner<SpaceBullet> space_owner;
mutable RID_Owner<ShapeBullet> shape_owner; mutable RID_Owner<ShapeBullet> shape_owner;