From 39fa9387e6b4711d8584bf1acdc73f2559f2807f Mon Sep 17 00:00:00 2001 From: Nick Fisher Date: Tue, 5 Mar 2024 10:30:42 +0800 Subject: [PATCH] expose removeCollisionComponent --- ios/include/FlutterFilamentApi.h | 1 + ios/src/FlutterFilamentApi.cpp | 4 ++++ lib/filament_controller.dart | 17 ++++++++++------- lib/filament_controller_ffi.dart | 18 +++++++++++++----- lib/generated_bindings.dart | 7 +++++++ 5 files changed, 35 insertions(+), 12 deletions(-) diff --git a/ios/include/FlutterFilamentApi.h b/ios/include/FlutterFilamentApi.h index c2479265..6c084a69 100644 --- a/ios/include/FlutterFilamentApi.h +++ b/ios/include/FlutterFilamentApi.h @@ -194,6 +194,7 @@ extern "C" FLUTTER_PLUGIN_EXPORT void ios_dummy(); FLUTTER_PLUGIN_EXPORT void flutter_filament_free(void *ptr); FLUTTER_PLUGIN_EXPORT void add_collision_component(void *const sceneManager, EntityId entityId, void (*callback)(const EntityId entityId1, const EntityId entityId2), bool affectsCollidingTransform); + FLUTTER_PLUGIN_EXPORT void remove_collision_component(void *const sceneManager, EntityId entityId); FLUTTER_PLUGIN_EXPORT void add_animation_component(void *const sceneManager, EntityId entityId); FLUTTER_PLUGIN_EXPORT EntityId create_geometry(void *const viewer, float* vertices, int numVertices, uint16_t* indices, int numIndices, const char* materialPath); diff --git a/ios/src/FlutterFilamentApi.cpp b/ios/src/FlutterFilamentApi.cpp index 5adc4249..31882754 100644 --- a/ios/src/FlutterFilamentApi.cpp +++ b/ios/src/FlutterFilamentApi.cpp @@ -589,6 +589,10 @@ extern "C" ((SceneManager*)sceneManager)->addCollisionComponent(entityId, onCollisionCallback, affectsCollidingTransform); } + FLUTTER_PLUGIN_EXPORT void remove_collision_component(void *const sceneManager, EntityId entityId) { + ((SceneManager*)sceneManager)->removeCollisionComponent(entityId); + } + FLUTTER_PLUGIN_EXPORT void add_animation_component(void *const sceneManager, EntityId entityId) { ((SceneManager*)sceneManager)->addAnimationComponent(entityId); } diff --git a/lib/filament_controller.dart b/lib/filament_controller.dart index 105deee4..0110ab56 100644 --- a/lib/filament_controller.dart +++ b/lib/filament_controller.dart @@ -626,15 +626,18 @@ abstract class FilamentController { Future addAnimationComponent(FilamentEntity entity); /// - /// Makes [collidableEntity] collidable with - /// (a) any entity whose transform is being controlled (via [control]) or - /// (b) any entity that has been marked as non-transformable but collidable (via [markNonTransformableCollidable]) - /// These are differentiated because a collision will affect the transform for controlled entities - /// (e.g. if a controlled entity collides with a wall, we ignore the control update to the transform so it doesn't go through the wall) + /// Makes [entity] collidable. + /// This allows you to call [testCollisions] with any other entity ("entity B") to see if [entity] has collided with entity B. The callback will be invoked if so. + /// Alternatively, if [affectsTransform] is true and this entity collides with another entity, any queued position updates to the latter entity will be ignored. /// - Future addCollisionComponent(FilamentEntity collidableEntity, + Future addCollisionComponent(FilamentEntity entity, {void Function(int entityId1, int entityId2)? callback, - bool affectsCollingTransform = false}); + bool affectsTransform = false}); + + /// + /// Removes the collision component from [entity], meaning this will no longer be tested when [testCollisions] or [queuePositionUpdate] is called with another entity. + /// + Future removeCollisionComponent(FilamentEntity entity); /// /// Creates a (renderable) entity with the specified geometry and adds to the scene. diff --git a/lib/filament_controller_ffi.dart b/lib/filament_controller_ffi.dart index 0beb5c3e..f1a6df5b 100644 --- a/lib/filament_controller_ffi.dart +++ b/lib/filament_controller_ffi.dart @@ -1485,10 +1485,12 @@ class FilamentControllerFFI extends FilamentController { return transformController; } + final _collisions = {}; + @override - Future addCollisionComponent(FilamentEntity collidableEntity, + Future addCollisionComponent(FilamentEntity entity, {void Function(int entityId1, int entityId2)? callback, - bool affectsCollingTransform = false}) async { + bool affectsTransform = false}) async { if (_sceneManager == null) { throw Exception("SceneManager must be non-null"); } @@ -1497,14 +1499,20 @@ class FilamentControllerFFI extends FilamentController { if (callback != null) { var ptr = NativeCallable< Void Function(Int32 entityId1, Int32 entityId2)>.listener(callback); - add_collision_component(_sceneManager!, collidableEntity, - ptr.nativeFunction, affectsCollingTransform); + add_collision_component( + _sceneManager!, entity, ptr.nativeFunction, affectsTransform); + _collisions[entity] = ptr; } else { add_collision_component( - _sceneManager!, collidableEntity, nullptr, affectsCollingTransform); + _sceneManager!, entity, nullptr, affectsTransform); } } + @override + Future removeCollisionComponent(FilamentEntity entity) async { + remove_collision_component(_sceneManager!, entity); + } + @override Future addAnimationComponent(FilamentEntity entity) async { add_animation_component(_sceneManager!, entity); diff --git a/lib/generated_bindings.dart b/lib/generated_bindings.dart index 617a9286..bdc9b2c0 100644 --- a/lib/generated_bindings.dart +++ b/lib/generated_bindings.dart @@ -931,6 +931,13 @@ external void add_collision_component( bool affectsCollidingTransform, ); +@ffi.Native, EntityId)>( + symbol: 'remove_collision_component', assetId: 'flutter_filament_plugin') +external void remove_collision_component( + ffi.Pointer sceneManager, + int entityId, +); + @ffi.Native, EntityId)>( symbol: 'add_animation_component', assetId: 'flutter_filament_plugin') external void add_animation_component(