stride
stride copied to clipboard
Can't change CharacterComponent's collider shape properties
Release Type: Official Release
Version: 4.1.0.1948
Platform(s): Windows
Describe the bug
Assigning a new ColliderShape into CharacterComponent in code (when game is running) and then calling CharacterComponent.ComposeShape(); causes the application to crash.
To Reproduce
- Create basic character with CharacterComponent.
- Assign capsule collider shape using Stride Studio.
- Assign script which perform this code (ex. when key is pressed)
characterComponent.ColliderShapes[0] = new CapsuleColliderShapeDesc() { Length = 0.6f, LocalOffset = new Vector3(0.0f, 0.70f, 0.0f), Orientation = ShapeOrientation.UpY, Radius = 0.4f };
characterPhysics.ComposeShape();
- This will caus the application to crash.
Expected behavior CharacterComponent starts to use new collider shape with new property values...
Log and callstacks
- I can see this in Windows Application Events: dDrive.Windows.exe 1.0.0.0 64e81669 libbulletc.DLL 0.0.0.0 5ccee150 c0000409 0000000000166069 3b48 01da0915ea6c6440 C:.....\Bin\Windows\Debug\win-x64\dDrive.Windows.exe C:.....\Bin\Windows\Debug\win-x64\libbulletc.DLL 6959c4f8-c585-472a-967f-89ccbed61439
Additional context
-
I need to change collider shape because of different character poses, like crouching and so on...
-
It seems trying to use Dispose() on any type of collider shape causes it.
ColliderShape.Dispose();is called insideStride.Engine.PhysicsComponent.ComposeShape()method. -
@Manio143 tip from discord:
I imagine we don't properly remove some reference after the dispose frees the native handle to the bullet collider and we still attempt to use it afterwards which is what causes the crash.
More context - in the CharacterComponent.OnAttach we create a new instance of PairCachingGhostObject from BulletSharp which is used to instantiate KinematicCharacterController from BulletSharp. We provide it a handle to the collider shape.
https://github.com/stride3d/stride/blob/e200843806af650ad45595f87a78374712ecaeea/sources/engine/Stride.Physics/Elements/CharacterComponent.cs#L267-L271
When you call ComposeShape we dispose of that internal handle and cause Bullet to try to "use after free" because it's holding a disposed reference to a native object.
Potential fix - make ComposeShape virtual and add reconstruction of the character controller in the component.
Potential workaround in the meantime - remove entity with character component from the scene, call ComposeShape, re-add to the scene. This will cause the correct disposal and instantiation of the character controller.
Hi! If possible could I work on this issue? I wanted to double check first in case it was recommended to work elsewhere with the ongoing Bepu Physics integration.
Sure, if you want to do so, go ahead !