Skip to content

Commit

Permalink
Merge pull request #42639 from AndreaCatania/revert_1
Browse files Browse the repository at this point in the history
Reverted physics body spawn optimization #39726 #40252
  • Loading branch information
akien-mga authored Oct 8, 2020
2 parents 8be6db9 + 2e8cb8a commit 899b900
Show file tree
Hide file tree
Showing 13 changed files with 211 additions and 391 deletions.
27 changes: 14 additions & 13 deletions modules/bullet/area_bullet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,14 @@ AreaBullet::~AreaBullet() {
}

void AreaBullet::dispatch_callbacks() {
RigidCollisionObjectBullet::dispatch_callbacks();
if (!isScratched) {
return;
}
isScratched = false;

// Reverse order because I've to remove EXIT objects
for (int i = overlappingObjects.size() - 1; 0 <= i; --i) {
OverlappingObjectData &otherObj = overlappingObjects[i];
OverlappingObjectData &otherObj = overlappingObjects.write[i];

switch (otherObj.state) {
case OVERLAP_STATE_ENTER:
Expand Down Expand Up @@ -109,9 +112,10 @@ void AreaBullet::call_event(CollisionObjectBullet *p_otherObject, PhysicsServer3
}

void AreaBullet::scratch() {
if (space != nullptr) {
space->add_to_pre_flush_queue(this);
if (isScratched) {
return;
}
isScratched = true;
}

void AreaBullet::clear_overlaps(bool p_notify) {
Expand Down Expand Up @@ -160,7 +164,7 @@ void AreaBullet::main_shape_changed() {
btGhost->setCollisionShape(get_main_shape());
}

void AreaBullet::do_reload_body() {
void AreaBullet::reload_body() {
if (space) {
space->remove_area(this);
space->add_area(this);
Expand All @@ -169,25 +173,22 @@ void AreaBullet::do_reload_body() {

void AreaBullet::set_space(SpaceBullet *p_space) {
// Clear the old space if there is one

if (space) {
clear_overlaps(false);
isScratched = false;

// Remove this object form the physics world
space->unregister_collision_object(this);
space->remove_area(this);
}

space = p_space;

if (space) {
space->register_collision_object(this);
reload_body();
scratch();
space->add_area(this);
}
}

void AreaBullet::do_reload_collision_filters() {
void AreaBullet::on_collision_filters_change() {
if (space) {
space->reload_collision_filters(this);
}
Expand All @@ -201,13 +202,13 @@ void AreaBullet::add_overlap(CollisionObjectBullet *p_otherObject) {

void AreaBullet::put_overlap_as_exit(int p_index) {
scratch();
overlappingObjects[p_index].state = OVERLAP_STATE_EXIT;
overlappingObjects.write[p_index].state = OVERLAP_STATE_EXIT;
}

void AreaBullet::put_overlap_as_inside(int p_index) {
// This check is required to be sure this body was inside
if (OVERLAP_STATE_DIRTY == overlappingObjects[p_index].state) {
overlappingObjects[p_index].state = OVERLAP_STATE_INSIDE;
overlappingObjects.write[p_index].state = OVERLAP_STATE_INSIDE;
}
}

Expand Down
24 changes: 13 additions & 11 deletions modules/bullet/area_bullet.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#define AREABULLET_H

#include "collision_object_bullet.h"
#include "core/local_vector.h"
#include "core/vector.h"
#include "servers/physics_server_3d.h"
#include "space_bullet.h"

Expand Down Expand Up @@ -83,7 +83,7 @@ class AreaBullet : public RigidCollisionObjectBullet {
Variant *call_event_res_ptr[5];

btGhostObject *btGhost;
LocalVector<OverlappingObjectData> overlappingObjects;
Vector<OverlappingObjectData> overlappingObjects;
bool monitorable = true;

PhysicsServer3D::AreaSpaceOverrideMode spOv_mode = PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED;
Expand All @@ -96,6 +96,8 @@ class AreaBullet : public RigidCollisionObjectBullet {
real_t spOv_angularDump = 0.1;
int spOv_priority = 0;

bool isScratched = false;

InOutEventCallback eventsCallbacks[2];

public:
Expand Down Expand Up @@ -137,11 +139,11 @@ class AreaBullet : public RigidCollisionObjectBullet {
_FORCE_INLINE_ void set_spOv_priority(int p_priority) { spOv_priority = p_priority; }
_FORCE_INLINE_ int get_spOv_priority() { return spOv_priority; }

virtual void main_shape_changed() override;
virtual void do_reload_body() override;
virtual void set_space(SpaceBullet *p_space) override;
virtual void main_shape_changed();
virtual void reload_body();
virtual void set_space(SpaceBullet *p_space);

virtual void dispatch_callbacks() override;
virtual void dispatch_callbacks();
void call_event(CollisionObjectBullet *p_otherObject, PhysicsServer3D::AreaBodyStatus p_status);
void set_on_state_change(ObjectID p_id, const StringName &p_method, const Variant &p_udata = Variant());
void scratch();
Expand All @@ -150,9 +152,9 @@ class AreaBullet : public RigidCollisionObjectBullet {
// Dispatch the callbacks and removes from overlapping list
void remove_overlap(CollisionObjectBullet *p_object, bool p_notify);

virtual void do_reload_collision_filters() override;
virtual void on_collision_checker_start() override {}
virtual void on_collision_checker_end() override { isTransformChanged = false; }
virtual void on_collision_filters_change();
virtual void on_collision_checker_start() {}
virtual void on_collision_checker_end() { isTransformChanged = false; }

void add_overlap(CollisionObjectBullet *p_otherObject);
void put_overlap_as_exit(int p_index);
Expand All @@ -164,8 +166,8 @@ class AreaBullet : public RigidCollisionObjectBullet {
void set_event_callback(Type p_callbackObjectType, ObjectID p_id, const StringName &p_method);
bool has_event_callback(Type p_callbackObjectType);

virtual void on_enter_area(AreaBullet *p_area) override;
virtual void on_exit_area(AreaBullet *p_area) override;
virtual void on_enter_area(AreaBullet *p_area);
virtual void on_exit_area(AreaBullet *p_area);
};

#endif
2 changes: 1 addition & 1 deletion modules/bullet/bullet_physics_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class BulletPhysicsServer3D : public PhysicsServer3D {

bool active = true;
char active_spaces_count = 0;
LocalVector<SpaceBullet *> active_spaces;
Vector<SpaceBullet *> active_spaces;

mutable RID_PtrOwner<SpaceBullet> space_owner;
mutable RID_PtrOwner<ShapeBullet> shape_owner;
Expand Down
76 changes: 22 additions & 54 deletions modules/bullet/collision_object_bullet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ btTransform CollisionObjectBullet::ShapeWrapper::get_adjusted_transform() const
}

void CollisionObjectBullet::ShapeWrapper::claim_bt_shape(const btVector3 &body_scale) {
if (bt_shape == nullptr) {
if (!bt_shape) {
if (active) {
bt_shape = shape->create_bt_shape(scale * body_scale);
} else {
Expand All @@ -88,13 +88,6 @@ void CollisionObjectBullet::ShapeWrapper::claim_bt_shape(const btVector3 &body_s
}
}

void CollisionObjectBullet::ShapeWrapper::release_bt_shape() {
if (bt_shape != nullptr) {
shape->destroy_bt_shape(bt_shape);
bt_shape = nullptr;
}
}

CollisionObjectBullet::CollisionObjectBullet(Type p_type) :
RIDBullet(),
type(p_type) {}
Expand Down Expand Up @@ -165,22 +158,6 @@ bool CollisionObjectBullet::has_collision_exception(const CollisionObjectBullet
return !bt_collision_object->checkCollideWith(p_otherCollisionObject->bt_collision_object);
}

void CollisionObjectBullet::reload_body() {
needs_body_reload = true;
}

void CollisionObjectBullet::dispatch_callbacks() {}

void CollisionObjectBullet::pre_process() {
if (needs_body_reload) {
do_reload_body();
} else if (needs_collision_filters_reload) {
do_reload_collision_filters();
}
needs_body_reload = false;
needs_collision_filters_reload = false;
}

void CollisionObjectBullet::set_collision_enabled(bool p_enabled) {
collisionsEnabled = p_enabled;
if (collisionsEnabled) {
Expand Down Expand Up @@ -254,7 +231,7 @@ void RigidCollisionObjectBullet::add_shape(ShapeBullet *p_shape, const Transform
}

void RigidCollisionObjectBullet::set_shape(int p_index, ShapeBullet *p_shape) {
ShapeWrapper &shp = shapes[p_index];
ShapeWrapper &shp = shapes.write[p_index];
shp.shape->remove_owner(this);
p_shape->add_owner(this);
shp.shape = p_shape;
Expand Down Expand Up @@ -316,7 +293,7 @@ void RigidCollisionObjectBullet::remove_all_shapes(bool p_permanentlyFromThisBod
void RigidCollisionObjectBullet::set_shape_transform(int p_index, const Transform &p_transform) {
ERR_FAIL_INDEX(p_index, get_shape_count());

shapes[p_index].set_transform(p_transform);
shapes.write[p_index].set_transform(p_transform);
shape_changed(p_index);
}

Expand All @@ -334,75 +311,66 @@ void RigidCollisionObjectBullet::set_shape_disabled(int p_index, bool p_disabled
if (shapes[p_index].active != p_disabled) {
return;
}
shapes[p_index].active = !p_disabled;
shapes.write[p_index].active = !p_disabled;
shape_changed(p_index);
}

bool RigidCollisionObjectBullet::is_shape_disabled(int p_index) {
return !shapes[p_index].active;
}

void RigidCollisionObjectBullet::pre_process() {
if (need_shape_reload) {
do_reload_shapes();
need_shape_reload = false;
}
CollisionObjectBullet::pre_process();
}

void RigidCollisionObjectBullet::shape_changed(int p_shape_index) {
ShapeWrapper &shp = shapes[p_shape_index];
ShapeWrapper &shp = shapes.write[p_shape_index];
if (shp.bt_shape == mainShape) {
mainShape = nullptr;
}
shp.release_bt_shape();
bulletdelete(shp.bt_shape);
reload_shapes();
}

void RigidCollisionObjectBullet::reload_shapes() {
need_shape_reload = true;
}

void RigidCollisionObjectBullet::do_reload_shapes() {
if (mainShape && mainShape->isCompound()) {
// Destroy compound
bulletdelete(mainShape);
}

mainShape = nullptr;

ShapeWrapper *shpWrapper;
const int shape_count = shapes.size();

// Reset all shapes if required
// Reset shape if required
if (force_shape_reset) {
for (int i(0); i < shape_count; ++i) {
shapes[i].release_bt_shape();
shpWrapper = &shapes.write[i];
bulletdelete(shpWrapper->bt_shape);
}
force_shape_reset = false;
}

const btVector3 body_scale(get_bt_body_scale());

// Try to optimize by not using compound
if (1 == shape_count) {
// Is it possible to optimize by not using compound?
btTransform transform = shapes[0].get_adjusted_transform();
shpWrapper = &shapes.write[0];
btTransform transform = shpWrapper->get_adjusted_transform();
if (transform.getOrigin().isZero() && transform.getBasis() == transform.getBasis().getIdentity()) {
shapes[0].claim_bt_shape(body_scale);
mainShape = shapes[0].bt_shape;
shpWrapper->claim_bt_shape(body_scale);
mainShape = shpWrapper->bt_shape;
main_shape_changed();
// Nothing more to do
return;
}
}

// Optimization not possible use a compound shape.
// Optimization not possible use a compound shape
btCompoundShape *compoundShape = bulletnew(btCompoundShape(enableDynamicAabbTree, shape_count));

for (int i(0); i < shape_count; ++i) {
shapes[i].claim_bt_shape(body_scale);
btTransform scaled_shape_transform(shapes[i].get_adjusted_transform());
shpWrapper = &shapes.write[i];
shpWrapper->claim_bt_shape(body_scale);
btTransform scaled_shape_transform(shpWrapper->get_adjusted_transform());
scaled_shape_transform.getOrigin() *= body_scale;
compoundShape->addChildShape(scaled_shape_transform, shapes[i].bt_shape);
compoundShape->addChildShape(scaled_shape_transform, shpWrapper->bt_shape);
}

compoundShape->recalculateLocalAabb();
Expand All @@ -416,10 +384,10 @@ void RigidCollisionObjectBullet::body_scale_changed() {
}

void RigidCollisionObjectBullet::internal_shape_destroy(int p_index, bool p_permanentlyFromThisBody) {
ShapeWrapper &shp = shapes[p_index];
ShapeWrapper &shp = shapes.write[p_index];
shp.shape->remove_owner(this, p_permanentlyFromThisBody);
if (shp.bt_shape == mainShape) {
mainShape = nullptr;
}
shp.release_bt_shape();
bulletdelete(shp.bt_shape);
}
Loading

0 comments on commit 899b900

Please sign in to comment.