summaryrefslogtreecommitdiff
path: root/Runtime/Dynamics/SpringJoint.cpp
blob: 440d8f8a970a1ea43052fc2c26628118697eed11 (plain)
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
#include "UnityPrefix.h"
#if ENABLE_PHYSICS
#include "SpringJoint.h"
#include "PhysicsManager.h"
#include "Runtime/Serialize/TransferFunctions/SerializeTransfer.h"
#include "External/PhysX/builds/SDKs/Physics/include/NxPhysics.h"

using namespace std;

namespace Unity
{

#define GET_JOINT() static_cast<NxDistanceJoint*> (m_Joint)

/*
- We awake the hingejoint only once. (AwakeFromLoad)
  At this point we setup the axes. They are never changed afterwards
  -> The perfect solution remembers the old position/rotation of the rigid bodies.
      Then when changing axis/anchor is changed it generates axes that are rleative to the old position/rotation state!
*/

SpringJoint::SpringJoint (MemLabelId label, ObjectCreationMode mode)
:	Super(label, mode)
{
}

SpringJoint::~SpringJoint ()
{
}

void SpringJoint::Reset()
{
	Super::Reset();
	m_MinDistance = 0.0F;
	m_MaxDistance = 0.5F;
	m_Spring = 2;
	m_Damper = 0.2f;
}

void SpringJoint::Create ()
{
	AssertIf (!IsActive ());

	NxDistanceJointDesc desc;

	if (m_Joint && m_Joint->getState () == NX_JS_SIMULATING)
		GET_JOINT()->saveToDesc (desc);

	desc.spring.spring = m_Spring;
	desc.spring.damper = m_Damper;
	
	if (desc.minDistance < m_MaxDistance)
	{
		desc.minDistance = m_MinDistance;
		desc.maxDistance = m_MaxDistance;
	}
	else
	{
		desc.minDistance = m_MaxDistance;
		desc.maxDistance = m_MinDistance;
	}
		
	desc.flags |= NX_DJF_SPRING_ENABLED | NX_DJF_MAX_DISTANCE_ENABLED | NX_DJF_MIN_DISTANCE_ENABLED;
	
	FINALIZE_CREATE (desc, NxDistanceJointDesc);
}
void SpringJoint::SetSpring (float spring)
{
	m_Spring = spring;
	SetDirty();

	if (GET_JOINT() != NULL)
	{
		NxDistanceJointDesc desc;
		GET_JOINT()->saveToDesc (desc);
		desc.spring.spring = m_Spring;
		GET_JOINT()->loadFromDesc (desc);
	}
}

void SpringJoint::SetDamper(float damper)
{
	m_Damper = damper;
	SetDirty();
	
	if (GET_JOINT() != NULL)
	{
		NxDistanceJointDesc desc;
		GET_JOINT()->saveToDesc (desc);
		desc.spring.damper = m_Damper;
		GET_JOINT()->loadFromDesc (desc);
	}
}

void SpringJoint::SetMinDistance(float distance)
{
	m_MinDistance = distance;
	SetDirty();

	if (GET_JOINT() != NULL)
	{
		NxDistanceJointDesc desc;
		GET_JOINT()->saveToDesc (desc);
		if (desc.minDistance < m_MaxDistance)
		{
			desc.minDistance = m_MinDistance;
			desc.maxDistance = m_MaxDistance;
		}
		else
		{
			desc.minDistance = m_MaxDistance;
			desc.maxDistance = m_MinDistance;
		}
		GET_JOINT()->loadFromDesc (desc);
	}
}

void SpringJoint::SetMaxDistance(float distance)
{
	m_MaxDistance = distance;
	SetDirty();

	if (GET_JOINT() != NULL)
	{
		NxDistanceJointDesc desc;
		GET_JOINT()->saveToDesc (desc);
		if (desc.minDistance < m_MaxDistance)
		{
			desc.minDistance = m_MinDistance;
			desc.maxDistance = m_MaxDistance;
		}
		else
		{
			desc.minDistance = m_MaxDistance;
			desc.maxDistance = m_MinDistance;
		}
		GET_JOINT()->loadFromDesc (desc);
	}
}

IMPLEMENT_AXIS_ANCHOR(SpringJoint,NxDistanceJointDesc)

template<class TransferFunction>
void SpringJoint::Transfer (TransferFunction& transfer)
{
	JointTransferPreNoAxis(transfer);
	TRANSFER_SIMPLE (m_Spring);
	TRANSFER_SIMPLE (m_Damper);
	TRANSFER_SIMPLE (m_MinDistance);
	TRANSFER_SIMPLE (m_MaxDistance);
	JointTransferPost (transfer);
}

#undef GET_JOINT

}

IMPLEMENT_CLASS (SpringJoint)
IMPLEMENT_OBJECT_SERIALIZE (SpringJoint)

#endif //ENABLE_PHYSICS