-
Notifications
You must be signed in to change notification settings - Fork 4
/
ActorRotationConstraint.cs
214 lines (193 loc) · 7.41 KB
/
ActorRotationConstraint.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using System;
using System.IO;
using System.Collections.Generic;
using Nima.Math2D;
namespace Nima
{
public class ActorRotationConstraint : ActorTargetedConstraint
{
const float PI2 = (float)(Math.PI*2.0);
protected bool m_Copy;
protected float m_Scale;
protected bool m_EnableMin;
protected bool m_EnableMax;
protected float m_Max;
protected float m_Min;
protected bool m_Offset;
protected TransformSpace m_SourceSpace;
protected TransformSpace m_DestSpace;
protected TransformSpace m_MinMaxSpace;
TransformComponents m_ComponentsA;
TransformComponents m_ComponentsB;
public ActorRotationConstraint()
{
m_Copy = false;
m_Scale = 1.0f;
m_EnableMin = false;
m_EnableMax = false;
m_Min = 0.0f;
m_Max = 0.0f;
m_Offset = false;
m_SourceSpace = TransformSpace.World;
m_DestSpace = TransformSpace.World;
m_MinMaxSpace = TransformSpace.World;
m_ComponentsA = new TransformComponents();
m_ComponentsB = new TransformComponents();
}
public override void OnDirty(byte dirt)
{
MarkDirty();
}
public static ActorRotationConstraint Read(Actor actor, BinaryReader reader, ActorRotationConstraint component = null)
{
if(component == null)
{
component = new ActorRotationConstraint();
}
ActorTargetedConstraint.Read(actor, reader, component);
if((component.m_Copy = reader.ReadByte() == 1))
{
component.m_Scale = reader.ReadSingle();
}
if((component.m_EnableMin = reader.ReadByte() == 1))
{
component.m_Min = reader.ReadSingle();
}
if((component.m_EnableMax = reader.ReadByte() == 1))
{
component.m_Max = reader.ReadSingle();
}
component.m_Offset = reader.ReadByte() == 1;
component.m_SourceSpace = (TransformSpace)reader.ReadByte();
component.m_DestSpace = (TransformSpace)reader.ReadByte();
component.m_MinMaxSpace = (TransformSpace)reader.ReadByte();
return component;
}
public void Copy(ActorRotationConstraint node, Actor resetActor)
{
base.Copy(node, resetActor);
m_Copy = node.m_Copy;
m_Scale = node.m_Scale;
m_EnableMin = node.m_EnableMin;
m_EnableMax = node.m_EnableMax;
m_Min = node.m_Min;
m_Max = node.m_Max;
m_Offset = node.m_Offset;
m_SourceSpace = node.m_SourceSpace;
m_DestSpace = node.m_DestSpace;
m_MinMaxSpace = node.m_MinMaxSpace;
}
public override void Constrain(ActorNode node)
{
ActorNode target = m_Target as ActorNode;
ActorNode grandParent = m_Parent.Parent;
Mat2D transformA = m_Parent.WorldTransform;
Mat2D transformB = new Mat2D();
Mat2D.Decompose(transformA, m_ComponentsA);
if(target == null)
{
Mat2D.Copy(transformB, transformA);
m_ComponentsB[0] = m_ComponentsA[0];
m_ComponentsB[1] = m_ComponentsA[1];
m_ComponentsB[2] = m_ComponentsA[2];
m_ComponentsB[3] = m_ComponentsA[3];
m_ComponentsB[4] = m_ComponentsA[4];
m_ComponentsB[5] = m_ComponentsA[5];
}
else
{
Mat2D.Copy(transformB, target.WorldTransform);
if(m_SourceSpace == TransformSpace.Local)
{
ActorNode sourceGrandParent = target.Parent;
if(sourceGrandParent != null)
{
Mat2D inverse = new Mat2D();
if(!Mat2D.Invert(inverse, sourceGrandParent.WorldTransform))
{
return;
}
Mat2D.Multiply(transformB, inverse, transformB);
}
}
Mat2D.Decompose(transformB, m_ComponentsB);
if(!m_Copy)
{
m_ComponentsB.Rotation = m_DestSpace == TransformSpace.Local ? 1.0f : m_ComponentsA.Rotation;
}
else
{
m_ComponentsB.Rotation *= m_Scale;
if(m_Offset)
{
m_ComponentsB.Rotation += m_Parent.Rotation;
}
}
if(m_DestSpace == TransformSpace.Local)
{
// Destination space is in parent transform coordinates.
// Recompose the parent local transform and get it in world, then decompose the world for interpolation.
if(grandParent != null)
{
Mat2D.Compose(transformB, m_ComponentsB);
Mat2D.Multiply(transformB, grandParent.WorldTransform, transformB);
Mat2D.Decompose(transformB, m_ComponentsB);
}
}
}
bool clampLocal = m_MinMaxSpace == TransformSpace.Local && grandParent != null;
if(clampLocal)
{
// Apply min max in local space, so transform to local coordinates first.
Mat2D.Compose(transformB, m_ComponentsB);
Mat2D inverse = new Mat2D();
if(!Mat2D.Invert(inverse, grandParent.WorldTransform))
{
return;
}
Mat2D.Multiply(transformB, inverse, transformB);
Mat2D.Decompose(transformB, m_ComponentsB);
}
if(m_EnableMax && m_ComponentsB.Rotation > m_Max)
{
m_ComponentsB.Rotation = m_Max;
}
if(m_EnableMin && m_ComponentsB.Rotation < m_Min)
{
m_ComponentsB.Rotation = m_Min;
}
if(clampLocal)
{
// Transform back to world.
Mat2D.Compose(transformB, m_ComponentsB);
Mat2D.Multiply(transformB, grandParent.WorldTransform, transformB);
Mat2D.Decompose(transformB, m_ComponentsB);
}
float angleA = m_ComponentsA.Rotation%PI2;
float angleB = m_ComponentsB.Rotation%PI2;
float diff = angleB - angleA;
if(diff > Math.PI)
{
diff -= PI2;
}
else if(diff < -Math.PI)
{
diff += PI2;
}
float ti = 1.0f-m_Strength;
m_ComponentsB.Rotation = m_ComponentsA.Rotation + diff * m_Strength;
m_ComponentsB.X = m_ComponentsA.X;
m_ComponentsB.Y = m_ComponentsA.Y;
m_ComponentsB.ScaleX = m_ComponentsA.ScaleX;
m_ComponentsB.ScaleY = m_ComponentsA.ScaleY;
m_ComponentsB.Skew = m_ComponentsA.Skew;
Mat2D.Compose(m_Parent.WorldTransform, m_ComponentsB);
}
public override ActorComponent MakeInstance(Actor resetActor)
{
ActorRotationConstraint instance = new ActorRotationConstraint();
instance.Copy(this, resetActor);
return instance;
}
}
}