Exemplo n.º 1
0
        private static CompoundShape create_shapes(RepeatedField <Collider> colliders)
        {
            if (colliders == null || colliders.Count == 0)
            {
                throw new Exception("Colliders is null or count is zero");
            }

            var shape_buffer = new List <TransformedShape>();

            foreach (var collider in colliders)
            {
                Shape      shape = null;
                Vector3    center;
                Quaternion rotation;

                switch (collider.ShapeCase)
                {
                case Collider.ShapeOneofCase.None:
                    throw new Exception("Collider without any shape");

                case Collider.ShapeOneofCase.Box:
                    var box = collider.Box;
                    shape    = new Jitter.Collision.Shapes.BoxShape(box.Length, box.Height, box.Width);
                    center   = box.Center;
                    rotation = box.Rotation;
                    break;

                case Collider.ShapeOneofCase.Sphere:
                    var sphere = collider.Sphere;
                    shape    = new Jitter.Collision.Shapes.SphereShape(sphere.Radius);
                    center   = sphere.Center;
                    rotation = sphere.Rotation;
                    break;

                case Collider.ShapeOneofCase.Capsule:
                    var capsule = collider.Capsule;
                    shape    = new Jitter.Collision.Shapes.CapsuleShape(capsule.Height, capsule.Radius);
                    center   = capsule.Center;
                    rotation = capsule.Rotation;
                    break;

                default:
                    Debug.Assert(false, "Unhandled enum value " + collider.ShapeCase);
                    throw new Exception();
                }

                var center_j          = to_j(center);
                var rotation_j        = to_j(rotation);
                var transformed_shape = new TransformedShape(shape, JMatrix.Identity, center_j);
                shape_buffer.Add(transformed_shape);

                if (collider.Children != null && collider.Children.Count > 0)
                {
                    create_shapes(collider.Children);
                }
            }

            return(new CompoundShape(shape_buffer));
        }
Exemplo n.º 2
0
        public CharacterController CreateCharacterController(float length, float radius)
        {
            var shape = new CapsuleShape(length, radius);
            var rigidBody = CreateRigidBody(shape, false);
            rigidBody.EnableDebugDraw = false;

            PhysicsWorld.AddBody(rigidBody);

            var controller = new CharacterController(PhysicsWorld, rigidBody);
            rigidBody.Tag = controller;

            Bodies.Add(controller);

            return controller;
        }
Exemplo n.º 3
0
        public Player(Engine _engine, Tracker _tracker, Vector3 position, Model _model)
            : base(_engine, _tracker, false)
        {
            model = _model;
            //Create body and add to physics engine
            Shape capsuleShape = new CapsuleShape(1, 0.5f);
            body = new RigidBody(capsuleShape);
            body.Mass = 2f;
            body.Position = position.ToJitterVector();
            body.AllowDeactivation = false;
            body.Tag = this;
            EnableInterfaceCalls = true;

            //TODO: Fix so that players can rotate around Y-axis.
            //Players can't tip over
            Constraint upright = new Jitter.Dynamics.Constraints.SingleBody.FixedAngle(body);
            Engine.Physics.AddConstraint(upright);

            //TEMPORARY FOR VIDYAJUEGOS
            currentWeapon = new Pistol1(_engine, Tracker, this, "", 1, 1000);
        }
Exemplo n.º 4
0
 void IConstructable.Construct(IDictionary<string, string> param)
 {
     var type = param["type"];
     switch (type)
     {
         case "trimesh":
             var physData = ResourceFactory.LoadAsset<PhysicsData>(param["physData"]);
             Shape shape = new TriangleMeshShape(physData.Octree);
             Body = new RigidBody(shape) { Material = { Restitution = 0f, KineticFriction = 0f } };
             break;
         case "hull":
             physData = ResourceFactory.LoadAsset<PhysicsData>(param["physData"]);
             shape = new ConvexHullShape(physData.Vertices);
             Body = new RigidBody(shape);
             break;
         case "sphere":
             shape = new SphereShape(float.Parse(param["radius"], CultureInfo.InvariantCulture));
             Body = new RigidBody(shape);
             break;
         case "box":
             var d = param["size"].ConvertToVector();
             var offset = param.Get("offset", "0;0;0").ConvertToVector();
             shape = new BoxShape(2.0f * d.ToJVector());
             Body = new RigidBody(shape) { Position = offset.ToJVector() };
             break;
         case "capsule":
             var height = float.Parse(param["height"], CultureInfo.InvariantCulture);
             var radius = float.Parse(param["radius"], CultureInfo.InvariantCulture);
             shape = new CapsuleShape(height, radius);
             Body = new RigidBody(shape)
             {
                 Position = JVector.Backward * (0.5f * height + radius),
                 Orientation = JMatrix.CreateRotationX(MathHelper.PiOver2)
             };
             break;
         default:
             throw new Exception("Unknown shape: " + type);
     }
     Body.IsStatic = Convert.ToBoolean(param.Get("static", "false"));
     Body.Material.KineticFriction = 0.5f;
     Body.Material.StaticFriction = 0.5f;
     Body.Material.Restitution = 0.5f;
 }