Character Controller Help!!!

Aug 11, 2009 at 4:54 AM

I'm attempting to combine the jiglibx physics library with my own. I've attempted to implement the character controller sample in my CharacterPhysics class. The problem is that If I change the desired velocity or attempt to jump it doesn't change. Can anyone tell me why?

-------------
//CharacterPhysics.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using JigLibX.Collision;
using JigLibX.Geometry;
using JigLibX.Math;
using JigLibX.Physics;
using JigLibX.Utils;
using JigLibX.Vehicles;

using PG53GameLib.Physics.CharacterControl;

using PG53GameLib.Models;

namespace PG53GameLib.Physics.Type
{
    public class CharacterPhysics : PhysicsType
    {
        private Character _char;
        public Character Char
        {
            get
            {
                return _char;
            }
        }

        private new Player _body;

        private float _mass;
        public float Mass
        {
            get
            {
                return _mass;
            }
            set
            {
                _mass = value;
            }
        }

        public Player CharacterBody { get; set; }

        public CharacterPhysics(Character c, float mass, Game g) : base(g)
        {
            _body = new Player();
            _skin = new CollisionSkin(_body);

            Capsule capsule = new Capsule(Vector3.Zero, Matrix.CreateRotationX(MathHelper.PiOver2), 1.0f, 1.0f);
            addPrimitive(capsule);
            
            _body.CollisionSkin = this._skin;

            _char = c;
            setPosition(c.getPosition());
            _mass = mass;

            _body.SetBodyInvInertia(0.0f, 0.0f, 0.0f);

            CharacterBody = _body as Player;

            _body.AllowFreezing = false;
            _body.EnableBody();

            setPosition(c.getPosition());
        }

        public void addPrimitive(Primitive p)
        {
            _skin.AddPrimitive(p, (int)MaterialTable.MaterialID.NotBouncyNormal);
        }

        public override Matrix getWorldMatrix()
        {
            return _skin.GetPrimitiveLocal(0).Transform.Orientation * _body.Orientation * Matrix.CreateTranslation(_body.Position);
            //return _skin.GetPrimitiveLocal(0).Transform.Orientation * CharacterBody.Orientation * Matrix.CreateTranslation(CharacterBody.Position);
        }

        public override void setPosition(Vector3 p)
        {
            _body.MoveTo(p, _body.Transform.Orientation);
            Vector3 com = SetMass(Mass);
            _skin.ApplyLocalTransform(new Transform(-com, _body.Transform.Orientation));
        }

        public override void setRotation(Vector3 r)
        {
            Matrix orientation = Matrix.CreateRotationX(MathHelper.ToRadians(r.Y)) * Matrix.CreateRotationY(MathHelper.ToRadians(r.X)) * Matrix.CreateRotationZ(MathHelper.ToRadians(r.Z));

            _body.MoveTo(_body.Position, orientation);
            Vector3 com = SetMass(Mass);
            _skin.ApplyLocalTransform(new Transform(-com, orientation));
        }

        protected Vector3 SetMass(float mass)
        {
            PrimitiveProperties primitiveProperties = new PrimitiveProperties(
                PrimitiveProperties.MassDistributionEnum.Solid,
                PrimitiveProperties.MassTypeEnum.Mass, mass);

            float junk;
            Vector3 com;
            Matrix it;
            Matrix itCoM;

            Skin.GetMassProperties(primitiveProperties, out junk, out com, out it, out itCoM);

            _body.BodyInertia = itCoM;//Body
            _body.Mass = junk;

            return com;
        }

        public void Move(Vector3 m)
        {
            Vector3 moveVector = new Vector3();
            float amountOfMovement = 10.0f;

            Matrix rot = Matrix.CreateRotationX(MathHelper.ToRadians(_char.getRotation().Y)) * Matrix.CreateRotationY(MathHelper.ToRadians(_char.getRotation().X));

            moveVector = Vector3.Transform(moveVector, rot);

            JiggleMath.NormalizeSafe(ref moveVector);
            moveVector *= amountOfMovement;

            CharacterBody.DesiredVelocity = moveVector;
            //_body.DesiredVelocity = moveVector;
        }

        public void Jump()
        {
            CharacterBody.DoJump();
        }
    }
}
-------------
//ASkinPredicate.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using JigLibX.Collision;
using JigLibX.Geometry;
using JigLibX.Math;
using JigLibX.Physics;
using JigLibX.Utils;
using JigLibX.Vehicles;

using PG53GameLib.Models;

namespace PG53GameLib.Physics.CharacterControl
{
    class ASkinPredicate : CollisionSkinPredicate1
    {
        public override bool ConsiderSkin(CollisionSkin skin0)
        {
            if (!(skin0.Owner is Player))
                return true;
            else
                return false;
        }
    }
}
------------
//Player.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using JigLibX.Collision;
using JigLibX.Geometry;
using JigLibX.Math;
using JigLibX.Physics;
using JigLibX.Utils;
using JigLibX.Vehicles;

using PG53GameLib.Models;

namespace PG53GameLib.Physics.CharacterControl
{
    public class Player : Body
    {

        public Player()
            : base()
        {
        }

        public Vector3 DesiredVelocity { get; set; }

        private bool doJump = false;

        public void DoJump()
        {
            doJump = true;
        }


        public override void AddExternalForces(float dt)
        {
            ClearForces();

            if (doJump)
            {
                foreach (CollisionInfo info in CollisionSkin.Collisions)
                {
                    Vector3 N = info.DirToBody0;
                    if (this == info.SkinInfo.Skin1.Owner)
                        Vector3.Negate(ref N, out N);

                    if (Vector3.Dot(N, Orientation.Up) > 0.7f)
                    {
                        Vector3 vel = Velocity; vel.Y = 5.0f;
                        Velocity = vel;
                        break;
                    }
                }
            }

            Vector3 deltaVel = DesiredVelocity - Velocity;

            bool running = true;

            if (DesiredVelocity.LengthSquared() < JiggleMath.Epsilon) running = false;
            else deltaVel.Normalize();

            deltaVel.Y = 0.0f;

            // start fast, slow down slower
            if (running) deltaVel *= 10.0f;
            else deltaVel *= 2.0f;

            float forceFactor = 1000.0f;

            AddBodyForce(deltaVel * Mass * dt * forceFactor);

            doJump = false;
            AddGravityToExternalForce();
        }
    }
}