Page 3 of 4

Re: Charachter controller

Posted: Thu Apr 11, 2013 4:57 pm
by Norbo
This isn't a physics question, so I can't justify spending a lot of time on it, but:
-For a third person view such that the hands need to be positioned in world space, use the camera's rotation. Extracting the yaw and pitch should work; don't invert it, though.
-For a first person view like the hand/weapon models in most FPS games, the hands are just drawn directly into view space. In that case, no extra rotation is required- the hands are fixed relative to the camera.

Re: Charachter controller

Posted: Fri Apr 19, 2013 9:24 pm
by mohammadAdnan
Hello teacher Norbo , I know this is not Physics stuff , but I post my problem to :

1 : Gamedev
2 : stackoverflow
3 : Microsoft XNA
and many others about the rotation of my weapon around with the camera , it's easy to do but they said to me that I can't may be because of the implementation of the Camera I got from BEPU source code or from the character controller ?? Norbo what ! I tried many codes and they all fails to rotate weapon and hands around with the camera :

Code: Select all

        public void Update(GameTime gameTime, KeyboardState KeyboardInputt, 
            KeyboardState PreviousKeyboardInputt, MouseState MouseInput)
        {

            cci.Update((float)gameTime.ElapsedGameTime.TotalSeconds, PreviousKeyboardInputt, KeyboardInputt);

            // this is used to rotate model aorund with the camera 
            Vector3 forward = cci.Camera.WorldMatrix.Forward + new Vector3(200, -5, 0);
            Matrix translatePlayer = Matrix.CreateTranslation(cci.Camera.Position + Vector3.Normalize(forward));
            Matrix scalePlayer = Matrix.CreateScale(0.05f);
            Matrix rotatePlayer = Matrix.Invert(Matrix.CreateFromAxisAngle(cci.Camera.ViewMatrix.Left,
                cci.Camera.Pitch) * Matrix.CreateFromAxisAngle(cci.Camera.ViewMatrix.Up, cci.Camera.Yaw));
            staticModel.Transform = scalePlayer * rotatePlayer * translatePlayer ;
            

            ModelDrawer.Update();

            space.Update();
        }
and here is the camera I got from BEPU :

Code: Select all

    /// <summary>
    /// Simple camera class for moving around the demos area.
    /// </summary>
    public class Camera : DrawableGameComponent
    {
        /// <summary>
        /// Gets or sets the position of the camera.
        /// </summary>
        public Vector3 Position { get; set; }

        public Matrix projection;

        private float yaw;
        private float pitch;
        
        /// <summary>
        /// Gets or sets the yaw rotation of the camera.
        /// </summary>
        public float Yaw
        {
            get { return yaw; }
            set { yaw = MathHelper.WrapAngle(value); }
        }

        /// <summary>
        /// Gets or sets the pitch rotation of the camera.
        /// </summary>
        public float Pitch
        {
            get { return pitch; }
            set
            {
                pitch = value;
                if (pitch > MathHelper.PiOver2 * .99f)
                    pitch = MathHelper.PiOver2 * .99f;
                else if (pitch < -MathHelper.PiOver2 * .99f)
                    pitch = -MathHelper.PiOver2 * .99f;
            }
        }

        /// <summary>
        /// Gets or sets the speed at which the camera moves.
        /// </summary>
        public float Speed { get; set; }

        /// <summary>
        /// Gets the view matrix of the camera.
        /// </summary>
        public Matrix ViewMatrix { get; private set; }

        /// <summary>
        /// Gets or sets the projection matrix of the camera.
        /// </summary>
        public Matrix ProjectionMatrix { get; set; }

        /// <summary>
        /// Gets the world transformation of the camera.
        /// </summary>
        public Matrix WorldMatrix { get; private set; }

        /// <summary>
        /// Whether or not to use the default free-flight camera controls.
        /// Set to false when using vehicles or character controllers.
        /// </summary>
        public bool UseMovementControls = true;

        /// <summary>
        /// Creates a camera.
        /// </summary>
        /// <param name="pos">Initial position of the camera.</param>
        /// <param name="s">Speed of the camera per second.</param>
        /// <param name="p">Initial pitch angle of the camera.</param>
        /// <param name="y">Initial yaw value of the camera.</param>
        public Camera(Game game, Vector3 pos, float s, float p, float y) : base (game)
        {

            float aspectRatio = (float)Game.Window.ClientBounds.Width /
                                (float)Game.Window.ClientBounds.Height;
            // 45 degrees
            float fieldOfView = MathHelper.PiOver4;

            projection = Matrix.CreatePerspectiveFieldOfView(fieldOfView, aspectRatio, 0.1f, 7000);

            Position = pos;
            Speed = s;
            Yaw = y;
            Pitch = p;
            ProjectionMatrix = projection;

            //rayCastFilter = RayCastFilter;
        }

        /// <summary>
        /// Moves the camera forward.
        /// </summary>
        /// <param name="distance">Distance to move.</param>
        public void MoveForward(float distance)
        {
            Position += WorldMatrix.Forward * distance;
        }

        /// <summary>
        /// Moves the camera to the right.
        /// </summary>
        /// <param name="distance">Distance to move.</param>
        public void MoveRight(float distance)
        {
            Position += WorldMatrix.Right * distance;
        }

        /// <summary>
        /// Moves the camera up.
        /// </summary>
        /// <param name="distance">Distanec to move.</param>
        public void MoveUp(float distance)
        {
            Position += new Vector3(0, distance, 0);
        }


        /// <summary>
        /// Updates the state of the camera.
        /// </summary>
        /// <param name="dt">Time since the last frame in seconds.</param>
        /// <param name="keyboardInput">Input for this frame from the keyboard.</param>
        /// <param name="oldMouseInput">Input from the previous frame from the mouse.</param>
        /// <param name="mouseInput">Input for this frame from the mouse.</param>

        public void Update(float dt, KeyboardState keyboardInput, MouseState mouseInput)
        {
            Yaw += (200 - mouseInput.X) * dt * .32f;
            Pitch += (200 - mouseInput.Y) * dt * .32f;


            WorldMatrix = Matrix.CreateFromAxisAngle(Vector3.Right, Pitch) * Matrix.CreateFromAxisAngle(Vector3.Up, Yaw);

            WorldMatrix = WorldMatrix * Matrix.CreateTranslation(Position);
            ViewMatrix = Matrix.Invert(WorldMatrix);
        }
    }
Note : cci is the instance from character controller class , so Norbo what is your opinion ?

Re: Charachter controller

Posted: Fri Apr 19, 2013 10:17 pm
by Norbo
I can't provide a complete answer because I'm not confident I know exactly what you want.

However, I will say that this section includes things which probably are not what you actually want:
Vector3 forward = cci.Camera.WorldMatrix.Forward + new Vector3(200, -5, 0);
Matrix translatePlayer = Matrix.CreateTranslation(cci.Camera.Position + Vector3.Normalize(forward));
Matrix scalePlayer = Matrix.CreateScale(0.05f);
Matrix rotatePlayer = Matrix.Invert(Matrix.CreateFromAxisAngle(cci.Camera.ViewMatrix.Left,
cci.Camera.Pitch) * Matrix.CreateFromAxisAngle(cci.Camera.ViewMatrix.Up, cci.Camera.Yaw));
1) Offsetting the forward direction (which is unit length) by 200 along x and then normalizing it will result in a direction which is pointing almost exactly along the X axis regardless of what the forward direction was originally.

2) You should not need to invert anything to get the rotation right because you already have the Camera.WorldMatrix. That Matrix.Invert call should be removed.

3) Using an axis from the Camera.ViewMatrix is almost certainly not useful. The Camera.ViewMatrix is the inverse of the Camera.WorldMatrix. If you want the 'left' direction of the camera, you want the Camera.WorldMatrix.Left. The same goes for the ViewMatrix.Up usage later.

4) I suspect you do not actually want to use the "Up" direction from any matrix at all for the yaw. Instead, you probably intend to rotate around the Vector3.Up, which is just (0,1,0). To see why, imagine if the Camera.WorldMatrix.Forward is pointing nearly upwards or downwards. In this case, the Camera.WorldMatrix.Up will actually be pointing horizontally. Trying to rotate around that will swing your transform around in a pretty unintuitive way.

I would recommend you slow down a little bit and build the logic deliberately, piece by piece, making sure you understand exactly what it does before adding the next piece of the transform. Trial and error does not work well here; there are too many degrees of freedom.

Re: Charachter controller

Posted: Sat Apr 20, 2013 1:54 pm
by mohammadAdnan
Thank you Norbo !! many game developers did not solve it and you did it just in few steps ! yeah it works fine and the weapon rotate well around while the camera rotate , I did what you said to me and offset the position of Weapon in Blender and set to to (0,0,0 ) and flip the weapon in the opposite axis and works fine :) thanks Norbo i will post it for them Norbo thank you again :

I tried to Invert the matrix , but it behave strangely as it was before ! , So Norbo what do you think ?

Code: Select all

            Vector3 forward = cci.Camera.WorldMatrix.Forward + new Vector3(0, -5, 0);
            Matrix translatePlayer = Matrix.CreateTranslation(cci.Camera.WorldMatrix.Translation+ forward);
            Matrix scalePlayer = Matrix.CreateScale(0.05f);
            Matrix rotatePlayer = Matrix.CreateFromAxisAngle(cci.Camera.WorldMatrix.Left,
                cci.Camera.Pitch) * Matrix.CreateFromAxisAngle(Vector3.Up, cci.Camera.Yaw);
            staticModel.Transform = scalePlayer * rotatePlayer * translatePlayer ;     
But the last issue is related to rotation around pitch axis it rotate well and not well !! ,i.e it rotates at the first time I run the game then it rotates strangely after I make rotation around y axis and then by x-axis, see the photos :

1 : Here is when I run the game
Image

2 : After I make rotation around the y-axis for example 130 degree then I make rotation around the pitch axis by 30 degree see what happens :
Image

Re: Charachter controller

Posted: Sat Apr 20, 2013 5:48 pm
by Norbo
All I can really offer is the general advice I mentioned before:
I would recommend you slow down a little bit and build the logic deliberately, piece by piece, making sure you understand exactly what it does before adding the next piece of the transform. Trial and error does not work well here; there are too many degrees of freedom.

Re: Charachter controller

Posted: Sat Apr 20, 2013 6:32 pm
by mohammadAdnan
Thank you :) you helps a lot thanks I will try and post any question related to character controller later :) .

Re: Charachter controller

Posted: Sun Apr 21, 2013 12:42 pm
by mohammadAdnan
Norbo I done it :) , anyone who want to get used of Norbo-code here it's full implementation :

Code: Select all

            Vector3 forward = cci.Camera.WorldMatrix.Forward + new Vector3(0, -1, 0);

            if (cci.Camera.Pitch <= MathHelper.ToRadians(-40))
            {
                cci.Camera.Pitch += MathHelper.ToRadians(1);
            }
            if (cci.Camera.Pitch >= MathHelper.ToRadians(40))
            {
                cci.Camera.Pitch -= MathHelper.ToRadians(1);
            }

            Matrix translatePlayer = Matrix.CreateTranslation(cci.Camera.WorldMatrix.Translation + forward);
            Matrix scalePlayer = Matrix.CreateScale(0.05f);

            Matrix rotatePlayer = Matrix.CreateRotationX(cci.Camera.Pitch) *
                Matrix.CreateFromAxisAngle(Vector3.Up, cci.Camera.Yaw);
            staticModel.Transform = scalePlayer * rotatePlayer * translatePlayer ;   
Thanks for Norbo very very much ,

Norbo before I done this post , I added enemy models to my Game with multiple animation clips saved as " model.x " , so :
Q : How to shot an entity , enemy ( .x model ) ?
I think for Model.x , I should create convex hull for it and through that convex Hull I check the collision against it from the World , for entity I just add it to space and check for collision , but how to do this using BEPU code ? Is there any source code I can check ? as I don't know much about the implementation of BEPU physics Library !

Re: Charachter controller

Posted: Sun Apr 21, 2013 5:12 pm
by Norbo
The BEPUphysicsDemos should provide sufficient source examples for entity configuration and the other basics. If you want to use ray casts to simulate bullets, then there is a Space.RayCast method you can use. It's used in a few places in the BEPUphysicsDemos too.

If you want to check if an entity is touching another object, you can check the entity.CollisionInformation.Pairs list. If there are any pairs, the bounding box of the two objects overlap. If you want to check if the objects are actually touching, look at the pair.Contacts list; a pair is colliding if there exists at least one contact with nonnegative penetration depth.

If you want to listen for when collisions occur through an event-style interface, you can use collision events..

Re: Charachter controller

Posted: Mon Apr 22, 2013 9:29 pm
by mohammadAdnan
Thanks Norbo , I am about 95 % to end this post :) :

Q1 : I have used used animation library which is "XNA Animation Component Library" to load FBX model with multiple animation files :) and contain "Die" clip so I think I have no need for ragdoll any more right Norbo ??

Q2 : Before I go to the next stage in my project " AI " , how to make my FBX models which contain all the animations clips collide with the world ?? , Norbo may be (correct me pls :) ) I have to go through all meshes in the skinned model and add convex hull for each one ??

Q3 : You said I have to use space.raycast , to use it in shooting from your gun ? okay , can you suggest code how to do it in BEPU ?

Re: Charachter controller

Posted: Mon Apr 22, 2013 9:51 pm
by Norbo
Q1 : I have used used animation library which is "XNA Animation Component Library" to load FBX model with multiple animation files and contain "Die" clip so I think I have no need for ragdoll any more right Norbo ??
Up to you! ;)
Q2 : Before I go to the next stage in my project " AI " , how to make my FBX models which contain all the animations clips collide with the world ?? , Norbo may be (correct me pls ) I have to go through all meshes in the skinned model and add convex hull for each one ??
While walking around, the character's world collision is usually represented only by the character controller body. There are exceptions to this, but it's the most common approach.

Per-bone collision is used in rag dolls and sometimes fine-grained hit detection. In the latter case, you don't really need them to be dynamic objects, they just need to be testable.

If you do end up creating per-bone proxies, the usual way is to construct a simplified version out of primitives (like convex hulls and boxes). These proxies are often designed and positioned relative to the graphical bones in the modeling program.
Q3 : You said I have to use space.raycast , to use it in shooting from your gun ? okay , can you suggest code how to do it in BEPU ?
There's a few examples in the BEPUphysicsDemos source, like the vehicle chase camera. From the Camera source:

Code: Select all

 RayCastResult result;
                if (entityToChase.Space.RayCast(new Ray(lookAt, backwards), distanceToTarget, rayCastFilter, out result))
                {
                    Position = lookAt + (Math.Max(result.HitData.T - chaseCameraMargin, 0)) * backwards; //Put the camera just before any hit spot.
                }...
This shows a usage of the ray cast method along with filter delegate.

Re: Charachter controller

Posted: Mon Apr 22, 2013 10:06 pm
by mohammadAdnan
So , I can use character controller for enemies as I did before right ? instead of creating per-bone collision :D

Re: Charachter controller

Posted: Mon Apr 22, 2013 10:08 pm
by Norbo
Yup- if something is supposed to move like a character, it should probably use a character controller.

Re: Charachter controller

Posted: Thu Apr 25, 2013 10:41 am
by mohammadAdnan
Hello , Norbo for Enemy character collision , I try to do this (I Follow the approach as you said which is using character controller ):

here is the instance of chrachter controller :

Code: Select all

CharacterController enemyChrachterController;
in the Load content method :

Code: Select all

            // Adding Enemy to Physics Space
            enemyChrachterController = new CharacterController();
in the Update method ( I am just in game test , so I am using cci.camera.position for testing purpose , later on I will add AI for the enemy and give it another position so that enemy will attack and follow from that position ...etc ) Norbo here is the code :

Code: Select all

            // Add this to the Update method
            enemyChrachterController.Body.World= cci.Camera.Position + 
                cci.Camera.Position + new Vector3(0, -15, 0) + cci.Camera.WorldMatrix.Forward * 70;

            dwarfAnimator.World = Matrix.CreateScale(5) * rotationModel *
                Matrix.CreateTranslation(cci.Camera.Position + new Vector3(0, -15, 0) +
                cci.Camera.WorldMatrix.Forward * 70);
I try another way to do this in the update method and it's still not working :

Code: Select all

 // Add this to the Update method
            enemyChrachterController.Body.WorldTransform = Matrix.CreateScale(5) * rotationModel *
                Matrix.CreateTranslation(cci.Camera.Position + new Vector3(0, -15, 0) +
                cci.Camera.WorldMatrix.Forward * 70);

            dwarfAnimator.World = enemyChrachterController.Body.WorldTransform;
still not working , here is picture :
Image

Re: Charachter controller

Posted: Thu Apr 25, 2013 6:35 pm
by Norbo
I am not clear on what the goal is or what the specific problem is, but here's a few things which could be going wrong:

1) Is the character controller added to the space? If it is not, then it won't simulate.

2)

Code: Select all

enemyChrachterController.Body.World= cci.Camera.Position + 
                cci.Camera.Position + new Vector3(0, -15, 0) + cci.Camera.WorldMatrix.Forward * 70;
I'm not sure what the World property is here, the Entity has no such property. If you're trying to teleport the entity, use the Position property.

3)

Code: Select all

enemyChrachterController.Body.WorldTransform = Matrix.CreateScale(5) * rotationModel *
                Matrix.CreateTranslation(cci.Camera.Position + new Vector3(0, -15, 0) +
                cci.Camera.WorldMatrix.Forward * 70);
An entity's world transform can only be rigid (that is, composed of a rotation and translation). It cannot hold hold a scaling factor.

Further, you should not rotate the character controller's body; it is designed to have fixed orientation.

Re: Charachter controller

Posted: Fri Apr 26, 2013 4:42 pm
by mohammadAdnan
Norbo , the problem is :

I added (.FBX & .X) models to my game and all I want to do is to make them collide with the world , you told me that I don't need to add convex hull for each model mesh , but instead (the most common approach is add character controller ) all I need is just add character controller for the model , and what I tried is this to make ( my .x model ) colliode with the world :

Norbo , Yes I added it to the space :)

Code: Select all

            // Adding Enemy to Physics Space
            enemyChrachterController = new CharacterController();
            enemyChrachterController.Body.Tag = "noDisplayObject";
            space.Add(enemyChrachterController);
In the Update method , I just offset the character controller transformation so that when the enemy move it moves with it ! ( what is wrong Norbo ?)

Code: Select all

            // Add this to the Update method
            enemyChrachterController.Body.WorldTransform = Matrix.CreateTranslation(cci.Camera.Position + 
                new Vector3(0, -15, 0) + cci.Camera.WorldMatrix.Forward * 70);
            dwarfAnimator.World = Matrix.CreateScale(5) * rotationModel *
                Matrix.CreateTranslation(cci.Camera.Position + new Vector3(0, -15, 0) +
                cci.Camera.WorldMatrix.Forward * 70);
I got this as shown in the picture down :
Image

So Norbo I hope my problem clear for you right now :)