/
CCMouseJoint.js
278 lines (247 loc) · 8.87 KB
/
CCMouseJoint.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/****************************************************************************
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 tempB2Vec2 = new b2.Vec2();
/**
* !#en
* A mouse joint is used to make a point on a body track a
* specified world point. This a soft constraint with a maximum
* force. This allows the constraint to stretch and without
* applying huge forces.
* Mouse Joint will auto register the touch event with the mouse region node,
* and move the choosed rigidbody in touch move event.
* Note : generally mouse joint only used in test bed.
* !#zh
* 鼠标关节用于使刚体上的一个点追踪一个指定的世界坐标系下的位置。
* 鼠标关节可以指定一个最大的里来施加一个柔和的约束。
* 鼠标关节会自动使用 mouse region 节点来注册鼠标事件,并且在触摸移动事件中移动选中的刚体。
* 注意:一般鼠标关节只在测试环境中使用。
* @class MouseJoint
* @extends Joint
*/
var MouseJoint = cc.Class({
name: 'cc.MouseJoint',
extends: cc.Joint,
editor: CC_EDITOR && {
inspector: 'packages://inspector/inspectors/comps/physics/joint.js',
menu: 'i18n:MAIN_MENU.component.physics/Joint/Mouse',
},
properties: {
_target: 1,
_frequency: 5,
_dampingRatio: 0.7,
_maxForce: 0,
connectedBody: {
default: null,
type: cc.RigidBody,
visible: false,
override: true
},
collideConnected: {
default: true,
visible: false,
override: true
},
/**
* !#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 node used to register touch evnet.
* If this is null, it will be the joint's node.
* !#zh
* 用于注册触摸事件的节点。
* 如果没有设置这个值,那么将会使用关节的节点来注册事件。
* @property {Node} mouseRegion
* @default null
*/
mouseRegion: {
tooltip: CC_DEV && 'i18n:COMPONENT.physics.physics_collider.mouseRegion',
default: null,
type: cc.Node
},
/**
* !#en
* The target point.
* The mouse joint will move choosed rigidbody to target point.
* !#zh
* 目标点,鼠标关节将会移动选中的刚体到指定的目标点
* @property {Vec2} target
*/
target: {
tooltip: CC_DEV && 'i18n:COMPONENT.physics.physics_collider.target',
visible: false,
get: function () {
return this._target;
},
set: function (value) {
this._target = value;
if (this._joint) {
tempB2Vec2.x = value.x/PTM_RATIO;
tempB2Vec2.y = value.y/PTM_RATIO;
this._joint.SetTarget(tempB2Vec2);
}
}
},
/**
* !#en
* The spring frequency.
* !#zh
* 弹簧系数。
* @property {Number} frequency
* @default 0
*/
frequency: {
tooltip: CC_DEV && 'i18n:COMPONENT.physics.physics_collider.frequency',
get: function () {
return this._frequency;
},
set: function (value) {
this._frequency = value;
if (this._joint) {
this._joint.SetFrequency(value);
}
}
},
/**
* !#en
* The damping ratio.
* !#zh
* 阻尼,表示关节变形后,恢复到初始状态受到的阻力。
* @property {Number} dampingRatio
* @property 0
*/
dampingRatio: {
tooltip: CC_DEV && 'i18n:COMPONENT.physics.physics_collider.dampingRatio',
get: function () {
return this._dampingRatio;
},
set: function (value) {
this._dampingRatio = value;
if (this._joint) {
this._joint.SetDampingRatio(value);
}
}
},
/**
* !#en
* The maximum force
* !#zh
* 最大阻力值
* @property {Number} maxForce
* @default 1
*/
maxForce: {
tooltip: CC_DEV && 'i18n:COMPONENT.physics.physics_collider.maxForce',
visible: false,
get: function () {
return this._maxForce;
},
set: function (value) {
this._maxForce = value;
if (this._joint) {
this._joint.SetMaxForce(value);
}
}
},
},
onLoad: function () {
var mouseRegion = this.mouseRegion || this.node;
mouseRegion.on(cc.Node.EventType.TOUCH_START, this.onTouchBegan, this);
mouseRegion.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
mouseRegion.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
mouseRegion.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
},
onEnable: function () {
},
start: function () {
},
onTouchBegan: function (event) {
var manager = cc.director.getPhysicsManager();
var target = this._pressPoint = event.touch.getLocation();
if (cc.Camera && cc.Camera.main) {
target = cc.Camera.main.getScreenToWorldPoint(target);
}
var collider = manager.testPoint( target );
if (!collider) return;
var body = this.connectedBody = collider.body;
body.awake = true;
this.maxForce = 1000 * this.connectedBody.getMass();
this.target = target;
this._init();
},
onTouchMove: function (event) {
this._pressPoint = event.touch.getLocation();
},
onTouchEnd: function (event) {
this._destroy();
this._pressPoint = null;
},
_createJointDef: function () {
var def = new b2.MouseJointDef();
tempB2Vec2.x = this.target.x/PTM_RATIO;
tempB2Vec2.y = this.target.y/PTM_RATIO;
def.target = tempB2Vec2;
def.maxForce = this.maxForce;
def.dampingRatio = this.dampingRatio;
def.frequencyHz = this.frequency;
return def;
},
update: function () {
if (!this._pressPoint || !this._isValid()) {
return;
}
var camera = cc.Camera.findCamera(this.node);
if (camera) {
this.target = camera.getScreenToWorldPoint(this._pressPoint);
}
else {
this.target = this._pressPoint;
}
}
});
cc.MouseJoint = module.exports = MouseJoint;