forked from allista/AT_Utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnimatedNode.cs
106 lines (98 loc) · 3.58 KB
/
AnimatedNode.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
// AnimatedNode.cs
//
// Author:
// Allis Tauri <[email protected]>
//
// Copyright (c) 2016 Allis Tauri
using System.Linq;
using UnityEngine;
namespace AT_Utils
{
public class AnimatedNode
{
readonly AttachNode node;
readonly Transform nT;
readonly Part part;
Part attached_part;
AttachNode attached_node;
public AnimatedNode(AttachNode node, Transform node_transform, Part part)
{
this.node = node;
this.part = part;
nT = node_transform;
}
void UpdatePartsPos()
{
if(attached_part == null || attached_node == null) return;
var dp =
part.transform.TransformPoint(node.position) -
attached_part.transform.TransformPoint(attached_node.position);
part.UpdateAttachedPartPos(attached_part, dp);
}
bool UpdateJoint()
{
if(attached_part == null || attached_node == null) return false;
ConfigurableJoint joint = part.GetComponents<ConfigurableJoint>().FirstOrDefault(j => j.connectedBody == attached_part.Rigidbody);
bool update_anchor = true;
if(joint == null)
{
joint = attached_part.GetComponents<ConfigurableJoint>().FirstOrDefault(j => j.connectedBody == part.Rigidbody);
update_anchor = false;
}
if(joint == null) return false;
//setup joint
joint.xMotion = ConfigurableJointMotion.Limited;
joint.yMotion = ConfigurableJointMotion.Limited;
joint.zMotion = ConfigurableJointMotion.Limited;
//move anchors
if(update_anchor)
{
joint.anchor = node.position;
joint.connectedAnchor = attached_node.position;
}
else
{
joint.connectedAnchor = node.position;
joint.anchor = attached_node.position;
}
return true;
}
public void UpdateNode()
{
//update node
//node.size = 0; //force node size to be zero; otherwise the Kraken comes when inflating
node.position = part.partTransform.InverseTransformPoint(nT.position);
node.originalPosition = node.position;
//update attached parts
attached_part = node.attachedPart;
if(attached_part != null)
attached_node = attached_part.FindAttachNodeByPart(part);
if(!UpdateJoint()) UpdatePartsPos();
}
#if DEBUG
public void DrawAnchor()
{
Part a_part = node.attachedPart;
if(a_part == null) return;
Joint joint = part.GetComponents<Joint>().FirstOrDefault(j => j.connectedBody == a_part.Rigidbody);
bool update_anchor = true;
if(joint == null)
{
joint = a_part.GetComponents<Joint>().FirstOrDefault(j => j.connectedBody == part.Rigidbody);
update_anchor = false;
}
if(joint == null) return;
if(update_anchor)
{
Utils.GLDrawPoint(joint.anchor, part.partTransform, Color.red);
Utils.GLDrawPoint(joint.connectedAnchor, a_part.transform, Color.green);
}
else
{
Utils.GLDrawPoint(joint.connectedAnchor, part.partTransform, Color.red);
Utils.GLDrawPoint(joint.anchor, a_part.transform, Color.green);
}
}
#endif
}
}