Page 1 of 2

Chrachter Controller

Posted: Sun May 19, 2013 1:12 pm
by mohammadAdnan
Hello Norbo , I face problem with aligning the enemy to the character controller , I know you talk a lot about this problem :) so hope you will be patient on my questions Norbo , but I think that for my case it differs than the problems in other questions that related to align physics to graphics :

I load .x file with animations clips ( hierarchal animations ) , the animations in my file splitted from 0 to 400 ( walk , run ......etc ) , I create instance of character controller and it's move correctly with the model.x file , I look at the documentation of your Norbo and you says in your library that " ChrachterController.Body.CollisionInformation.LocalPosition " localPosition is used to offset the collision geometry , so I do what you said :

Code: Select all

        public void LoadContent(ContentManager Content)
        {
            #region Enemy
            Model model = Content.Load<Model>("Models//Beast//beastmodel");

            enemyAnimator = new ModelAnimator(game, model);
            
             idle = new AnimationController(game, enemyAnimator.Animations["idle"]);
            // Add this in LoadGraphicsContent
            run = new AnimationController(game, enemyAnimator.Animations["Jump with no Z movement for control in-game"]);
            walk = new AnimationController(game, enemyAnimator.Animations["walk"]);
            crouch = new AnimationController(game, enemyAnimator.Animations["crouchDown"]);
            stayCrouched = new AnimationController(game, enemyAnimator.Animations["stayCrouched"]);
            killed = new AnimationController(game, enemyAnimator.Animations["Die"]);
            attak = new AnimationController(game, enemyAnimator.Animations["Bite 2"]);

            // Add Enemy to Physics Space
            enemyChrachterController = new CharacterController();
            enemyChrachterController.HorizontalMotionConstraint.Speed *= 20;
            enemyChrachterController.HorizontalMotionConstraint.CrouchingSpeed *= 10;
            enemyChrachterController.HorizontalMotionConstraint.MaximumForce = 5000;
            enemyChrachterController.HorizontalMotionConstraint.MaximumAirForce = 5000;
            enemyChrachterController.Body.Tag = "noDisplayObject";
            enemyChrachterController.Body.Height = 80;
            enemyChrachterController.Body.Radius = 40;
            enemyChrachterController.Body.Position = dwarfPosition ;
            Vector3 center = enemyChrachterController.Body.CollisionInformation.LocalPosition;
            enemyChrachterController.Body.WorldTransform *= 
                Matrix.CreateTranslation(-center);
            
            space.Add(enemyChrachterController);
            RunController(enemyAnimator, idle);
            
           #endregion
           
}

void Update(GameTime gameTime, KeyboardState keyboardInputt, 
            KeyboardState previousKeyboardInputt, MouseState mouseInput)
{ 
                 #region Enemy
               enemyAnimator.World = Matrix.CreateScale(5) * rotationModel *Matrix.CreateTranslation(enemyChrachterController.Body.CollisionInformation.LocalPosition)*
              enemyChrachterController.Body.WorldTransform;

              foreach (ModelMesh mesh in enemyAnimator.Model.Meshes)
                  foreach (Effect effect in mesh.Effects)
                       effect.Parameters["View"].SetValue(camera.ViewMatrix);
                 #endregion
}



Norbo the Model is still in the air and not touching the ground but move correctly with the physics entity ??
Norbo every thing works great , when I am close to enemy he follow me by applying force to character controller then the model.x follow but the model.x is offsets from the ground and I think it's because center of mass because the model.x 100% collide and behaviors greatly .

Re: Chrachter Controller

Posted: Sun May 19, 2013 7:14 pm
by Norbo
Entity.CollisionInformation.LocalPosition defaults to zero and should be kept at zero unless you want to make an entity effectively rotate around a point other than its center of mass. What it does is offset the collision shape from the entity's Position property. You don't want to do this.

Further, because I don't see you setting the LocalPosition anywhere, I presume it's still the default of zero. That means these lines do nothing:

Code: Select all

            Vector3 center = enemyChrachterController.Body.CollisionInformation.LocalPosition;
            enemyChrachterController.Body.WorldTransform *= 
                Matrix.CreateTranslation(-center);
In addition, the local translation part of this line does nothing:

Code: Select all

enemyAnimator.World = Matrix.CreateScale(5) * rotationModel *Matrix.CreateTranslation(enemyChrachterController.Body.CollisionInformation.LocalPosition)*
              enemyChrachterController.Body.WorldTransform;
However, that line can be adapted to do what you actually want to do. Instead of using the Entity.CollisionInformation.LocalPosition to translate, use whatever offset is needed to pull the graphic into alignment with the physics. Again, to emphasize, this is a purely graphical transformation; the model was built with a different origin in mind (probably at the bottom of its feet), and you must compensate for that because the entity's position is at its center of mass.
when I am close to enemy he follow me by applying force to character controller
I would recommend moving the character by telling it to move, rather than pushing it around. Applying forces will not work well in all cases.
I think that for my case it differs than the problems in other questions that related to align physics to graphic
For reference, it sounds like what you're seeing is exactly the same as the other physics-graphics alignment issues, so any advice you see about those issues should also apply here.

Re: Chrachter Controller

Posted: Sun May 19, 2013 10:21 pm
by mohammadAdnan
Yeah Norbo , it seems that the problem with center of mass :

Image

Norbo , I used matrixOffset :

Matrix matrixOffset = Matrix.CreateTranslation(Vector3.Down * 0.5f);

Code: Select all

void loadContent(....)
{
// Add Enemy to Physics Space
            enemyChrachterController = new CharacterController();
            enemyChrachterController.HorizontalMotionConstraint.Speed *= 20;
            enemyChrachterController.HorizontalMotionConstraint.CrouchingSpeed *= 10;
            enemyChrachterController.HorizontalMotionConstraint.MaximumForce = 5000;
            enemyChrachterController.HorizontalMotionConstraint.MaximumAirForce = 5000;
           //enemyChrachterController.Body.Tag = "noDisplayObject";
            enemyChrachterController.Body.Height = 80;
            enemyChrachterController.Body.Radius = 40;

enemyAnimator.World = matrixOffset *
                Matrix.CreateTranslation(enemyChrachterController.Body.Position);
}

void Update(.....)
{
Matrix matrixTranslate = matrixOffset * 
                Matrix.CreateTranslation(enemyChrachterController.Body.Position);

            enemyAnimator.World = Matrix.CreateScale(50) * rotationModel * matrixTranslate;
}

the result is shown in the picture above , what is your steps Norbo ? I had done this for all my models before and they are working 100 % but for this model may be because graphic bounded by character controller make it differs

Re: Chrachter Controller

Posted: Sun May 19, 2013 10:25 pm
by Norbo
Given that the character's height is 80, and assuming that the graphical model's origin is at its feet, then you need to offset the graphical model by 40 down.

Re: Chrachter Controller

Posted: Sun May 19, 2013 10:33 pm
by mohammadAdnan
thank you teacher Norbooooooooooooooo , I worth nothing without you , I want to be like you give me some hints pls ?!!!!

Re: Chrachter Controller

Posted: Sun May 19, 2013 11:01 pm
by Norbo
I want to be like you give me some hints pls ?!!!!
Eat lots of veggies, sacrifice a duck to appease the Physbeast, and meditate about orbiting multicolored cubes. ;)

Re: Chrachter Controller

Posted: Tue May 21, 2013 11:37 am
by mohammadAdnan
Hello Norbo , some times when the enemy attack and become closer to me ( colliode ) BEPU fire errore :

Image

Re: Chrachter Controller

Posted: Tue May 21, 2013 8:18 pm
by Norbo
I'm not sure what's causing that; could you reproduce the issue in the latest BEPUphysicsDemos for me to look at?

A null reference exception there would imply that the contact collection enumeration is failing (assuming that that code is unmodified from the SupportFinder). The most likely source of that would be in a CollidablePairHandler.GetContactInformation implementation somewhere, but I don't know of any issues there. If you're using an older version, it may be that it was an old bug that got fixed at some point.

Re: Chrachter Controller

Posted: Wed May 22, 2013 7:56 pm
by mohammadAdnan
thanks Norbo , the last question before close this discussion because it's not relevant to this post !

Ok you are right now every thing looks great and the character controller works great :) , but when I change the Lib to new version , ModelDrawer give errore

Norbo ( I include the new Model folder into my game ) , do you change the way we can create instance of ModelDrawer and use it to Draw BEPU entities ?

Image

Re: Chrachter Controller

Posted: Wed May 22, 2013 8:10 pm
by Norbo
The BEPUphysicsDrawer .dll uses a BEPUphysics reference. It will search for a particular version of BEPUphysics. If there's a mismatch, assembly loading will fail and you'll get something like that exception.

To fix it, grab the BEPUphysicsDrawer .dll compiled from the latest BEPUphysics source.

Re: Chrachter Controller

Posted: Wed May 22, 2013 8:49 pm
by mohammadAdnan
thanks Norbo it works ,but in the last Update World being drawn using InstanceModelDrawer , so the mesh for the world looks like mega boxes !! OK Norbo thanks

Re: Chrachter Controller

Posted: Thu May 23, 2013 8:52 pm
by mohammadAdnan
Hello teacher Norbo,

If I want to find the hits against the character controller which is hit by bullets so that I can make some AI based on it for the state machine AI , I have strategy but before I do my one I want to take yours if you can teacher Norbo!!!

Re: Chrachter Controller

Posted: Thu May 23, 2013 9:03 pm
by Norbo
Bullets are often represented by ray casts, so using Space.RayCast would do the trick. If the query hits a character's body, the character can be told to react. The ray cast will return the hit location, normal, and other information.

If the bullets are actually entities themselves, then you can use either collision events or contact checking (iterating through the pairs of a character's collidable, and through the contacts of each pair).

Re: Chrachter Controller

Posted: Thu May 23, 2013 9:30 pm
by mohammadAdnan
Yeah Norbo , I had already do this , but now I have many enemies , how could I know if the bullet hit for example the Zombie not the dog ( both of them use chrachter controller Norbo ) and based on that increase the number of hits for that enemy , then if he gets enough hits die of flee(AI) ........etc , I try to iterate through all entities but that's not working for my case as I had to know exactly which one the bullet hit then increase the number of hits

Re: Chrachter Controller

Posted: Thu May 23, 2013 9:58 pm
by Norbo
The ray cast result contains exactly which object was hit. Add some extra game-specific data to its Tag property to link it up, or have some dictionary linking physical objects to their gameplay owners (or anything else which establishes the relationship).