Cysharp / MagicPhysX

.NET PhysX 5 binding to all platforms(win, osx, linux) for 3D engine, deep learning, dedicated server of gaming.
MIT License
255 stars 17 forks source link

CCT example #15

Closed NAMiedema closed 10 months ago

NAMiedema commented 10 months ago

Hey there, I hope someone might be able to help me. I'm trying to get a CCT to work, I am able to detect the capsule of the CCT in overlap queries and also am able to move. However the CCT seems to ignore all static geometry in the scene and will just fly through everything. An example of a working CCT would be really helpful.

Here is also a snippet of how I am initializing and calling the cct. Any gelp or insights are greatly appreciated.

        /// <summary>
        /// Initializes the <see cref="CharacterController"/>.
        /// </summary>
        /// <param name="manager">The manager.</param>
        internal void Init(PhysicsSystem physics, PxControllerManager* manager)
        {
            _desc = PxCapsuleControllerDesc_new_alloc();
            var material = physics.CreateMaterial(1, 1, 1);
            _desc->position = PxExtendedVec3_new_1(_initPosition.X, _initPosition.Y, _initPosition.Z);
            _desc->stepOffset = _initStepOffset;
            _desc->contactOffset = _initcontactOffset;
            _desc->radius = _initRadius;
            _desc->height = _initHeight;
            _desc->upDirection = -Vector3.UnitY;
            _desc->material = material;

            var filterData = PxFilterData_new(PxEMPTY.PxEmpty);       
            _filters = PxControllerFilters_new(&filterData, null, null);
             _controller = PxControllerManager_createController_mut(manager, (PxControllerDesc*)_desc);
            IsInitialized = true;
        }

        /// <summary>
        /// Moves the character controller.
        /// </summary>
        /// <param name="deltaTime">passed amount of time.</param>
        /// <param name="direction">The direction to move in.</param>
        public void Move(float deltaTime, Vector3 direction)
        {
            _direction = direction;
            fixed (PxVec3* dirPtr = &_direction)
            fixed (PxControllerFilters* fltrPtr = &_filters)
            {
                var res = PxController_move_mut(_controller, dirPtr, float.Epsilon, deltaTime, fltrPtr, null);
                if(res == PxControllerCollisionFlags.CollisionUp || res == PxControllerCollisionFlags.CollisionDown || res == PxControllerCollisionFlags.CollisionSides)
                {
                    Console.WriteLine(res);
                }
            }

        }
NAMiedema commented 10 months ago

I was able to fix this one myself. I completely missed that fixed pointers only work in the C# context and that I needed to use alloc for any pointer I sent to the lib. My final code came out a bit like this. Still might be nice to use it for an example to help others.

        private PxController* _controller;
        private PxControllerFilters* _filters;
        private PxCapsuleControllerDesc* _desc;
        private bool _isInitialized;
        private PxObstacleContext* _context;
        private PxFilterData* _filterData;
        private PxVec3 _direction;

        /// <summary>
        /// Initializes the <see cref="CharacterController"/>.
        /// </summary>
        /// <param name="manager">The manager.</param>
        internal void Init(PhysicsSystem physics, PxControllerManager* manager)
        {
            _desc = PxCapsuleControllerDesc_new_alloc();
            var material = physics.CreateMaterial(.1f, .1f, .1f);
            _desc->position = PxExtendedVec3_new_1(_initPosition.X, _initPosition.Y, _initPosition.Z);
            _desc->stepOffset = _initStepOffset;
            _desc->contactOffset = _initcontactOffset;
            _desc->radius = _initRadius;
            _desc->height = _initHeight;
            _desc->upDirection = -Vector3.UnitY;
            _desc->material = material;

            _filterData = (PxFilterData*)Marshal.AllocHGlobal(sizeof(PxFilterData));
            var val = PxFilterData_new(PxEMPTY.PxEmpty);
            Marshal.StructureToPtr(val, (nint)_filterData, false);

            //var filterData = PxFilterData_new(PxEMPTY.PxEmpty);       
            var filters = PxControllerFilters_new(_filterData, null, null);
            _filters = (PxControllerFilters*)Marshal.AllocHGlobal(sizeof(PxControllerFilters));
            Marshal.StructureToPtr(filters, (nint)_filters, false);
            manager->SetPreciseSweepsMut(true);
            manager->SetOverlapRecoveryModuleMut(true);
             _controller = PxControllerManager_createController_mut(manager, (PxControllerDesc*)_desc);
            IsInitialized = true;
        }

        /// <summary>
        /// Moves the character controller.
        /// </summary>
        /// <param name="deltaTime">passed amount of time.</param>
        /// <param name="direction">The direction to move in.</param>
        public void Move(float deltaTime, Vector3 direction)
        {
            _direction = direction;
            fixed (PxVec3* dirPtr = &_direction)
           // fixed (PxControllerFilters* fltrPtr = &_filters)
            {
                var res = PxController_move_mut(_controller, dirPtr, float.Epsilon, deltaTime, _filters, null);
                if(res == PxControllerCollisionFlags.CollisionUp || res == PxControllerCollisionFlags.CollisionDown || res == PxControllerCollisionFlags.CollisionSides)
                {
                    Console.WriteLine(res);
                }
            }

        }