-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathDerivedObject.js
More file actions
90 lines (60 loc) · 1.68 KB
/
DerivedObject.js
File metadata and controls
90 lines (60 loc) · 1.68 KB
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
//@EliasHasle
/*
Depends on JSONSpecObject.js
*/
function DerivedObject( specification, baseObjects ) {
this.baseObjects = baseObjects;
JSONSpecObject.call( this, specification );
}
DerivedObject.prototype = Object.create( JSONSpecObject.prototype );
Object.assign( DerivedObject.prototype, {
constructor: DerivedObject,
setFromSpecification: function ( spec ) {
this.id = spec.id;
this.group = spec.group || null;
this.affiliations = spec.affiliations;
if ( typeof spec.baseObject === "string" ) {
this.baseObject = this.baseObjects[ spec.baseObject ];
} else {
this.baseObject = new BaseObject( spec.baseObject );
}
this.referenceState = spec.referenceState;
//this.referenceStateVersion = 0;
this.style = spec.style || {};
return this;
},
getSpecification: function () {
let spec = {
id: this.id,
group: this.group,
affiliations: this.affiliations,
referenceState: this.referenceState,
style: this.style
};
if ( this.baseObjects[ this.baseObject.id ] !== undefined ) {
spec.baseObject = this.baseObject.id;
} else {
spec.baseObject = this.baseObject.getSpecification();
}
return spec;
},
getWeight: function ( state ) {
let oState = state.getObjectState( this );
//Support disabled objects:
if ( oState.exists === false ) {
return { mass: 0, cg: { x: 0, y: 0, z: 0 } };
}
let p = {
x: oState.xCentre,
y: oState.yCentre,
z: oState.zBase
};
let w = this.baseObject.getWeight( oState.fullness );
let m = w.mass;
let cg = Vectors.add( p, w.cg );
if ( isNaN( cg.x + cg.y + cg.z ) ) {
console.error( "DerivedObject.getWeight: returning NaN values." );
}
return { mass: m, cg: cg };
}
} );