private void Walk(float elapsedTime)
        {
            _walk.Update(elapsedTime);
            UpdateRotation(elapsedTime);

            if (!_update)
            {
                _lastAnimPosition = _character.LocalPosition;
                _update           = true;
                return;
            }

            var cursor = _walk.FirstPlayback.Cursor;

            if (!cursor.TimeRestart)
            {
                _displacement = _character.LocalPosition - _lastAnimPosition;
            }
            else
            {
                _displacement = _character.LocalPosition - _initialForwardPositon;
            }

            _displacement = Vector3.TransformCoordinates(_displacement, Matrix.RotationY(_heading));
            _displacement = WalkAroundCamera(_displacement);

            _lastAnimPosition = _character.LocalPosition;
        }
 private void Idle(float deltaT)
 {
     _idle.Update(deltaT);
 }
예제 #3
0
        public void Automata_Idle_Walk()
        {
            SceneTests.InitializeScene();

            const float startTimeWalk = 34f / 30f;
            const float endTimeWalk   = 63f / 30f;
            const float durationWalk  = endTimeWalk - startTimeWalk;
            const float startTimeIdle = 0;
            const float endTimeIdle   = 0;
            const float durationIdle  = endTimeIdle;
            const float blendDuration = 0.25f;

            Vector3 speedVector = new Vector3(0, 0, -1f);

            ContentImporter.Import(SceneManager.Scene, @"C:\Users\ansel\Documents\3dsmax\export\lighting_shadowed.DAE")
            .OnSceneAttach(SceneManager.Scene);
            Frame cameraNode = SceneManager.Scene.FindNode("camera1");

            Frame         root     = SceneManager.Scene.EnumerateNodesPosOrden().First(x => x.Type == FrameType.Bone);
            BonesResetter resetter = new BonesResetter(root);

            Vector3 translation = root.LocalPosition;
            var     iniHeading  = root.Heading;

            var animation = SceneManager.Scene.AnimManager.Animations[0];

            animation.Sample(startTimeWalk);
            var iniKeyValue = root.LocalPosition;

            animation.Sample(endTimeWalk);
            var lastKeyValue = root.LocalPosition;

            Vector3 lastAnimTrans = root.LocalPosition; //new Vector3();
            bool    update        = false;
            float   deltaH        = 0;

            KeyFrameAnimationPlayback idle     = new KeyFrameAnimationPlayback(animation, startTimeIdle, durationIdle, AnimationLooping.Loop);
            KeyFrameAnimationPlayback walk     = new KeyFrameAnimationPlayback(animation, startTimeWalk, durationWalk, AnimationLooping.Loop);
            AnimationTransition       idleWalk = new AnimationTransition(idle, walk, blendDuration);
            AnimationTransition       walkIdle = new AnimationTransition(walk, idle, blendDuration);

            var walkCursor = walk.GetCursor(animation);

            Action <float> updateAction = (deltaT) =>
            {
                walk.Update(deltaT);

                if (!update)
                {
                    lastAnimTrans = root.LocalPosition;
                    update        = true;
                    return;
                }

                var cursor = walkCursor;
                if (!cursor.TimeRestart)
                {
                    _disp = root.LocalPosition - lastAnimTrans;
                }
                else
                {
                    _disp = root.LocalPosition - (cursor.PlayDirection > 0 ? iniKeyValue : lastKeyValue);
                }

                _disp = Vector3.TransformCoordinates(_disp, Matrix.RotationY(deltaH));

                translation  += _disp;
                lastAnimTrans = root.LocalPosition;
            };

            Automata stateMachine = new Automata()
                                    .AddState("idle", x =>
                                              idle.Update(x))
                                    .AddState("idle-walk", dt =>
            {
                idleWalk.Update(dt);
                _disp = new Vector3();
                if (Engine.KeyBoard.IsKeyPressed(Keys.Uparrow))
                {
                    _disp = speedVector;
                }
                else
                {
                    _disp = -speedVector;
                }
                _disp = Vector3.TransformCoordinates(_disp, Matrix.RotationY(deltaH));
            })
                                    .AddState("walk", updateAction)
                                    .AddState("walk-idle", x =>
                                              walkIdle.Update(x))
                                    .AddTransition("idle", "idle-walk", x =>
            {
                if (Engine.KeyBoard.IsKeyPressed(Keys.Uparrow))
                {
                    walk.FirstPlayback.Cursor.PlayDirection = 1;
                    update = false;
                    return(true);
                }
                else if (Engine.KeyBoard.IsKeyPressed(Keys.Downarrow))
                {
                    walk.FirstPlayback.Cursor.PlayDirection = -1;
                    update = false;
                    return(true);
                }
                return(false);
            })
                                    .AddTransition("idle-walk", "walk", x =>
                                                   idleWalk.TransitionComplete)
                                    .AddTransition("walk", "walk-idle", x =>
                                                   !Engine.KeyBoard.IsKeyPressed(Keys.Uparrow) && !Engine.KeyBoard.IsKeyPressed(Keys.Downarrow))
                                    .AddTransition("walk-idle", "idle", x =>
                                                   walkIdle.TransitionComplete);

            SceneManager.Scene.Dynamics.Add(new Dynamic(deltaT =>
            {
                //store position previus animation
                var localPosition = root.LocalPosition;
                _disp             = new Vector3();

                float rotSpeed = Numerics.ToRadians(90f);

                if (Engine.KeyBoard.IsKeyPressed(Keys.Leftarrow))
                {
                    deltaH -= rotSpeed * deltaT;
                }
                else if (Engine.KeyBoard.IsKeyPressed(Keys.Rightarrow))
                {
                    deltaH += rotSpeed * deltaT;
                }

                resetter.Reset();
                stateMachine.Update(deltaT);

                //root.Tx = translation.X;
                //root.Tz = translation.Z;
                root.X       = localPosition.X + _disp.X;
                root.Z       = localPosition.Z + _disp.Z;
                root.Heading = iniHeading + deltaH;

                root.ComputeLocalPose();
                root.CommitChanges();

                //var displacement = root.GlobalPose.Translation - localPosition;
                //cameraNode.Tx += displacement.X;
                //cameraNode.Tz += displacement.Z;
                cameraNode.X += _disp.X;
                cameraNode.Z += _disp.Z;
                cameraNode.CommitChanges();
            }));
        }