diff --git a/src/BulletCollision/CollisionShapes/btCollisionShape.h b/src/BulletCollision/CollisionShapes/btCollisionShape.h index 5e86568005..b7cfbc02a4 100644 --- a/src/BulletCollision/CollisionShapes/btCollisionShape.h +++ b/src/BulletCollision/CollisionShapes/btCollisionShape.h @@ -35,7 +35,7 @@ ATTRIBUTE_ALIGNED16(class) btCollisionShape BT_DECLARE_ALIGNED_ALLOCATOR(); - btCollisionShape() : m_shapeType (INVALID_SHAPE_PROXYTYPE), m_userPointer(0), m_userIndex(-1) + btCollisionShape() : m_shapeType (INVALID_SHAPE_PROXYTYPE), m_userPointer(0), m_userIndex(0) { } diff --git a/src/otbullet/discrete_dynamics_world.cpp b/src/otbullet/discrete_dynamics_world.cpp index d888fd2e09..a3a210b2c0 100644 --- a/src/otbullet/discrete_dynamics_world.cpp +++ b/src/otbullet/discrete_dynamics_world.cpp @@ -246,8 +246,10 @@ namespace ot { uint tri_count = 0; for (uints j = 0; j < _cow_internal.size(); j++) { - - btCollisionObjectWrapper internal_obj_wrapper(_cow_internal[j]._parent, + if (_cow_internal[j]._shape->getUserIndex() & 1) { // do not collide with terrain + continue; + } + btCollisionObjectWrapper internal_obj_wrapper(_cow_internal[j]._parent, _cow_internal[j]._shape, obj, _cow_internal[j]._worldTransform, @@ -317,9 +319,13 @@ namespace ot { //_relocation_offset = _tb_cache.get_array().ptr(); bool is_above_tm = false; + double3 under_terrain_contact; + float3 under_terrain_normal; + + int col_result = _aabb_intersect(_context, _from, _basis, _lod_dim, _triangles, _tree_batches, _tb_cache, frame_count, is_above_tm, under_terrain_contact, under_terrain_normal); - if (!_aabb_intersect(_context, _from, _basis, _lod_dim, _triangles, _tree_batches, _tb_cache, frame_count,is_above_tm)) { - DASSERT(_tree_batches.size() == 0); + if (col_result == 0) { + //DASSERT(_tree_batches.size() == 0); continue; } /* @@ -357,7 +363,13 @@ namespace ot { _stats.triangle_processing_time_ms += timer.time_ns() * 0.000001f; #endif // _PROFILING_ENABLED - } + } + else if (col_result == -1) { + gContactAddedCallback = nullptr; + res.addContactPoint(btVector3(under_terrain_normal.x, under_terrain_normal.y, under_terrain_normal.z), + btVector3(_from.x, _from.y, _from.z), + -glm::length(_from - under_terrain_contact)); + } if (_tree_batches.size() > 0) { prepare_tree_collision_pairs(obj, _tree_batches, frame_count); @@ -369,7 +381,7 @@ namespace ot { res.refreshContactPoints(); - if (manifold->getNumContacts() == 0 || tri_count == 0) { + if (manifold->getNumContacts() == 0 /*|| (tri_count == 0)*/) { getDispatcher()->releaseManifold(manifold); _manifolds.get_item(rb->getTerrainManifoldHandle()); _manifolds.del(_manifolds.get_item(rb->getTerrainManifoldHandle())); diff --git a/src/otbullet/discrete_dynamics_world.h b/src/otbullet/discrete_dynamics_world.h index 89f32d126f..a31ea472b1 100644 --- a/src/otbullet/discrete_dynamics_world.h +++ b/src/otbullet/discrete_dynamics_world.h @@ -194,7 +194,7 @@ class discrete_dynamics_world : public btDiscreteDynamicsWorld coid::slotalloc& tree_batches, uint frame ); - typedef bool(*fn_ext_collision_2)( + typedef int(*fn_ext_collision_2)( const void* context, const double3& center, const float3x3& basis, @@ -203,7 +203,9 @@ class discrete_dynamics_world : public btDiscreteDynamicsWorld coid::dynarray& trees, coid::slotalloc& tree_batches, uint frame, - bool& is_above_tm); + bool& is_above_tm, + double3& under_contact, + float3& under_normal); typedef float3(*fn_process_tree_collision)(btRigidBody * obj, bt::tree_collision_contex & ctx, float time_step, coid::slotalloc& tree_batches ); diff --git a/src/otbullet/ot_terrain_contact_common.cpp b/src/otbullet/ot_terrain_contact_common.cpp index d253e5dcdf..a5b65dbb8a 100644 --- a/src/otbullet/ot_terrain_contact_common.cpp +++ b/src/otbullet/ot_terrain_contact_common.cpp @@ -330,6 +330,10 @@ void ot_terrain_contact_common::collide_convex_triangle(const bt::triangle & tri btCollisionObjectWrapper triObWrap(_manifold->getBody1Wrap(), &tm, _manifold->getBody1Wrap()->getCollisionObject(), _manifold->getBody1Wrap()->getWorldTransform(), 0, triangle.tri_idx);//correct transform? + + const btCollisionObjectWrapper * curr_col_obj_wrapper = _manifold->getBody0Wrap(); + _manifold->setBody0Wrap(_convex_object); + btCollisionAlgorithm* colAlgo = _collision_world->getDispatcher()->findAlgorithm(_manifold->getBody0Wrap(), &triObWrap, _manifold->getPersistentManifold()); const btCollisionObjectWrapper* tmpWrap = 0; @@ -341,6 +345,7 @@ void ot_terrain_contact_common::collide_convex_triangle(const bt::triangle & tri colAlgo->processCollision(_manifold->getBody0Wrap(), &triObWrap, _collision_world->getDispatchInfo(), _manifold); + _manifold->setBody1Wrap(curr_col_obj_wrapper); _manifold->setBody1Wrap(tmpWrap); colAlgo->~btCollisionAlgorithm(); diff --git a/src/otbullet/otbullet.hpp b/src/otbullet/otbullet.hpp index 79194ecdcb..859dafe976 100644 --- a/src/otbullet/otbullet.hpp +++ b/src/otbullet/otbullet.hpp @@ -95,7 +95,7 @@ class physics : public policy_intrusive_base coid::slotalloc& tree_batches, uint frame ); - ifc_event bool terrain_collisions_aabb( + ifc_event int terrain_collisions_aabb( const void* context, const double3& center, float3x3 basis, @@ -104,7 +104,9 @@ class physics : public policy_intrusive_base coid::dynarray& trees, coid::slotalloc& tree_batches, uint frame, - bool& is_above_tm); + bool& is_above_tm, + double3& under_contact, + float3& under_normal); ifc_event float3 tree_collisions(btRigidBody * obj, bt::tree_collision_contex & ctx, diff --git a/src/otbullet/otbullet.intergen.cpp b/src/otbullet/otbullet.intergen.cpp index 11d1127e0a..c01c16dbd1 100644 --- a/src/otbullet/otbullet.intergen.cpp +++ b/src/otbullet/otbullet.intergen.cpp @@ -205,10 +205,10 @@ class physics_dispatcher : public physics on ? (void*)&_generic_interface_creator : nullptr); interface_register::register_interface_creator( - "bt::physics.create@2719886067", + "bt::physics.create@2650151887", on ? (void*)&create : nullptr); interface_register::register_interface_creator( - "bt::physics.get@2719886067", + "bt::physics.get@2650151887", on ? (void*)&get : nullptr); } }; @@ -240,12 +240,12 @@ bool physics::terrain_collisions( const void* context, const double3& center, fl return _ifc_host->iface()->terrain_collisions(context, center, radius, lod_dimension, data, trees, tree_batches, frame); } -bool physics::terrain_collisions_aabb( const void* context, const double3& center, float3x3 basis, float lod_dimension, coid::dynarray& data, coid::dynarray& trees, coid::slotalloc& tree_batches, uint frame, bool& is_above_tm ) +int physics::terrain_collisions_aabb( const void* context, const double3& center, float3x3 basis, float lod_dimension, coid::dynarray& data, coid::dynarray& trees, coid::slotalloc& tree_batches, uint frame, bool& is_above_tm, double3& under_contact, float3& under_normal ) { if (!_ifc_host) throw coid::exception() << "terrain_collisions_aabb" << " handler not implemented"; else - return _ifc_host->iface()->terrain_collisions_aabb(context, center, basis, lod_dimension, data, trees, tree_batches, frame, is_above_tm); + return _ifc_host->iface()->terrain_collisions_aabb(context, center, basis, lod_dimension, data, trees, tree_batches, frame, is_above_tm, under_contact, under_normal); } float3 physics::tree_collisions( btRigidBody* obj, bt::tree_collision_contex& ctx, float time_step, coid::slotalloc& tree_batches ) diff --git a/src/otbullet/physics.h b/src/otbullet/physics.h index 44322fb9aa..216057a3bf 100644 --- a/src/otbullet/physics.h +++ b/src/otbullet/physics.h @@ -122,7 +122,7 @@ class physics virtual bool terrain_collisions( const void* context, const double3& center, float radius, float lod_dimension, coid::dynarray& data, coid::dynarray& trees, coid::slotalloc& tree_batches, uint frame ){ throw coid::exception("handler not implemented"); } - virtual bool terrain_collisions_aabb( const void* context, const double3& center, float3x3 basis, float lod_dimension, coid::dynarray& data, coid::dynarray& trees, coid::slotalloc& tree_batches, uint frame, bool& is_above_tm ){ throw coid::exception("handler not implemented"); } + virtual int terrain_collisions_aabb( const void* context, const double3& center, float3x3 basis, float lod_dimension, coid::dynarray& data, coid::dynarray& trees, coid::slotalloc& tree_batches, uint frame, bool& is_above_tm, double3& under_contact, float3& under_normal ){ throw coid::exception("handler not implemented"); } virtual float3 tree_collisions( btRigidBody* obj, bt::tree_collision_contex& ctx, float time_step, coid::slotalloc& tree_batches ){ throw coid::exception("handler not implemented"); } @@ -170,7 +170,7 @@ class physics if (_cleaner) _cleaner(this,0); } - static const int HASHID = 2719886067; + static const int HASHID = 2650151887; int intergen_hash_id() const override final { return HASHID; } @@ -184,7 +184,7 @@ class physics } static const coid::token& intergen_default_creator_static( EBackend bck ) { - static const coid::token _dc("bt::physics.get@2719886067"); + static const coid::token _dc("bt::physics.get@2650151887"); static const coid::token _djs("bt::physics@wrapper.js"); static const coid::token _dlua("bt::physics@wrapper.lua"); static const coid::token _dnone; @@ -258,7 +258,7 @@ inline iref physics::create( T* _subclass_, double r, void* context ) typedef iref (*fn_creator)(physics*, double, void*); static fn_creator create = 0; - static const coid::token ifckey = "bt::physics.create@2719886067"; + static const coid::token ifckey = "bt::physics.create@2650151887"; if (!create) create = reinterpret_cast( @@ -276,7 +276,7 @@ inline iref physics::get( T* _subclass_ ) typedef iref (*fn_creator)(physics*); static fn_creator create = 0; - static const coid::token ifckey = "bt::physics.get@2719886067"; + static const coid::token ifckey = "bt::physics.get@2650151887"; if (!create) create = reinterpret_cast( diff --git a/src/otbullet/wrapper.cpp b/src/otbullet/wrapper.cpp index dec8e43705..3f61becef0 100644 --- a/src/otbullet/wrapper.cpp +++ b/src/otbullet/wrapper.cpp @@ -40,7 +40,7 @@ extern bool _ext_collider(const void* context, coid::slotalloc& tree_batches, uint frame ); -extern bool _ext_collider_obb( +extern int _ext_collider_obb( const void * context, const double3& center, const float3x3& basis, @@ -49,7 +49,9 @@ extern bool _ext_collider_obb( coid::dynarray& trees, coid::slotalloc& tree_batches, uint frame, - bool& is_above_tm); + bool& is_above_tm, + double3& under_contact, + float3& under_normal); extern float _ext_terrain_ray_intersect( const void* planet, @@ -86,7 +88,7 @@ static bool _ext_collider( return _physics->terrain_collisions(planet, center, radius, lod_dimension, data, trees, tree_batches, frame); } -static bool _ext_collider_obb( +static int _ext_collider_obb( const void * planet, const double3& center, const float3x3& basis, @@ -95,9 +97,11 @@ static bool _ext_collider_obb( coid::dynarray& trees, coid::slotalloc& tree_batches, uint frame, - bool& is_above_tm) + bool& is_above_tm, + double3& under_contact, + float3& under_normal) { - return _physics->terrain_collisions_aabb(planet, center, basis, lod_dimension, data, trees, tree_batches, frame,is_above_tm); + return _physics->terrain_collisions_aabb(planet, center, basis, lod_dimension, data, trees, tree_batches, frame,is_above_tm, under_contact, under_normal); } static float _ext_terrain_ray_intersect(