-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModelBullet.js
164 lines (132 loc) · 4.52 KB
/
ModelBullet.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
/**
* @module libgptjs Graphical Programming with ThreeJS (GPT)
* @class ModelBullet
*/
import { lerp } from "three/src/math/MathUtils";
import THREE from "../external-libs/three-global";
import GPT_Model from "../core/GPT_Model";
import Common from "./Common";
import GPT_ModelCollider from "../core/GPT_ModelCollider";
/**
* Creates a model for bullet
*
* Inherits from GPT_Model
*
* Sets initial position of mesh/model (passed as parameter)
* before creating the collider, so collider will be spawn in same position
*
* @param {[THREE.Vector3]} trajectory_points3D_ array of Vector3 points to move along the bullet
* @param {THREE.Vector3} start_pos_ initial position to spawn model and collider
*/
function ModelBullet(trajectory_points3D_, start_pos_) {
this.trajectory_points3D = trajectory_points3D_
if (undefined === this.trajectory_points3D) {
console.error("ModelBullet: 'trajectory_points' is undefined");
return;
}
if (this.trajectory_points3D.size == 0) {
console.error("ModelBullet: 'trajectory_points' has 0 elements");
return;
}
if (undefined === start_pos_) {
console.error("ModelBullet: 'start_pos' is undefined");
return;
}
// 1. Call parent object
GPT_Model.call(this);
this.mesh.position.set(start_pos_.x, start_pos_.y, start_pos_.z);
// Attach collider once mesh is built and set in intial postion
this.collider = new GPT_ModelCollider(false, this.mesh);
// initialization
this.current_point_index = 0;
this.prev_ts = performance.now();
}
// 2. Extend from parent object prototype (keep proto clean)
ModelBullet.prototype = Object.create(GPT_Model.prototype);
// 3. Repair the inherited constructor
ModelBullet.prototype.constructor = ModelBullet;
/**
* Overriding it
*/
ModelBullet.prototype.get_geometry = function () {
const _geom = new THREE.SphereGeometry(22, 8, 8);
return _geom;
}
/**
* Overriding it
*/
ModelBullet.prototype.get_material = function () {
const _mat = new THREE.MeshPhongMaterial(
{
color: 0xD76009,
emissive: 0x681F77,
flatShading: true,
specular: 0xFF5733,
shininess: 50,
side: THREE.FrontSide,
transparent: false
}
);
return _mat;
}
/**
* Extending method (calling parent method and performing pre / post operations)
*/
ModelBullet.prototype.dispose_buffers = function () {
GPT_Model.prototype.dispose_buffers.call(this);
this.collider.dispose_buffers();
console.log("ModelBullet: dispose_buffers()");
}
/**
* Increases "current_point_index" and moves mesh to next point
* @returns {Bool} false when index >= last_index, true otherwise
*/
ModelBullet.prototype.move_to_next_point = function () {
const _last_index = this.trajectory_points3D.length - 1;
if (this.current_point_index >= _last_index) {
return false;
}
this.current_point_index++;
const _p = this.trajectory_points3D[this.current_point_index];
this.mesh.position.set(_p.x, _p.y, _p.z);
// update collider
this.collider.update_aabb();
return true;
}
/**
* Based on time passed since last call and Common.BULLET_STEP_DURATION_MS (duration
* between 2 succesive points3D)
*
* While elapsed < BULLET_SEPT_DURATION_MS it calculates interpolated point and moves the object
* Otherwise it only increases the current_point_index
*
* @returns {Bool} true object moved, false otherwise
*/
ModelBullet.prototype.move_to_next_point_interpolated = function () {
const _last_index = this.trajectory_points3D.length - 1;
if (this.current_point_index >= _last_index) {
return false;
}
const _now_ts = performance.now();
const _elapsed = _now_ts - this.prev_ts;
if (_elapsed < Common.BULLET_STEP_DURATION_MS) {
const _i = _elapsed / Common.BULLET_STEP_DURATION_MS;
const _p = this.trajectory_points3D[this.current_point_index];
const _p_next = this.trajectory_points3D[this.current_point_index + 1];
// interpolate coordinates between current and next
const _ip_x = lerp(_p.x, _p_next.x, _i);
const _ip_y = lerp(_p.y, _p_next.y, _i);
const _ip_z = lerp(_p.z, _p_next.z, _i);
// apply
this.mesh.position.set(_ip_x, _ip_y, _ip_z);
// update collider
this.collider.update_aabb();
return true;
}
else {
this.current_point_index++;
this.prev_ts = performance.now();
return false;
}
}
export default ModelBullet;