Пример #1
0
        void RestartJacks()
        {
            var ll = new Vector <Node>();  //get rid of some old objects

            scene.GetChildrenWithName(ll, "Sphere", true);
            for (int ii = 0; ii < ll.Size; ii++)
            {
                ll[ii].Remove();
            }

            var nn = new Vector <Node>();

            scene.GetChildrenWithName(nn, "Stuffing", true);
            for (int ii = 0; ii < nn.Size; ii++)
            {
                nn[ii].Remove();
            }

            var mm = new Vector <Node>();

            scene.GetChildrenWithName(mm, "Jack", true);
            for (int ii = 0; ii < mm.Size; ii++)
            {
                mm[ii].Remove();
            }

            // Create animated models, you dont know ... jack
            var cache = GetSubsystem <ResourceCache>();

            for (int z = -1; z <= 1; ++z)
            {
                for (int x = -4; x <= 4; ++x)
                {
                    Node modelNode = scene.CreateChild("Jack");
                    modelNode.Position = new Vector3(x * 5.0f, 0.0f, z * 5.0f);
                    modelNode.Rotation = new Quaternion(0.0f, 180.0f, 0.0f);
                    AnimatedModel modelObject = modelNode.CreateComponent <AnimatedModel>();
                    modelObject.Model = cache.Get <Model>("Models/Jack.mdl");
                    modelObject.SetMaterial(cache.Get <Material>("Materials/Jack.xml"));
                    modelObject.CastShadows = true;
                    // Set the model to also update when invisible to avoid staying invisible when the model should come into
                    // view, but does not as the bounding box is not updated
                    modelObject.UpdateInvisible = true;

                    // Create a rigid body and a collision shape. These will act as a trigger for transforming the
                    // model into a ragdoll when hit by a moving object
                    RigidBody body = modelNode.CreateComponent <RigidBody>();
                    // The Trigger mode makes the rigid body only detect collisions, but impart no forces on the
                    // colliding objects
                    body.Trigger = true;
                    CollisionShape shape = modelNode.CreateComponent <CollisionShape>();
                    // Create the capsule shape with an offset so that it is correctly aligned with the model, which
                    // has its origin at the feet
                    shape.SetCapsule(0.7f, 2.0f, new Vector3(0.0f, 1.0f, 0.0f), Quaternion.Identity);

                    // Create a custom component that reacts to collisions and creates the ragdoll
                    modelNode.AddComponent(new Ragdoll());
                }
            }
        }
Пример #2
0
        void CreateRagdollBone(string boneName, ShapeType type, Vector3 size, Vector3 position, Quaternion rotation)
        {
            // Find the correct child scene node recursively
            Node boneNode = Node.GetChild(boneName, true);

            if (boneNode == null)
            {
                Log.Warn($"Could not find bone {boneName} for creating ragdoll physics components");
                return;
            }

            RigidBody body = boneNode.CreateComponent <RigidBody>();

            // Set mass to make movable
            body.Mass = 1.0f;
            // Set damping parameters to smooth out the motion
            body.LinearDamping  = 0.05f;
            body.AngularDamping = 0.85f;
            // Set rest thresholds to ensure the ragdoll rigid bodies come to rest to not consume CPU endlessly
            body.LinearRestThreshold  = 1.5f;
            body.AngularRestThreshold = 2.5f;

            CollisionShape shape = boneNode.CreateComponent <CollisionShape>();

            // We use either a box or a capsule shape for all of the bones
            if (type == ShapeType.SHAPE_BOX)
            {
                shape.SetBox(size, position, rotation);
            }
            else
            {
                shape.SetCapsule(size.X, size.Y, position, rotation);
            }
        }
Пример #3
0
        public override void Start()
        {
            Context.Input.SetMouseVisible(true);

            // Viewport
            _scene = new Scene(Context);
            _scene.LoadFile("Scenes/SampleScene.xml");
            _scene.CreateComponent <PhysicsWorld>();

            var character = _scene.GetChild("character");

            _body = character.CreateComponent <RigidBody>();
            _body.CollisionLayer = 1u;
            _body.SetKinematic(true);
            _body.SetTrigger(true);
            _body.SetAngularFactor(Vector3.Zero);
            _body.CollisionEventMode = CollisionEventMode.CollisionAlways;

            _shape = character.CreateComponent <CollisionShape>();
            _shape.SetCapsule(0.7f, 1.8f, new Vector3(0.0f, 0.9f, 0.0f));

            _characterController = character.CreateComponent <KinematicCharacterController>();

            _camera          = _scene.GetChild("Main Camera", true);
            _viewport        = new Viewport(Context);
            _viewport.Scene  = _scene;
            _viewport.Camera = _camera.GetComponent <Camera>();
            //var renderPath = _viewport.RenderPath;
            //renderPath.Append(Context.Cache.GetResource<XMLFile>("PostProcess/GammaCorrection.xml"));
            Context.Renderer.SetViewport(0, _viewport);
            _camera.Parent.CreateComponent <RotateObject>();

            SubscribeToEvent(E.Update, args =>
            {
                var timestep = args[E.Update.TimeStep].Float;
                Debug.Assert(this != null);

                if (ImGui.Begin("Urho3D.NET"))
                {
                    ImGui.TextColored(Color.Red, $"Ftm by luyssport\nLicense: CC Attribution - NonCommercial - ShareAlike\nhttps://sketchfab.com/3d-models/ftm-0970f30574d047b1976ba0aa6f2ef855\nFrame time: {timestep}");
                    if (ImGui.Button("Exit"))
                    {
                        Context.Engine.Exit();
                    }
                }
                ImGui.End();

                if (Context.Input.GetKeyDown(Key.KeyEscape))
                {
                    Context.Engine.Exit();
                }
            });
        }
Пример #4
0
        void CreateCharacter()
        {
            var cache = ResourceCache;

            Node objectNode = scene.CreateChild("Jack");

            objectNode.Position = (new Vector3(0.0f, 1.0f, 0.0f));

            // spin node
            Node adjustNode = objectNode.CreateChild("AdjNode");

            adjustNode.Rotation = (new  Quaternion(0, 180, 0));

            // Create the rendering component + animation controller
            AnimatedModel obj = adjustNode.CreateComponent <AnimatedModel>();

            obj.Model = cache.GetModel("Models/Mutant/Mutant.mdl");
            obj.SetMaterial(cache.GetMaterial("Models/Mutant/Materials/mutant_M.xml"));
            obj.CastShadows = true;
            adjustNode.CreateComponent <AnimationController>();

            // Set the head bone for manual control
            obj.Skeleton.GetBoneSafe("Mutant:Head").Animated = false;

            // Create rigidbody, and set non-zero mass so that the body becomes dynamic
            RigidBody body = objectNode.CreateComponent <RigidBody>();

            body.CollisionLayer = 1;
            body.Mass           = 1.0f;

            // Set zero angular factor so that physics doesn't turn the character on its own.
            // Instead we will control the character yaw manually
            body.SetAngularFactor(Vector3.Zero);

            // Set the rigidbody to signal collision also when in rest, so that we get ground collisions properly
            body.CollisionEventMode = CollisionEventMode.Always;

            // Set a capsule shape for collision
            CollisionShape shape = objectNode.CreateComponent <CollisionShape>();

            shape.SetCapsule(0.7f, 1.8f, new Vector3(0.0f, 0.9f, 0.0f), Quaternion.Identity);

            // Create the character logic component, which takes care of steering the rigidbody
            // Remember it so that we can set the controls. Use a WeakPtr because the scene hierarchy already owns it
            // and keeps it alive as long as it's not removed from the hierarchy
            character = new Character();
            objectNode.AddComponent(character);
        }
Пример #5
0
 public override void SetTo(CollisionShape shapeComponent)
 {
     shapeComponent.SetCapsule(diameter, height, position, rotation);
 }
Пример #6
0
        void CreateScene()
        {
            var cache = GetSubsystem <ResourceCache>();

            scene = new Scene();

            // Create octree, use default volume (-1000, -1000, -1000) to (1000, 1000, 1000)
            // Create a physics simulation world with default parameters, which will update at 60fps. Like the Octree must
            // exist before creating drawable components, the PhysicsWorld must exist before creating physics components.
            // Finally, create a DebugRenderer component so that we can draw physics debug geometry
            scene.CreateComponent <Octree>();
            scene.CreateComponent <PhysicsWorld>();
            scene.CreateComponent <DebugRenderer>();

            // Create a Zone component for ambient lighting & fog control
            Node zoneNode = scene.CreateChild("Zone");
            Zone zone     = zoneNode.CreateComponent <Zone>();

            zone.SetBoundingBox(new BoundingBox(-1000.0f, 1000.0f));
            zone.AmbientColor = (new Color(0.15f, 0.15f, 0.15f));
            zone.FogColor     = new Color(0.5f, 0.5f, 0.7f);
            zone.FogStart     = 100.0f;
            zone.FogEnd       = 300.0f;

            // Create a directional light to the world. Enable cascaded shadows on it
            Node lightNode = scene.CreateChild("DirectionalLight");

            lightNode.SetDirection(new Vector3(0.6f, -1.0f, 0.8f));
            Light light = lightNode.CreateComponent <Light>();

            light.LightType   = LightType.LIGHT_DIRECTIONAL;
            light.CastShadows = true;
            light.ShadowBias  = new BiasParameters(0.00025f, 0.5f);
            // Set cascade splits at 10, 50 and 200 world units, fade shadows out at 80% of maximum shadow distance
            light.ShadowCascade = new CascadeParameters(10.0f, 50.0f, 200.0f, 0.0f, 0.8f);

            {
                // Create a floor object, 500 x 500 world units. Adjust position so that the ground is at zero Y
                Node floorNode = scene.CreateChild("Floor");
                floorNode.Position = new Vector3(0.0f, -0.5f, 0.0f);
                floorNode.Scale    = new Vector3(500.0f, 1.0f, 500.0f);
                StaticModel floorObject = floorNode.CreateComponent <StaticModel>();
                floorObject.Model = cache.Get <Model>("Models/Box.mdl");
                floorObject.SetMaterial(cache.Get <Material>("Materials/StoneTiled.xml"));

                // Make the floor physical by adding RigidBody and CollisionShape components
                RigidBody body = floorNode.CreateComponent <RigidBody>();
                // We will be spawning spherical objects in this sample. The ground also needs non-zero rolling friction so that
                // the spheres will eventually come to rest
                body.RollingFriction = 0.15f;
                CollisionShape shape = floorNode.CreateComponent <CollisionShape>();
                // Set a box shape of size 1 x 1 x 1 for collision. The shape will be scaled with the scene node scale, so the
                // rendering and physics representation sizes should match (the box model is also 1 x 1 x 1.)
                shape.SetBox(Vector3.One, Vector3.Zero, Quaternion.Identity);
            }

            // Create animated models
            for (int z = -1; z <= 1; ++z)
            {
                for (int x = -4; x <= 4; ++x)
                {
                    Node modelNode = scene.CreateChild("Jack");
                    modelNode.Position = new Vector3(x * 5.0f, 0.0f, z * 5.0f);
                    modelNode.Rotation = new Quaternion(0.0f, 180.0f, 0.0f);
                    AnimatedModel modelObject = modelNode.CreateComponent <AnimatedModel>();
                    modelObject.Model = cache.Get <Model>("Models/Jack.mdl");
                    modelObject.SetMaterial(cache.Get <Material>("Materials/Jack.xml"));
                    modelObject.CastShadows = true;
                    // Set the model to also update when invisible to avoid staying invisible when the model should come into
                    // view, but does not as the bounding box is not updated
                    modelObject.UpdateInvisible = true;

                    // Create a rigid body and a collision shape. These will act as a trigger for transforming the
                    // model into a ragdoll when hit by a moving object
                    RigidBody body = modelNode.CreateComponent <RigidBody>();
                    // The Trigger mode makes the rigid body only detect collisions, but impart no forces on the
                    // colliding objects
                    body.Trigger = true;
                    CollisionShape shape = modelNode.CreateComponent <CollisionShape>();
                    // Create the capsule shape with an offset so that it is correctly aligned with the model, which
                    // has its origin at the feet
                    shape.SetCapsule(0.7f, 2.0f, new Vector3(0.0f, 1.0f, 0.0f), Quaternion.Identity);

                    // Create a custom component that reacts to collisions and creates the ragdoll
                    modelNode.AddComponent(new Ragdoll());
                }
            }

            // Create the camera. Limit far clip distance to match the fog
            CameraNode     = new Node();
            camera         = CameraNode.CreateComponent <Camera>();
            camera.FarClip = 300.0f;
            // Set an initial position for the camera scene node above the plane
            CameraNode.Position = new Vector3(0.0f, 3.0f, -20.0f);
        }
Пример #7
0
    void Start()
    {
        SubscribeToEvent <KeyDownEvent>(e =>
        {
            if (e.Key == Constants.KEY_ESCAPE)
            {
                GetSubsystem <Engine>().Exit();
            }
        });

        Node objectNode = Scene.CreateChild("AtomicMutant");

        objectNode.Scale    = new Vector3(1.5f, 1.5f, 1.5f);
        objectNode.Position = Node.Position;

        // spin node
        Node adjustNode = objectNode.CreateChild("AdjNode");

        adjustNode.Rotation = Quaternion.FromAxisAngle(new Vector3(0, 1, 0), 180);

        // Create the rendering component + animation controller
        AnimatedModel animatedModel = adjustNode.CreateComponent <AnimatedModel>();

        var cache = GetSubsystem <ResourceCache>();

        animatedModel.Model       = cache.GetResource <Model>("Models/Mutant/Mutant.mdl");
        animatedModel.Material    = cache.GetResource <Material>("Models/Mutant/Materials/mutant_M.material");
        animatedModel.CastShadows = true;

        // Create animation controller
        adjustNode.CreateComponent <AnimationController>();

        // Create rigidbody, and set non-zero mass so that the body becomes dynamic
        RigidBody body = objectNode.CreateComponent <RigidBody>();

        body.CollisionLayer = 1;
        body.Mass           = 1.0f;

        // Set zero angular factor so that physics doesn't turn the character on its own.
        // Instead we will control the character yaw manually
        body.AngularFactor = Vector3.Zero;

        // Set the rigidbody to signal collision also when in rest, so that we get ground collisions properly
        body.CollisionEventMode = CollisionEventMode.COLLISION_ALWAYS;

        // Set a capsule shape for collision
        CollisionShape shape = objectNode.CreateComponent <CollisionShape>();

        shape.SetCapsule(1.5f, 1.8f, new Vector3(0.0f, 0.9f, 0.0f));

        character = objectNode.CreateComponent <Character>();


        AtomicNET.GetSubsystem <Renderer>().ShadowMapSize = 2048;

        // Get Camera from Scene
        Vector <Node> children = new Vector <Node>();

        Scene.GetChildrenWithComponent <Camera>(children, true);
        if (children.Size > 0)
        {
            cameraNode = children[0];
        }
    }