summaryrefslogtreecommitdiff
path: root/Runtime/Physics2D/DistanceJoint2D.cpp
blob: 85b932a7a2f89506cb7509c7bd1b333439ae07a3 (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_2D_PHYSICS || DOXYGEN

#include "Runtime/Physics2D/DistanceJoint2D.h"
#include "Runtime/Physics2D/RigidBody2D.h"

#include "Runtime/Graphics/Transform.h"
#include "Runtime/Serialize/TransferFunctions/SerializeTransfer.h"
#include "Runtime/Utilities/ValidateArgs.h"

#include "External/Box2D/Box2D/Box2D.h"


IMPLEMENT_CLASS (DistanceJoint2D)
IMPLEMENT_OBJECT_SERIALIZE (DistanceJoint2D)


// --------------------------------------------------------------------------


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


DistanceJoint2D::~DistanceJoint2D ()
{
}


template<class TransferFunction>
void DistanceJoint2D::Transfer (TransferFunction& transfer)
{
	Super::Transfer(transfer);

	TRANSFER (m_Anchor);
	TRANSFER (m_ConnectedAnchor);
	TRANSFER (m_Distance);
}


void DistanceJoint2D::CheckConsistency ()	
{
	Super::CheckConsistency ();

	m_Distance = clamp<float> (m_Distance, b2_linearSlop, PHYSICS_2D_LARGE_RANGE_CLAMP);

	if (!IsFinite(m_Anchor))
		m_Anchor = Vector2f::zero;

	if (!IsFinite(m_ConnectedAnchor))
		m_ConnectedAnchor = Vector2f::zero;
}


void DistanceJoint2D::Reset ()
{
	Super::Reset ();

	m_Distance = 1.0f;
	m_Anchor = Vector2f::zero;
	m_ConnectedAnchor = Vector2f::zero;
}


void DistanceJoint2D::SetAnchor (const Vector2f& anchor)
{
	ABORT_INVALID_VECTOR2 (anchor, anchor, DistanceJoint2D);

	m_Anchor = anchor;
	SetDirty();

	// Recreate the joint.
	if (m_Joint != NULL)
		ReCreate();
}


void DistanceJoint2D::SetConnectedAnchor (const Vector2f& anchor)
{
	ABORT_INVALID_VECTOR2 (anchor, connectedAnchor, DistanceJoint2D);

	m_ConnectedAnchor = anchor;
	SetDirty();

	// Recreate the joint.
	if (m_Joint != NULL)
		ReCreate();
}


void DistanceJoint2D::SetDistance (float distance)
{
	ABORT_INVALID_FLOAT (distance, distance, DistanceJoint2D);

	m_Distance = clamp<float> (distance, b2_linearSlop, PHYSICS_2D_LARGE_RANGE_CLAMP);
	SetDirty();

	if (m_Joint != NULL)
		((b2RopeJoint*)m_Joint)->SetMaxLength (m_Distance);
}


// --------------------------------------------------------------------------


void DistanceJoint2D::Create ()
{
	Assert (m_Joint == NULL);

	if (!IsActive ())
		return;

	// Fetch transform scales.
	const Vector3f scale = GetComponent (Transform).GetWorldScaleLossy ();
	const Vector3f connectedScale = m_ConnectedRigidBody.IsNull () ? Vector3f::one : m_ConnectedRigidBody->GetComponent (Transform).GetWorldScaleLossy ();

	// Configure the joint definition.
	b2RopeJointDef jointDef;
	jointDef.maxLength = m_Distance;
	jointDef.localAnchorA.Set (m_Anchor.x * scale.x, m_Anchor.y * scale.y);
	jointDef.localAnchorB.Set (m_ConnectedAnchor.x * connectedScale.x, m_ConnectedAnchor.y * connectedScale.y);
		
	// Create the joint.
	FinalizeCreateJoint (&jointDef);
}


void DistanceJoint2D::AutoCalculateDistance ()
{
	// Reset to default.
	m_Distance = 1.0f;

	if (m_ConnectedRigidBody.IsNull ())
		return;

	// Find the appropriate rigid body A.
	Rigidbody2D* rigidBodyA = QueryComponent(Rigidbody2D);
	Assert (rigidBodyA != NULL);

	// Find the appropriate rigid body B.
	Rigidbody2D* rigidBodyB = m_ConnectedRigidBody;

	Transform* transformA = QueryComponent (Transform);
	Vector3f pointA = transformA->TransformPoint(Vector3f(m_Anchor.x, m_Anchor.y, 0.0f));

	if (rigidBodyB == NULL)
	{
		m_Distance = Magnitude(Vector2f(pointA.x, pointA.y));
		return;
	}

	Transform* transformB = rigidBodyB->GetGameObjectPtr ()->QueryComponent (Transform);
	Vector3f pointB = transformB->TransformPoint(Vector3f(m_ConnectedAnchor.x, m_ConnectedAnchor.y, 0.0f));

	m_Distance = Magnitude(Vector2f(pointA.x-pointB.x, pointA.y-pointB.y));
}

#endif //ENABLE_2D_PHYSICS