Stop changing the includes!
[blender.git] / source / gameengine / Ketsji / KX_SG_BoneParentNodeRelationship.cpp
1 /**
2  * $Id$
3  * ***** BEGIN GPL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  *
19  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20  * All rights reserved.
21  *
22  * The Original Code is: all of this file.
23  *
24  * Contributor(s): none yet.
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <iostream>
30  
31 #include "KX_SG_BoneParentNodeRelationship.h"
32
33 #include "MT_Matrix4x4.h"
34 #include "BL_ArmatureObject.h"
35
36
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 /**
42  * Implementation of classes defined in KX_SG_BoneParentNodeRelationship.h
43  */
44
45 /** 
46  * first of all KX_SG_BoneParentRelation
47  */
48
49         KX_BoneParentRelation *
50 KX_BoneParentRelation::
51 New(Bone* bone
52 ) {
53         return new KX_BoneParentRelation(bone);
54 }               
55
56         bool
57 KX_BoneParentRelation::
58 UpdateChildCoordinates(
59         SG_Spatial * child,
60         const SG_Spatial * parent,
61         bool& parentUpdated     
62 ){
63         MT_assert(child != NULL);
64         
65         // This way of accessing child coordinates is a bit cumbersome
66         // be nice to have non constant reference access to these values.
67
68         const MT_Vector3 & child_scale = child->GetLocalScale();
69         const MT_Point3 & child_pos = child->GetLocalPosition();
70         const MT_Matrix3x3 & child_rotation = child->GetLocalOrientation();
71         // we don't know if the armature has been updated or not, assume yes
72         parentUpdated = true;
73
74         // the childs world locations which we will update.     
75         
76         MT_Vector3 child_w_scale;
77         MT_Point3 child_w_pos;
78         MT_Matrix3x3 child_w_rotation;
79         
80         bool valid_parent_transform = false;
81         
82         if (parent)
83         {
84                 BL_ArmatureObject *armature = (BL_ArmatureObject*)(parent->GetSGClientObject());
85                 if (armature)
86                 {
87                         MT_Matrix4x4 parent_matrix;
88                         if (armature->GetBoneMatrix(m_bone, parent_matrix))
89                         {
90                                 // Get the child's transform, and the bone matrix.
91                                 MT_Matrix4x4 child_transform ( 
92                                         MT_Transform(child_pos + MT_Vector3(0.0, armature->GetBoneLength(m_bone), 0.0), 
93                                                 child_rotation.scaled(
94                                                         child_scale[0], 
95                                                         child_scale[1], 
96                                                         child_scale[2])));
97                                 
98                                 // The child's world transform is parent * child
99                                 parent_matrix = parent->GetWorldTransform() * parent_matrix;
100                                 child_transform = parent_matrix * child_transform;
101                                 
102                                 // Recompute the child transform components from the transform.
103                                 child_w_scale.setValue( 
104                                         MT_Vector3(child_transform[0][0], child_transform[0][1], child_transform[0][2]).length(),
105                                         MT_Vector3(child_transform[1][0], child_transform[1][1], child_transform[1][2]).length(),
106                                         MT_Vector3(child_transform[2][0], child_transform[2][1], child_transform[2][2]).length());
107                                 child_w_rotation.setValue(child_transform[0][0], child_transform[0][1], child_transform[0][2], 
108                                         child_transform[1][0], child_transform[1][1], child_transform[1][2], 
109                                         child_transform[2][0], child_transform[2][1], child_transform[2][2]);
110                                 child_w_rotation.scale(1.0/child_w_scale[0], 1.0/child_w_scale[1], 1.0/child_w_scale[2]);
111                                         
112                                 child_w_pos = MT_Point3(child_transform[0][3], child_transform[1][3], child_transform[2][3]);
113                                         
114                                 valid_parent_transform = true;
115                         }
116                 }
117         } 
118         
119         if (valid_parent_transform)
120         {
121                 child->SetWorldScale(child_w_scale);
122                 child->SetWorldPosition(child_w_pos);
123                 child->SetWorldOrientation(child_w_rotation);
124         }
125         else {
126                 child->SetWorldFromLocalTransform();
127         }
128         child->ClearModified();
129         // this node must always be updated, so reschedule it for next time
130         child->ActivateRecheduleUpdateCallback();
131         return valid_parent_transform;
132 }
133
134         SG_ParentRelation *
135 KX_BoneParentRelation::
136 NewCopy(
137 ){
138         KX_BoneParentRelation* bone_parent = new KX_BoneParentRelation(m_bone);
139         return bone_parent;
140 }
141
142 KX_BoneParentRelation::
143 ~KX_BoneParentRelation(
144 ){
145         //nothing to do
146 }
147
148
149 KX_BoneParentRelation::
150 KX_BoneParentRelation(Bone* bone
151 )
152 : m_bone(bone)
153 {
154         // nothing to do
155 }