Charachter controller

Discuss any questions about BEPUphysics or problems encountered.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Charachter controller

Post by Norbo »

Working on the assumption that the picture is showing the character intersecting some world geometry, and further that this isn't what you want: setting the world transform of the character controller body teleports it. Teleporting objects will not undergo proper velocity-based collision response, because there are no velocities involved. It's just immediately changing positions to wherever it was told to go. In that code, cci.Camera fully controls the position of the character; collisions can't change anything.

Character controllers should be told to move using the various exposed properties, like CharacterController.HorizontalMotionConstraint.MovementDirection. Check out the default CharacterControllerInput implementation for examples of its usage.
mohammadAdnan
Posts: 48
Joined: Sat Jan 26, 2013 5:54 pm

Re: Charachter controller

Post by mohammadAdnan »

Thaaaanks Norbo :) , now it's working here is the code :

Code: Select all

// Add Enemy to Physics Space
            enemyChrachterController = new CharacterController();
            enemyChrachterController.HorizontalMotionConstraint.Speed *= 15;
            enemyChrachterController.HorizontalMotionConstraint.CrouchingSpeed *= 10;
            enemyChrachterController.HorizontalMotionConstraint.MaximumForce = 2500;
            enemyChrachterController.HorizontalMotionConstraint.MaximumAirForce = 5000;
            enemyChrachterController.StepManager.MaximumStepHeight = 20;
            enemyChrachterController.SlidingJumpSpeed = 20;
            enemyChrachterController.Body.IsAffectedByGravity = true;
            enemyChrachterController.Body.Radius = 30;
            enemyChrachterController.Body.Tag = "noDisplayObject";
            space.Add(enemyChrachterController);
The last thing before done this post , Right Now I can add characters with multiple Animations clips , they collide and everything looks great :) , now :
Q1 : For characters bounded by charactercontroller.Body entity, If collision happened ( say gun Bullet collide with the entity , in case this entity is the bounding cylinder ) how to make this entity receive to collision and move for example .

Q2: Norbo my character as shown in the picture below , is not stuck to the ground :
Image
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Charachter controller

Post by Norbo »

Q1 : For characters bounded by charactercontroller.Body entity, If collision happened ( say gun Bullet collide with the entity , in case this entity is the bounding cylinder ) how to make this entity receive to collision and move for example .
The character controller body is an entity like any other; applying an impulse or changing the velocity or whatever else will move the character appropriately. Regular collisions will already be handled correctly and automatically.
Q2: Norbo my character as shown in the picture below , is not stuck to the ground :
The graphical model is probably not aligned with the physics. The details are left as an exercise :P
mohammadAdnan
Posts: 48
Joined: Sat Jan 26, 2013 5:54 pm

Re: Charachter controller

Post by mohammadAdnan »

Norbo , Thank you for left it as exercises teacher :cry: , by the way I will show for you what is going on !! I added it to the space and it's not affected (Gravity ) it's just collide greatly which shows it matches with the 3d model .

Norbo you the enemy collide and math the entity because it collided naturally with the world ? why you says it's not matched with the world :

here is picture the body entity that bounded my enemy is flying and thats why my enemy is up of the ground :

Image

the code :

Code: Select all

            #region Charachter Controller Intialization
                       // Add Enemy to Physics Space
            enemyChrachterController = new CharacterController();
            enemyChrachterController.IsUpdating = true;

            enemyChrachterController.HorizontalMotionConstraint.Speed *= 5;
            enemyChrachterController.HorizontalMotionConstraint.CrouchingSpeed *= 10;
            enemyChrachterController.Body.IsAffectedByGravity = true;
            //enemyChrachterController.Body.Tag = "noDisplayObject";
            enemyChrachterController.Body.WorldTransform = Matrix.CreateTranslation(dwarfPosition);
            enemyChrachterController.IsUpdating = true;
            space.Add(enemyChrachterController.Body);
in the update method here is the code :

Code: Select all

 enemyChrachterController.Body.Position = dwarfPosition;
            dwarfAnimator.World =  Matrix.CreateScale(5) *
                rotationModel * enemyChrachterController.Body.WorldTransform;
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Charachter controller

Post by Norbo »

Code: Select all

space.Add(enemyChrachterController.Body);
Add the CharacterController itself to the Space, not its body. Otherwise, none of the character's systems will operate.

Code: Select all

enemyChrachterController.Body.Position = dwarfPosition;
This is teleporting the character. It prevents physics from behaving correctly. It cannot fall correctly because it immediately gets teleported back up. If you want to move the character, do so using the character controller's properties, not by teleporting the entity. Again, please check the CharacterControllerInput in the BEPUphysicsDemos for examples of all of this.
mohammadAdnan
Posts: 48
Joined: Sat Jan 26, 2013 5:54 pm

Re: Charachter controller

Post by mohammadAdnan »

Hello Norbo , I am about 99% to finish physics side in my game , but the problem still exist :( , I did not yet move enemy along with the character controller body , I don't need code Norbo if you can just say your opinion about the code :), I go to "character controller Input.cs" source code and saw the implementation and tried to do so , but not working correctly :

1 : LoadContent() {
.
.

Code: Select all

// Add Enemy to Physics Space
            enemyChrachterController = new CharacterController();
            space.Add(enemyChrachterController);
            enemyChrachterController.HorizontalMotionConstraint.Speed *= 5;
            enemyChrachterController.HorizontalMotionConstraint.CrouchingSpeed *= 10;
            enemyChrachterController.HorizontalMotionConstraint.MaximumAirForce = 5000;
            enemyChrachterController.StepManager.MaximumStepHeight = 20;
}

2 : Update Method () {
.
.

Code: Select all

            #region Enemy

            totalMovement = Vector2.Zero;
            forwrd = cci.Camera.WorldMatrix.Forward;
            forwrd.Normalize();
            totalMovement += new Vector2(forwrd.X,forwrd.Z);
            enemyChrachterController.HorizontalMotionConstraint.MovementDirection = Vector2.Normalize(totalMovement);
         
            enemyPosition += new Vector3(totalMovement.X, 0, totalMovement.Y);

            if (currentSpeed > 0)
            {
                dwarfPosition += (-Matrix.CreateTranslation(0, 0, currentSpeed) *
                    Matrix.CreateTranslation(new Vector3(totalMovement.X,0,totalMovement.Y)) ).Translation;

                totalMovement += new Vector2(dwarfPosition.X, dwarfPosition.Z);
            }

if (stateModel == "idle")
            {
                currentSpeed = 0;
                // Add this to the beginning of the if (state=="idle") block in the UpdateState method
                // Remove the old if (keyState.IsKeyDown(Keys.W)) block
                if (keyState.IsKeyDown(Keys.W))
                {
                    blendFactor = 0;
                    stateModel = "idleToWalk";

                    movementDirection = cci.Camera.WorldMatrix.Forward;
                    totalMovement += Vector2.Normalize(new Vector2(movementDirection.X, movementDirection.Z));

                }
                // Add this in the if (state=="idle") block of the UpdateState method
                if (keyState.IsKeyDown(Keys.Space))
                {
                    crouch.ElapsedTime = 0;
                    crouch.IsLooping = false;
                    stateModel = "crouchDown";
                }

                if (mouseState.LeftButton == ButtonState.Pressed)
                {
                    stateModel = "attack0";
                }

                RunController(dwarfAnimator, idle);

            }
            else if (stateModel == "walk")
            {
                currentSpeed = WALK_SPEED;
                // Add this to the beginning of the if (state=="walk") block in the UpdateState method
                // Remove the old if (keyState.IsKeyUp(Keys.W)) block
                if (keyState.IsKeyUp(Keys.W))
                {
                    blendFactor = 0;
                    stateModel = "walkToIdle";
                }
                if (keyState.IsKeyDown(Keys.LeftShift) && keyState.IsKeyDown(Keys.W))
                {
                    blendFactor = 0;
                    stateModel = "walkToRun";
                    run.SpeedFactor = 0;
                }
                RunController(dwarfAnimator, walk);
            }

            if (totalMovement == Vector2.Zero)
                enemyChrachterController.HorizontalMotionConstraint.MovementDirection = Vector2.Zero;
            else
                enemyChrachterController.HorizontalMotionConstraint.MovementDirection = Vector2.Normalize(totalMovement);

            dwarfAnimator.World = Matrix.CreateScale(5) *
                rotationModel * enemyChrachterController.Body.WorldTransform;;
Norbo , the enemy not moving at all ( translated ) not the Character controller ( before it was moved correctly and collide but my problem was that it was not touching the ground )
mohammadAdnan
Posts: 48
Joined: Sat Jan 26, 2013 5:54 pm

Re: Charachter controller

Post by mohammadAdnan »

Ok , Norbo the last questions :

Q1 : Do I have to use characterController like the one used for hero ( Player ) for NPC ( enemy ) too ?? and if yes it's implementation based on Camera transformation ? and I don't want it to be like that as I am moving my character based on AI behaviours not based on camera.forward and etc ..... ( Sorry for english )?

Q2 : I can check if the enemy was on the ground or not and doing great things based on that , but How to make Enemy which is supposed to use characterController stick to the ground if it's supposed to use characterController ? ( General Steps Norbo if you can :) for making enemy stick to the ground )

Q3 : When transforming the enemy which is supposed to use characterController I multiply it's transformations matrices by enemyCharacterController.Body.WordTransform ; (Right Norbo ?? )

Thanks Norbo for your great Library :) and for your efforts .
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Charachter controller

Post by Norbo »

Do I have to use characterController like the one used for hero ( Player ) for NPC ( enemy ) too ?
If it is supposed to behave like a character, it should use a character controller.
and if yes it's implementation based on Camera transformation ?
The character controller can be directed without any camera. Check the CharacterStressTestDemo for an example of thousands of controllers acting independently with no camera involved.
but How to make Enemy which is supposed to use characterController stick to the ground if it's supposed to use characterController ?
Sticking to the ground should be the default automatic behavior for a character controller, assuming nothing is interfering (e.g. teleporting it up into the air).
When transforming the enemy which is supposed to use characterController I multiply it's transformations matrices by enemyCharacterController.Body.WordTransform
If you're referring to transforming the graphical model, then yes- but you'll probably need an additional local transform to get the model aligned with the physical cylinder, unless the graphical model just happens to be centered properly.

Most of these questions could be answered by searching or studying examples (such as those in the BEPUphysicsDemos); in the future, please spend additional time with those resources to see if they can answer your questions first.
Post Reply