Merge pull request #14965 from AndreaCatania/intshap

Fixed bullet intersect_shape crash, Fixed bullet sleeping
This commit is contained in:
Rémi Verschelde 2017-12-23 11:18:30 +01:00 committed by GitHub
commit 5463b5e348
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 12 deletions

View file

@ -142,6 +142,9 @@ bool GodotAllContactResultCallback::needsCollision(btBroadphaseProxy *proxy0) co
btScalar GodotAllContactResultCallback::addSingleResult(btManifoldPoint &cp, const btCollisionObjectWrapper *colObj0Wrap, int partId0, int index0, const btCollisionObjectWrapper *colObj1Wrap, int partId1, int index1) { btScalar GodotAllContactResultCallback::addSingleResult(btManifoldPoint &cp, const btCollisionObjectWrapper *colObj0Wrap, int partId0, int index0, const btCollisionObjectWrapper *colObj1Wrap, int partId1, int index1) {
if (m_count >= m_resultMax)
return cp.getDistance();
if (cp.getDistance() <= 0) { if (cp.getDistance() <= 0) {
PhysicsDirectSpaceState::ShapeResult &result = m_results[m_count]; PhysicsDirectSpaceState::ShapeResult &result = m_results[m_count];
@ -165,7 +168,7 @@ btScalar GodotAllContactResultCallback::addSingleResult(btManifoldPoint &cp, con
++m_count; ++m_count;
} }
return m_count < m_resultMax; return cp.getDistance();
} }
bool GodotContactPairContactResultCallback::needsCollision(btBroadphaseProxy *proxy0) const { bool GodotContactPairContactResultCallback::needsCollision(btBroadphaseProxy *proxy0) const {

View file

@ -264,6 +264,7 @@ RigidBodyBullet::RigidBodyBullet() :
can_sleep(true), can_sleep(true),
force_integration_callback(NULL), force_integration_callback(NULL),
isTransformChanged(false), isTransformChanged(false),
previousActiveState(true),
maxCollisionsDetection(0), maxCollisionsDetection(0),
collisionsCount(0), collisionsCount(0),
maxAreasWhereIam(10), maxAreasWhereIam(10),
@ -287,6 +288,7 @@ RigidBodyBullet::RigidBodyBullet() :
for (int i = areasWhereIam.size() - 1; 0 <= i; --i) { for (int i = areasWhereIam.size() - 1; 0 <= i; --i) {
areasWhereIam[i] = NULL; areasWhereIam[i] = NULL;
} }
btBody->setSleepingThresholds(0.2, 0.2);
} }
RigidBodyBullet::~RigidBodyBullet() { RigidBodyBullet::~RigidBodyBullet() {
@ -337,7 +339,7 @@ void RigidBodyBullet::set_space(SpaceBullet *p_space) {
void RigidBodyBullet::dispatch_callbacks() { void RigidBodyBullet::dispatch_callbacks() {
/// The check isTransformChanged is necessary in order to call integrated forces only when the first transform is sent /// The check isTransformChanged is necessary in order to call integrated forces only when the first transform is sent
if (btBody->isActive() && force_integration_callback && isTransformChanged) { if (previousActiveState != btBody->isActive() && force_integration_callback && isTransformChanged) {
BulletPhysicsDirectBodyState *bodyDirect = BulletPhysicsDirectBodyState::get_singleton(this); BulletPhysicsDirectBodyState *bodyDirect = BulletPhysicsDirectBodyState::get_singleton(this);
@ -364,6 +366,8 @@ void RigidBodyBullet::dispatch_callbacks() {
/// Lock axis /// Lock axis
btBody->setLinearVelocity(btBody->getLinearVelocity() * btBody->getLinearFactor()); btBody->setLinearVelocity(btBody->getLinearVelocity() * btBody->getLinearFactor());
btBody->setAngularVelocity(btBody->getAngularVelocity() * btBody->getAngularFactor()); btBody->setAngularVelocity(btBody->getAngularVelocity() * btBody->getAngularFactor());
previousActiveState = btBody->isActive();
} }
void RigidBodyBullet::set_force_integration_callback(ObjectID p_id, const StringName &p_method, const Variant &p_udata) { void RigidBodyBullet::set_force_integration_callback(ObjectID p_id, const StringName &p_method, const Variant &p_udata) {
@ -580,6 +584,7 @@ Variant RigidBodyBullet::get_state(PhysicsServer::BodyState p_state) const {
void RigidBodyBullet::apply_central_impulse(const Vector3 &p_impulse) { void RigidBodyBullet::apply_central_impulse(const Vector3 &p_impulse) {
btVector3 btImpu; btVector3 btImpu;
G_TO_B(p_impulse, btImpu); G_TO_B(p_impulse, btImpu);
if (Vector3() != p_impulse)
btBody->activate(); btBody->activate();
btBody->applyCentralImpulse(btImpu); btBody->applyCentralImpulse(btImpu);
} }
@ -589,6 +594,7 @@ void RigidBodyBullet::apply_impulse(const Vector3 &p_pos, const Vector3 &p_impul
btVector3 btPos; btVector3 btPos;
G_TO_B(p_impulse, btImpu); G_TO_B(p_impulse, btImpu);
G_TO_B(p_pos, btPos); G_TO_B(p_pos, btPos);
if (Vector3() != p_impulse)
btBody->activate(); btBody->activate();
btBody->applyImpulse(btImpu, btPos); btBody->applyImpulse(btImpu, btPos);
} }
@ -596,6 +602,7 @@ void RigidBodyBullet::apply_impulse(const Vector3 &p_pos, const Vector3 &p_impul
void RigidBodyBullet::apply_torque_impulse(const Vector3 &p_impulse) { void RigidBodyBullet::apply_torque_impulse(const Vector3 &p_impulse) {
btVector3 btImp; btVector3 btImp;
G_TO_B(p_impulse, btImp); G_TO_B(p_impulse, btImp);
if (Vector3() != p_impulse)
btBody->activate(); btBody->activate();
btBody->applyTorqueImpulse(btImp); btBody->applyTorqueImpulse(btImp);
} }
@ -605,6 +612,7 @@ void RigidBodyBullet::apply_force(const Vector3 &p_force, const Vector3 &p_pos)
btVector3 btPos; btVector3 btPos;
G_TO_B(p_force, btForce); G_TO_B(p_force, btForce);
G_TO_B(p_pos, btPos); G_TO_B(p_pos, btPos);
if (Vector3() != p_force)
btBody->activate(); btBody->activate();
btBody->applyForce(btForce, btPos); btBody->applyForce(btForce, btPos);
} }
@ -612,6 +620,7 @@ void RigidBodyBullet::apply_force(const Vector3 &p_force, const Vector3 &p_pos)
void RigidBodyBullet::apply_central_force(const Vector3 &p_force) { void RigidBodyBullet::apply_central_force(const Vector3 &p_force) {
btVector3 btForce; btVector3 btForce;
G_TO_B(p_force, btForce); G_TO_B(p_force, btForce);
if (Vector3() != p_force)
btBody->activate(); btBody->activate();
btBody->applyCentralForce(btForce); btBody->applyCentralForce(btForce);
} }
@ -619,6 +628,7 @@ void RigidBodyBullet::apply_central_force(const Vector3 &p_force) {
void RigidBodyBullet::apply_torque(const Vector3 &p_torque) { void RigidBodyBullet::apply_torque(const Vector3 &p_torque) {
btVector3 btTorq; btVector3 btTorq;
G_TO_B(p_torque, btTorq); G_TO_B(p_torque, btTorq);
if (Vector3() != p_torque)
btBody->activate(); btBody->activate();
btBody->applyTorque(btTorq); btBody->applyTorque(btTorq);
} }
@ -626,6 +636,7 @@ void RigidBodyBullet::apply_torque(const Vector3 &p_torque) {
void RigidBodyBullet::set_applied_force(const Vector3 &p_force) { void RigidBodyBullet::set_applied_force(const Vector3 &p_force) {
btVector3 btVec = btBody->getTotalTorque(); btVector3 btVec = btBody->getTotalTorque();
if (Vector3() != p_force)
btBody->activate(); btBody->activate();
btBody->clearForces(); btBody->clearForces();
@ -644,6 +655,7 @@ Vector3 RigidBodyBullet::get_applied_force() const {
void RigidBodyBullet::set_applied_torque(const Vector3 &p_torque) { void RigidBodyBullet::set_applied_torque(const Vector3 &p_torque) {
btVector3 btVec = btBody->getTotalForce(); btVector3 btVec = btBody->getTotalForce();
if (Vector3() != p_torque)
btBody->activate(); btBody->activate();
btBody->clearForces(); btBody->clearForces();
@ -711,6 +723,7 @@ bool RigidBodyBullet::is_continuous_collision_detection_enabled() const {
void RigidBodyBullet::set_linear_velocity(const Vector3 &p_velocity) { void RigidBodyBullet::set_linear_velocity(const Vector3 &p_velocity) {
btVector3 btVec; btVector3 btVec;
G_TO_B(p_velocity, btVec); G_TO_B(p_velocity, btVec);
if (Vector3() != p_velocity)
btBody->activate(); btBody->activate();
btBody->setLinearVelocity(btVec); btBody->setLinearVelocity(btVec);
} }
@ -724,6 +737,7 @@ Vector3 RigidBodyBullet::get_linear_velocity() const {
void RigidBodyBullet::set_angular_velocity(const Vector3 &p_velocity) { void RigidBodyBullet::set_angular_velocity(const Vector3 &p_velocity) {
btVector3 btVec; btVector3 btVec;
G_TO_B(p_velocity, btVec); G_TO_B(p_velocity, btVec);
if (Vector3() != p_velocity)
btBody->activate(); btBody->activate();
btBody->setAngularVelocity(btVec); btBody->setAngularVelocity(btVec);
} }
@ -833,6 +847,9 @@ void RigidBodyBullet::on_exit_area(AreaBullet *p_area) {
void RigidBodyBullet::reload_space_override_modificator() { void RigidBodyBullet::reload_space_override_modificator() {
if (!is_active())
return;
Vector3 newGravity(space->get_gravity_direction() * space->get_gravity_magnitude()); Vector3 newGravity(space->get_gravity_direction() * space->get_gravity_magnitude());
real_t newLinearDamp(linearDamp); real_t newLinearDamp(linearDamp);
real_t newAngularDamp(angularDamp); real_t newAngularDamp(angularDamp);

View file

@ -207,6 +207,7 @@ private:
bool isScratchedSpaceOverrideModificator; bool isScratchedSpaceOverrideModificator;
bool isTransformChanged; bool isTransformChanged;
bool previousActiveState; // Last check state
ForceIntegrationCallback *force_integration_callback; ForceIntegrationCallback *force_integration_callback;