/
CCMotorJoint.js
205 lines (187 loc) · 6.9 KB
/
CCMotorJoint.js
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
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
https://www.cocos.com/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
var PTM_RATIO = require('../CCPhysicsTypes').PTM_RATIO;
var ANGLE_TO_PHYSICS_ANGLE = require('../CCPhysicsTypes').ANGLE_TO_PHYSICS_ANGLE;
/**
* !#en
* A motor joint is used to control the relative motion
* between two bodies. A typical usage is to control the movement
* of a dynamic body with respect to the ground.
* !#zh
* 马达关节被用来控制两个刚体间的相对运动。
* 一个典型的例子是用来控制一个动态刚体相对于地面的运动。
* @class MotorJoint
* @extends Joint
*/
var MotorJoint = cc.Class({
name: 'cc.MotorJoint',
extends: cc.Joint,
editor: CC_EDITOR && {
menu: 'i18n:MAIN_MENU.component.physics/Joint/Motor',
inspector: 'packages://inspector/inspectors/comps/physics/joint.js',
},
properties: {
_linearOffset: cc.v2(0, 0),
_angularOffset: 0,
_maxForce: 1,
_maxTorque: 1,
_correctionFactor: 0.3,
/**
* !#en
* The anchor of the rigidbody.
* !#zh
* 刚体的锚点。
* @property {Vec2} anchor
* @default cc.v2(0, 0)
*/
anchor: {
tooltip: CC_DEV && 'i18n:COMPONENT.physics.physics_collider.anchor',
default: cc.v2(0, 0),
override: true,
visible: false
},
/**
* !#en
* The anchor of the connected rigidbody.
* !#zh
* 关节另一端刚体的锚点。
* @property {Vec2} connectedAnchor
* @default cc.v2(0, 0)
*/
connectedAnchor: {
tooltip: CC_DEV && 'i18n:COMPONENT.physics.physics_collider.connectedAnchor',
default: cc.v2(0, 0),
override: true,
visible: false
},
/**
* !#en
* The linear offset from connected rigidbody to rigidbody.
* !#zh
* 关节另一端的刚体相对于起始端刚体的位置偏移量
* @property {Vec2} linearOffset
* @default cc.v2(0,0)
*/
linearOffset: {
tooltip: CC_DEV && 'i18n:COMPONENT.physics.physics_collider.linearOffset',
get: function () {
return this._linearOffset;
},
set: function (value) {
this._linearOffset = value;
if (this._joint) {
this._joint.SetLinearOffset( new b2.Vec2(value.x/PTM_RATIO, value.y/PTM_RATIO) );
}
}
},
/**
* !#en
* The angular offset from connected rigidbody to rigidbody.
* !#zh
* 关节另一端的刚体相对于起始端刚体的角度偏移量
* @property {Number} angularOffset
* @default 0
*/
angularOffset: {
tooltip: CC_DEV && 'i18n:COMPONENT.physics.physics_collider.angularOffset',
get: function () {
return this._angularOffset;
},
set: function (value) {
this._angularOffset = value;
if (this._joint) {
this._joint.SetAngularOffset(value);
}
}
},
/**
* !#en
* The maximum force can be applied to rigidbody.
* !#zh
* 可以应用于刚体的最大的力值
* @property {Number} maxForce
* @default 1
*/
maxForce: {
tooltip: CC_DEV && 'i18n:COMPONENT.physics.physics_collider.maxForce',
get: function () {
return this._maxForce;
},
set: function (value) {
this._maxForce = value;
if (this._joint) {
this._joint.SetMaxForce(value);
}
}
},
/**
* !#en
* The maximum torque can be applied to rigidbody.
* !#zh
* 可以应用于刚体的最大扭矩值
* @property {Number} maxTorque
* @default 1
*/
maxTorque: {
tooltip: CC_DEV && 'i18n:COMPONENT.physics.physics_collider.maxTorque',
get: function () {
return this._maxTorque;
},
set: function (value) {
this._maxTorque = value;
if (this._joint) {
this._joint.SetMaxTorque(value);
}
}
},
/**
* !#en
* The position correction factor in the range [0,1].
* !#zh
* 位置矫正系数,范围为 [0, 1]
* @property {Number} correctionFactor
* @default 0.3
*/
correctionFactor: {
tooltip: CC_DEV && 'i18n:COMPONENT.physics.physics_collider.correctionFactor',
get: function () {
return this._correctionFactor;
},
set: function (value) {
this._correctionFactor = value;
if (this._joint) {
this._joint.SetCorrectionFactor(value);
}
}
},
},
_createJointDef: function () {
var def = new b2.MotorJointDef();
def.linearOffset = new b2.Vec2(this.linearOffset.x/PTM_RATIO, this.linearOffset.y/PTM_RATIO);
def.angularOffset = this.angularOffset * ANGLE_TO_PHYSICS_ANGLE;
def.maxForce = this.maxForce;
def.maxTorque = this.maxTorque;
def.correctionFactor = this.correctionFactor;
return def;
}
});
cc.MotorJoint = module.exports = MotorJoint;