Merge pull request #22339 from AndreaCatania/fixes2

Toward 3.1 - physics fixes
This commit is contained in:
Max Hilbrunner 2018-09-22 16:47:57 +02:00 committed by GitHub
commit e863bbb3b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 87 additions and 25 deletions

View file

@ -69,15 +69,7 @@ PhysicalBoneEditor::PhysicalBoneEditor(EditorNode *p_editor) :
hide();
}
PhysicalBoneEditor::~PhysicalBoneEditor() {
// TODO the spatial_editor_hb should be removed from SpatialEditor, but in this moment it's not possible
for (int i = spatial_editor_hb->get_child_count() - 1; 0 <= i; --i) {
Node *n = spatial_editor_hb->get_child(i);
spatial_editor_hb->remove_child(n);
memdelete(n);
}
memdelete(spatial_editor_hb);
}
PhysicalBoneEditor::~PhysicalBoneEditor() {}
void PhysicalBoneEditor::set_selected(PhysicalBone *p_pb) {
@ -98,19 +90,17 @@ void PhysicalBoneEditor::show() {
PhysicalBonePlugin::PhysicalBonePlugin(EditorNode *p_editor) :
editor(p_editor),
selected(NULL) {
physical_bone_editor = memnew(PhysicalBoneEditor(editor));
}
selected(NULL),
physical_bone_editor(editor) {}
void PhysicalBonePlugin::make_visible(bool p_visible) {
if (p_visible) {
physical_bone_editor->show();
physical_bone_editor.show();
} else {
physical_bone_editor->hide();
physical_bone_editor->set_selected(NULL);
physical_bone_editor.hide();
physical_bone_editor.set_selected(NULL);
selected = NULL;
}
}
@ -119,5 +109,5 @@ void PhysicalBonePlugin::edit(Object *p_node) {
selected = static_cast<PhysicalBone *>(p_node); // Trust it
ERR_FAIL_COND(!selected);
physical_bone_editor->set_selected(selected);
physical_bone_editor.set_selected(selected);
}

View file

@ -64,7 +64,7 @@ class PhysicalBonePlugin : public EditorPlugin {
EditorNode *editor;
PhysicalBone *selected;
PhysicalBoneEditor *physical_bone_editor;
PhysicalBoneEditor physical_bone_editor;
public:
virtual String get_name() const { return "PhysicalBone"; }

View file

@ -94,3 +94,59 @@ btCollisionAlgorithmCreateFunc *GodotCollisionConfiguration::getClosestPointsAlg
return btDefaultCollisionConfiguration::getClosestPointsAlgorithmCreateFunc(proxyType0, proxyType1);
}
}
GodotSoftCollisionConfiguration::GodotSoftCollisionConfiguration(const btDiscreteDynamicsWorld *world, const btDefaultCollisionConstructionInfo &constructionInfo) :
btSoftBodyRigidBodyCollisionConfiguration(constructionInfo) {
void *mem = NULL;
mem = btAlignedAlloc(sizeof(GodotRayWorldAlgorithm::CreateFunc), 16);
m_rayWorldCF = new (mem) GodotRayWorldAlgorithm::CreateFunc(world);
mem = btAlignedAlloc(sizeof(GodotRayWorldAlgorithm::SwappedCreateFunc), 16);
m_swappedRayWorldCF = new (mem) GodotRayWorldAlgorithm::SwappedCreateFunc(world);
}
GodotSoftCollisionConfiguration::~GodotSoftCollisionConfiguration() {
m_rayWorldCF->~btCollisionAlgorithmCreateFunc();
btAlignedFree(m_rayWorldCF);
m_swappedRayWorldCF->~btCollisionAlgorithmCreateFunc();
btAlignedFree(m_swappedRayWorldCF);
}
btCollisionAlgorithmCreateFunc *GodotSoftCollisionConfiguration::getCollisionAlgorithmCreateFunc(int proxyType0, int proxyType1) {
if (CUSTOM_CONVEX_SHAPE_TYPE == proxyType0 && CUSTOM_CONVEX_SHAPE_TYPE == proxyType1) {
// This collision is not supported
return m_emptyCreateFunc;
} else if (CUSTOM_CONVEX_SHAPE_TYPE == proxyType0) {
return m_rayWorldCF;
} else if (CUSTOM_CONVEX_SHAPE_TYPE == proxyType1) {
return m_swappedRayWorldCF;
} else {
return btSoftBodyRigidBodyCollisionConfiguration::getCollisionAlgorithmCreateFunc(proxyType0, proxyType1);
}
}
btCollisionAlgorithmCreateFunc *GodotSoftCollisionConfiguration::getClosestPointsAlgorithmCreateFunc(int proxyType0, int proxyType1) {
if (CUSTOM_CONVEX_SHAPE_TYPE == proxyType0 && CUSTOM_CONVEX_SHAPE_TYPE == proxyType1) {
// This collision is not supported
return m_emptyCreateFunc;
} else if (CUSTOM_CONVEX_SHAPE_TYPE == proxyType0) {
return m_rayWorldCF;
} else if (CUSTOM_CONVEX_SHAPE_TYPE == proxyType1) {
return m_swappedRayWorldCF;
} else {
return btSoftBodyRigidBodyCollisionConfiguration::getClosestPointsAlgorithmCreateFunc(proxyType0, proxyType1);
}
}

View file

@ -32,6 +32,7 @@
#define GODOT_COLLISION_CONFIGURATION_H
#include <BulletCollision/CollisionDispatch/btDefaultCollisionConfiguration.h>
#include <BulletSoftBody/btSoftBodyRigidBodyCollisionConfiguration.h>
/**
@author AndreaCatania
@ -50,4 +51,16 @@ public:
virtual btCollisionAlgorithmCreateFunc *getCollisionAlgorithmCreateFunc(int proxyType0, int proxyType1);
virtual btCollisionAlgorithmCreateFunc *getClosestPointsAlgorithmCreateFunc(int proxyType0, int proxyType1);
};
class GodotSoftCollisionConfiguration : public btSoftBodyRigidBodyCollisionConfiguration {
btCollisionAlgorithmCreateFunc *m_rayWorldCF;
btCollisionAlgorithmCreateFunc *m_swappedRayWorldCF;
public:
GodotSoftCollisionConfiguration(const btDiscreteDynamicsWorld *world, const btDefaultCollisionConstructionInfo &constructionInfo = btDefaultCollisionConstructionInfo());
virtual ~GodotSoftCollisionConfiguration();
virtual btCollisionAlgorithmCreateFunc *getCollisionAlgorithmCreateFunc(int proxyType0, int proxyType1);
virtual btCollisionAlgorithmCreateFunc *getClosestPointsAlgorithmCreateFunc(int proxyType0, int proxyType1);
};
#endif

View file

@ -150,14 +150,14 @@ int BulletPhysicsDirectSpaceState::intersect_shape(const RID &p_shape, const Tra
return btQuery.m_count;
}
bool BulletPhysicsDirectSpaceState::cast_motion(const RID &p_shape, const Transform &p_xform, const Vector3 &p_motion, float p_margin, float &p_closest_safe, float &p_closest_unsafe, const Set<RID> &p_exclude, uint32_t p_collision_mask, bool p_collide_with_bodies, bool p_collide_with_areas, ShapeRestInfo *r_info) {
bool BulletPhysicsDirectSpaceState::cast_motion(const RID &p_shape, const Transform &p_xform, const Vector3 &p_motion, float p_margin, float &r_closest_safe, float &r_closest_unsafe, const Set<RID> &p_exclude, uint32_t p_collision_mask, bool p_collide_with_bodies, bool p_collide_with_areas, ShapeRestInfo *r_info) {
ShapeBullet *shape = space->get_physics_server()->get_shape_owner()->get(p_shape);
btCollisionShape *btShape = shape->create_bt_shape(p_xform.basis.get_scale(), p_margin);
if (!btShape->isConvex()) {
bulletdelete(btShape);
ERR_PRINTS("The shape is not a convex shape, then is not supported: shape type: " + itos(shape->get_type()));
return 0;
return false;
}
btConvexShape *bt_convex_shape = static_cast<btConvexShape *>(btShape);
@ -177,10 +177,13 @@ bool BulletPhysicsDirectSpaceState::cast_motion(const RID &p_shape, const Transf
space->dynamicsWorld->convexSweepTest(bt_convex_shape, bt_xform_from, bt_xform_to, btResult, 0.002);
r_closest_unsafe = 1.0;
r_closest_safe = 1.0;
if (btResult.hasHit()) {
const btScalar l = bt_motion.length();
p_closest_unsafe = btResult.m_closestHitFraction;
p_closest_safe = MAX(p_closest_unsafe - (1 - ((l - 0.01) / l)), 0);
r_closest_unsafe = btResult.m_closestHitFraction;
r_closest_safe = MAX(r_closest_unsafe - (1 - ((l - 0.01) / l)), 0);
if (r_info) {
if (btCollisionObject::CO_RIGID_BODY == btResult.m_hitCollisionObject->getInternalType()) {
B_TO_G(static_cast<const btRigidBody *>(btResult.m_hitCollisionObject)->getVelocityInLocalPoint(btResult.m_hitPointWorld), r_info->linear_velocity);
@ -195,7 +198,7 @@ bool BulletPhysicsDirectSpaceState::cast_motion(const RID &p_shape, const Transf
}
bulletdelete(bt_convex_shape);
return btResult.hasHit();
return true; // Mean success
}
/// Returns the list of contacts pairs in this order: Local contact, other body contact
@ -577,7 +580,7 @@ void SpaceBullet::create_empty_world(bool p_create_soft_world) {
}
if (p_create_soft_world) {
collisionConfiguration = bulletnew(btSoftBodyRigidBodyCollisionConfiguration);
collisionConfiguration = bulletnew(GodotSoftCollisionConfiguration(static_cast<btDiscreteDynamicsWorld *>(world_mem)));
} else {
collisionConfiguration = bulletnew(GodotCollisionConfiguration(static_cast<btDiscreteDynamicsWorld *>(world_mem)));
}

View file

@ -78,7 +78,7 @@ public:
virtual int intersect_point(const Vector3 &p_point, ShapeResult *r_results, int p_result_max, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false);
virtual bool intersect_ray(const Vector3 &p_from, const Vector3 &p_to, RayResult &r_result, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false, bool p_pick_ray = false);
virtual int intersect_shape(const RID &p_shape, const Transform &p_xform, float p_margin, ShapeResult *r_results, int p_result_max, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false);
virtual bool cast_motion(const RID &p_shape, const Transform &p_xform, const Vector3 &p_motion, float p_margin, float &p_closest_safe, float &p_closest_unsafe, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false, ShapeRestInfo *r_info = NULL);
virtual bool cast_motion(const RID &p_shape, const Transform &p_xform, const Vector3 &p_motion, float p_margin, float &r_closest_safe, float &r_closest_unsafe, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false, ShapeRestInfo *r_info = NULL);
/// Returns the list of contacts pairs in this order: Local contact, other body contact
virtual bool collide_shape(RID p_shape, const Transform &p_shape_xform, float p_margin, Vector3 *r_results, int p_result_max, int &r_result_count, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false);
virtual bool rest_info(RID p_shape, const Transform &p_shape_xform, float p_margin, ShapeRestInfo *r_info, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false);