-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
physics-system.ts
191 lines (170 loc) · 4.93 KB
/
physics-system.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
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
/**
* @category physics
*/
import { Vec3 } from '../../core/math';
import { PhysicsWorldBase } from '../api';
import { createPhysicsWorld } from '../instance';
import { director, Director } from '../../core/director';
import { System } from '../../core/components';
import { PhysicMaterial } from '../assets/physic-material';
/**
* @zh
* 物理系统。
*/
export class PhysicsSystem extends System {
/**
* @zh
* 获取或设置是否启用物理系统,可以用于暂停或继续运行物理系统。
*/
public get enable (): boolean {
return this._enable;
}
public set enable (value: boolean) {
this._enable = value;
}
/**
* @zh
* 获取或设置物理系统是否允许自动休眠,默认为 true
*/
public get allowSleep (): boolean {
return this._allowSleep;
}
public set allowSleep (v: boolean) {
this._allowSleep = v;
if (!CC_EDITOR) {
if (!CC_PHYSICS_BUILT_IN) {
this._world.setAllowSleep(this._allowSleep);
}
}
}
// shielding for alpha version
// /**
// * @zh
// * 获取或设置是否只运行一步。
// * @param {boolean} b
// */
// public get singleStep () {
// return this._singleStep;
// }
// public set singleStep (b: boolean) {
// this._singleStep = b;
// }
/**
* @zh
* 获取或设置每帧模拟的最大子步数。
*/
public get maxSubStep () {
return this._maxSubStep;
}
public set maxSubStep (value: number) {
this._maxSubStep = value;
}
/**
* @zh
* 获取或设置每步模拟消耗的固定时间。
*/
public get deltaTime () {
return this._deltaTime;
}
public set deltaTime (value: number) {
this._deltaTime = value;
}
/**
* @zh
* 获取或设置物理世界的重力数值,默认为 (0, -10, 0)
*/
public get gravity (): Vec3 {
if (!CC_EDITOR) {
if (!CC_PHYSICS_BUILT_IN) {
this._world.getGravity(this._gravity);
}
}
return this._gravity;
}
public set gravity (gravity: Vec3) {
this._gravity.x = gravity.x;
this._gravity.y = gravity.y;
this._gravity.z = gravity.z;
if (!CC_EDITOR) {
if (!CC_PHYSICS_BUILT_IN) {
this._world.setGravity(gravity);
}
}
}
/**
* @zh
* 获取全局的默认物理材质,注意:builtin 时为 null
*/
public get defaultMaterial (): PhysicMaterial | null {
return this._material;
}
// get world () {
// return this._world;
// }
// shielding for alpha version
// /**
// * @zh
// * 获取或设置物理每秒模拟的帧率。
// */
// public get frameRate () {
// return this._frameRate;
// }
// public set frameRate (value: number) {
// this._frameRate = value;
// this._deltaTime = 1 / this._frameRate;
// }
public static instance: PhysicsSystem;
public static ID: 'physics';
public _world: PhysicsWorldBase;
private _enable = true;
private _deltaTime = 1.0 / 60.0;
private _maxSubStep = 2;
// private _frameRate = 60;
// private _singleStep = false;
private _allowSleep = true;
private readonly _gravity = new Vec3(0, -10, 0);
private readonly _material: PhysicMaterial | null = null;
constructor () {
super();
this._world = createPhysicsWorld();
if (!CC_PHYSICS_BUILT_IN) {
this.gravity = this._gravity;
this.allowSleep = this._allowSleep;
this._material = new PhysicMaterial();
this._material.friction = 0.6;
this._material.restitution = -1;
this._material.on('physics_material_update', this._updateMaterial, this);
this._world.defaultMaterial = this._material;
}
}
/**
* @zh
* 执行一次物理系统的模拟,目前将在每帧自动执行一次。
* @param deltaTime 与上一次执行相差的时间,目前为每帧消耗时间
*/
public postUpdate (deltaTime: number) {
if (CC_EDITOR && !this._executeInEditMode) {
return;
}
if (!this._enable) {
return;
}
director.emit(Director.EVENT_BEFORE_PHYSICS);
this._world.step(this._deltaTime, deltaTime, this._maxSubStep);
// if (this._singleStep) {
// this._enable = false;
// }
director.emit(Director.EVENT_AFTER_PHYSICS);
}
private _updateMaterial () {
if (!CC_PHYSICS_BUILT_IN) {
this._world.defaultMaterial = this._material;
}
}
}
cc.PhysicsSystem = PhysicsSystem;
director.on(Director.EVENT_INIT, function () {
let sys = new PhysicsSystem();
PhysicsSystem.instance = sys;
director.registerSystem(PhysicsSystem.ID, sys, 0);
});