Merge pull request #32641 from nekomatata/change-collision-layer

Fixed RigidBody stuttering when changing collision layer/mask
This commit is contained in:
Rémi Verschelde 2019-10-12 14:07:55 +02:00 committed by GitHub
commit 26bbb26738
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 10 deletions

View File

@ -167,15 +167,19 @@ public:
_FORCE_INLINE_ const VSet<RID> &get_exceptions() const { return exceptions; }
_FORCE_INLINE_ void set_collision_layer(uint32_t p_layer) {
if (collisionLayer != p_layer) {
collisionLayer = p_layer;
on_collision_filters_change();
}
}
_FORCE_INLINE_ uint32_t get_collision_layer() const { return collisionLayer; }
_FORCE_INLINE_ void set_collision_mask(uint32_t p_mask) {
if (collisionMask != p_mask) {
collisionMask = p_mask;
on_collision_filters_change();
}
}
_FORCE_INLINE_ uint32_t get_collision_mask() const { return collisionMask; }
virtual void on_collision_filters_change() = 0;

View File

@ -411,6 +411,8 @@ void RigidBodyBullet::on_collision_filters_change() {
if (space) {
space->reload_collision_filters(this);
}
set_activation_state(true);
}
void RigidBodyBullet::on_collision_checker_start() {

View File

@ -42,6 +42,7 @@
#include "servers/physics_server.h"
#include "soft_body_bullet.h"
#include <BulletCollision/BroadphaseCollision/btBroadphaseProxy.h>
#include <BulletCollision/CollisionDispatch/btCollisionObject.h>
#include <BulletCollision/CollisionDispatch/btGhostObject.h>
#include <BulletCollision/NarrowPhaseCollision/btGjkEpaPenetrationDepthSolver.h>
@ -459,9 +460,13 @@ void SpaceBullet::remove_area(AreaBullet *p_area) {
}
void SpaceBullet::reload_collision_filters(AreaBullet *p_area) {
// This is necessary to change collision filter
dynamicsWorld->removeCollisionObject(p_area->get_bt_ghost());
dynamicsWorld->addCollisionObject(p_area->get_bt_ghost(), p_area->get_collision_layer(), p_area->get_collision_mask());
btGhostObject *ghost_object = p_area->get_bt_ghost();
btBroadphaseProxy *ghost_proxy = ghost_object->getBroadphaseHandle();
ghost_proxy->m_collisionFilterGroup = p_area->get_collision_layer();
ghost_proxy->m_collisionFilterMask = p_area->get_collision_mask();
dynamicsWorld->refreshBroadphaseProxy(ghost_object);
}
void SpaceBullet::add_rigid_body(RigidBodyBullet *p_body) {
@ -482,9 +487,13 @@ void SpaceBullet::remove_rigid_body(RigidBodyBullet *p_body) {
}
void SpaceBullet::reload_collision_filters(RigidBodyBullet *p_body) {
// This is necessary to change collision filter
remove_rigid_body(p_body);
add_rigid_body(p_body);
btRigidBody *rigid_body = p_body->get_bt_rigid_body();
btBroadphaseProxy *body_proxy = rigid_body->getBroadphaseProxy();
body_proxy->m_collisionFilterGroup = p_body->get_collision_layer();
body_proxy->m_collisionFilterMask = p_body->get_collision_mask();
dynamicsWorld->refreshBroadphaseProxy(rigid_body);
}
void SpaceBullet::add_soft_body(SoftBodyBullet *p_body) {