/
physicsEngine.ts
185 lines (167 loc) · 5.57 KB
/
physicsEngine.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
import type { Nullable } from "../../types";
import { Vector3 } from "../../Maths/math.vector";
import type { IPhysicsEngine } from "../IPhysicsEngine";
import type { IPhysicsEnginePluginV2 } from "./IPhysicsEnginePlugin";
import { PhysicsRaycastResult } from "../physicsRaycastResult";
import { _WarnImport } from "../../Misc/devTools";
import type { PhysicsBody } from "./physicsBody";
/**
* Class used to control physics engine
* @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine
*/
/** @internal */
export class PhysicsEngine implements IPhysicsEngine {
/** @internal */
private _physicsBodies: Array<PhysicsBody> = [];
private _subTimeStep: number = 0;
//private _uniqueIdCounter = 0;
/**
* Gets the gravity vector used by the simulation
*/
public gravity: Vector3;
/**
*
* @returns physics plugin version
*/
public getPluginVersion(): number {
return this._physicsPlugin.getPluginVersion();
}
/**
* Factory used to create the default physics plugin.
* @returns The default physics plugin
*/
public static DefaultPluginFactory(): IPhysicsEnginePluginV2 {
throw _WarnImport("");
}
/**
* Creates a new Physics Engine
* @param gravity defines the gravity vector used by the simulation
* @param _physicsPlugin defines the plugin to use (CannonJS by default)
*/
constructor(gravity: Nullable<Vector3>, private _physicsPlugin: IPhysicsEnginePluginV2 = PhysicsEngine.DefaultPluginFactory()) {
gravity = gravity || new Vector3(0, -9.807, 0);
this.setGravity(gravity);
this.setTimeStep();
}
/**
* Sets the gravity vector used by the simulation
* @param gravity defines the gravity vector to use
*/
public setGravity(gravity: Vector3): void {
this.gravity = gravity;
this._physicsPlugin.setGravity(this.gravity);
}
/**
* Set the time step of the physics engine.
* Default is 1/60.
* To slow it down, enter 1/600 for example.
* To speed it up, 1/30
* @param newTimeStep defines the new timestep to apply to this world.
*/
public setTimeStep(newTimeStep: number = 1 / 60) {
this._physicsPlugin.setTimeStep(newTimeStep);
}
/**
* Get the time step of the physics engine.
* @returns the current time step
*/
public getTimeStep(): number {
return this._physicsPlugin.getTimeStep();
}
/**
* Set the sub time step of the physics engine.
* Default is 0 meaning there is no sub steps
* To increase physics resolution precision, set a small value (like 1 ms)
* @param subTimeStep defines the new sub timestep used for physics resolution.
*/
public setSubTimeStep(subTimeStep: number = 0) {
this._subTimeStep = subTimeStep;
}
/**
* Get the sub time step of the physics engine.
* @returns the current sub time step
*/
public getSubTimeStep() {
return this._subTimeStep;
}
/**
* Release all resources
*/
public dispose(): void {
this._physicsPlugin.dispose();
}
/**
* Gets the name of the current physics plugin
* @returns the name of the plugin
*/
public getPhysicsPluginName(): string {
return this._physicsPlugin.name;
}
/**
* Adding a new impostor for the impostor tracking.
* This will be done by the impostor itself.
* @param impostor the impostor to add
*/
/**
* Called by the scene. No need to call it.
* @param delta defines the timespan between frames
*/
public _step(delta: number) {
//check if any mesh has no body / requires an update
/*this._impostors.forEach((impostor) => {
if (impostor.isBodyInitRequired()) {
this._physicsPlugin.generatePhysicsBody(impostor);
}
});
*/
if (delta > 0.1) {
delta = 0.1;
} else if (delta <= 0) {
delta = 1.0 / 60.0;
}
this._physicsPlugin.executeStep(delta, this._physicsBodies);
}
/**
*
* @param body
*/
public addBody(physicsBody: PhysicsBody): void {
this._physicsBodies.push(physicsBody);
}
/**
*
*/
public removeBody(physicsBody: PhysicsBody): void {
const index = this._physicsBodies.indexOf(physicsBody);
if (index > -1) {
/*const removed =*/ this._physicsBodies.splice(index, 1);
}
}
/**
* Gets the current plugin used to run the simulation
* @returns current plugin
*/
public getPhysicsPlugin(): IPhysicsEnginePluginV2 {
return this._physicsPlugin;
}
/**
* Does a raycast in the physics world
* @param from when should the ray start?
* @param to when should the ray end?
* @param result resulting PhysicsRaycastResult
*/
public raycastToRef(from: Vector3, to: Vector3, result: PhysicsRaycastResult): void {
this._physicsPlugin.raycast(from, to, result);
}
/**
* Does a raycast in the physics world
* @param from when should the ray start?
* @param to when should the ray end?
* @returns PhysicsRaycastResult
*/
public raycast(from: Vector3, to: Vector3): PhysicsRaycastResult {
const result = new PhysicsRaycastResult();
this._physicsPlugin.raycast(from, to, result);
return result;
}
}