-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
constant-force.ts
160 lines (136 loc) · 3.73 KB
/
constant-force.ts
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
/**
* @category physics
*/
import {
ccclass,
executeInEditMode,
executionOrder,
menu,
property,
requireComponent,
disallowMultiple,
} from '../../core/data/class-decorator';
import { Component } from '../../core/components/component';
import { RigidBodyComponent } from './rigid-body-component';
import { Vec3 } from '../../core/math/vec3';
/**
* @zh
* 在每帧对一个刚体施加持续的力,依赖 RigidBodyComponent 组件
*/
@ccclass('cc.ConstantForce')
@executionOrder(98)
@requireComponent(RigidBodyComponent)
@menu('Components/ConstantForce')
@disallowMultiple
@executeInEditMode
export class ConstantForce extends Component {
private _rigidbody: RigidBodyComponent | null = null;
@property
private readonly _force: Vec3 = new Vec3();
@property
private readonly _localForce: Vec3 = new Vec3();
@property
private readonly _torque: Vec3 = new Vec3();
@property
private readonly _localTorque: Vec3 = new Vec3();
private _mask: number = 0;
/**
* @zh
* 获取和设置世界朝向的力
* @note
* 设置时请用 this.force = otherVec3 的方式
*/
@property({
displayOrder: 0,
})
public get force () {
return this._force;
}
public set force (value: Vec3) {
Vec3.copy(this._force, value);
this._maskUpdate(this._force, 1);
}
/**
* @zh
* 获取和设置本地朝向的力
* @note
* 设置时请用 this.localForce = otherVec3 的方式
*/
@property({
displayOrder: 1,
})
public get localForce () {
return this._localForce;
}
public set localForce (value: Vec3) {
Vec3.copy(this._localForce, value);
this._maskUpdate(this.localForce, 2);
}
/**
* @zh
* 获取和设置世界朝向的扭转力
* @note
* 设置时请用 this.torque = otherVec3 的方式
*/
@property({
displayOrder: 2,
})
public get torque () {
return this._torque;
}
public set torque (value: Vec3) {
Vec3.copy(this._torque, value);
this._maskUpdate(this._torque, 4);
}
/**
* @zh
* 获取和设置本地朝向的扭转力
* @note
* 设置时请用 this.localTorque = otherVec3 的方式
*/
@property({
displayOrder: 3,
})
public get localTorque () {
return this._localTorque;
}
public set localTorque (value: Vec3) {
Vec3.copy(this._localTorque, value);
this._maskUpdate(this._localTorque, 8);
}
public onLoad () {
if (!CC_PHYSICS_BUILT_IN) {
this._rigidbody = this.node.getComponent(RigidBodyComponent);
this._maskUpdate(this._force, 1);
this._maskUpdate(this._localForce, 2);
this._maskUpdate(this._torque, 4);
this._maskUpdate(this._localTorque, 8);
}
}
public lateUpdate (dt: number) {
if (!CC_PHYSICS_BUILT_IN) {
if (this._rigidbody != null && this._mask != 0) {
if (this._mask & 1) {
this._rigidbody.applyForce(this._force);
}
if (this._mask & 2) {
this._rigidbody.applyLocalForce(this.localForce);
}
if (this._mask & 4) {
this._rigidbody.applyTorque(this._torque);
}
if (this._mask & 8) {
this._rigidbody.applyLocalTorque(this._localTorque);
}
}
}
}
private _maskUpdate (t: Vec3, m: number) {
if (t.strictEquals(Vec3.ZERO)) {
this._mask &= ~m;
} else {
this._mask |= m;
}
}
}
cc.ConstantForce = ConstantForce;