1 //
2 // Copyright (c) 2008-2017 the Urho3D project.
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 //
22 
23 #pragma once
24 
25 #include "Sample.h"
26 
27 namespace Urho3D
28 {
29 class AnimationController;
30 class Node;
31 class IKEffector;
32 class IKSolver;
33 class Scene;
34 }
35 
36 /// Inverse Kinematics demo.
37 /// This sample demonstrates how to adjust the position of animated feet so they match the ground's angle using IK
38 class InverseKinematics : public Sample
39 {
40     URHO3D_OBJECT(InverseKinematics, Sample);
41 
42 public:
43     /// Construct.
44     InverseKinematics(Context* context);
45 
46     /// Setup after engine initialization and before running the main loop.
47     virtual void Start();
48 
49 protected:
50     /// Animation controller of Jack.
51     SharedPtr<Urho3D::AnimationController> jackAnimCtrl_;
52     /// Inverse kinematic effectors and solver
53     SharedPtr<Urho3D::IKEffector> leftEffector_;
54     SharedPtr<Urho3D::IKEffector> rightEffector_;
55     SharedPtr<Urho3D::IKSolver> solver_;
56     /// Need references to these nodes to calculate foot angles and offsets
57     SharedPtr<Urho3D::Node> leftFoot_;
58     SharedPtr<Urho3D::Node> rightFoot_;
59     SharedPtr<Urho3D::Node> jackNode_;
60     /// So we can rotate the floor
61     SharedPtr<Urho3D::Node> floorNode_;
62     float floorPitch_;
63     float floorRoll_;
64     /// Whether or not to draw debug geometry
65     bool drawDebug_;
66 
67 private:
68     /// Construct the scene content.
69     void CreateScene();
70     /// Construct an instruction text to the UI.
71     void CreateInstructions();
72     /// Set up a viewport for displaying the scene.
73     void SetupViewport();
74     /// Read input and moves the camera.
75     void UpdateCameraAndFloor(float timeStep);
76     /// Subscribe to application-wide logic update events.
77     void SubscribeToEvents();
78     /// Handle the logic update event.
79     void HandleUpdate(StringHash eventType, VariantMap& eventData);
80     /// Draw debug geometry
81     void HandlePostRenderUpdate(StringHash eventType, VariantMap& eventData);
82     /// Process IK logic
83     void HandleSceneDrawableUpdateFinished(StringHash eventType, VariantMap& eventData);
84 
85     SharedPtr<Node> cameraRotateNode_;
86 };
87