-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcube-impulse.js
More file actions
158 lines (135 loc) · 4.7 KB
/
cube-impulse.js
File metadata and controls
158 lines (135 loc) · 4.7 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
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
document.addEventListener( "DOMContentLoaded", function( e ) {
require.config({
baseUrl: "../.."
});
require(
[ "gladius-core",
"gladius-cubicvr",
"gladius-box2d"],
function( Gladius, cubicvrExtension, box2dExtension ) {
var engine = new Gladius();
// Engine monitor setup
function monitor( engine ) {
debugger;
engine.detach( monitor );
}
document.addEventListener( "keydown", function( event ) {
var code = event.which || event.keyCode;
if( code === 0x4D && event.ctrlKey && event.altKey ) {
engine.attach( monitor );
}
});
var cubicvrOptions = {
renderer: {
canvas: document.getElementById( "test-canvas" )
}
};
engine.registerExtension( cubicvrExtension, cubicvrOptions );
engine.registerExtension( box2dExtension );
var resources = {};
var materialArgs = '?colorTexture=../assets/images/cube-impulse-diffuse.jpg' +
'&bumpTexture=../assets/images/cube-impulse-bump.jpg' +
'&normalTexture=../assets/images/cube-impulse-normal.jpg';
engine.get(
[
{
type: engine["gladius-cubicvr"].Mesh,
url: '../assets/procedural-mesh.js',
load: engine.loaders.procedural,
onsuccess: function( mesh ) {
resources.mesh = mesh;
},
onfailure: function( error ) {
}
},
{
type: engine["gladius-cubicvr"].MaterialDefinition,
url: '../assets/procedural-material.js' + materialArgs,
load: engine.loaders.procedural,
onsuccess: function( material ) {
resources.material = material;
},
onfailure: function( error ) {
}
}
],
{
oncomplete: game.bind( null, engine, resources )
}
);
});
function game( engine, resources ) {
var space = new engine.SimulationSpace();
var cubicvr = engine.findExtension( "gladius-cubicvr" );
var box2d = engine.findExtension( "gladius-box2d" );
var lightDefinition = new cubicvr.LightDefinition({
intensity: 1,
light_type: cubicvr.LightDefinition.LightTypes.POINT,
method: cubicvr.LightDefinition.LightingMethods.DYNAMIC
});
space.add( new engine.Entity( "camera",
[
new engine.core.Transform( [0, 0, 5] ),
new cubicvr.Camera( {
targeted:false
} )
]
));
space.add( new engine.Entity( "light",
[
new engine.core.Transform( [0, 0, -1] ),
new cubicvr.Light( lightDefinition )
]
));
space.add(new engine.Entity( "gravity",
[
new box2d.Force({force:[0,-1], forceType:box2d.Force.ForceTypes.GLOBAL})
]
));
var bodyDefinition = new box2d.BodyDefinition();
var fixtureDefinition = new box2d.FixtureDefinition({shape:new box2d.BoxShape()});
var parentCube = new engine.Entity( "cube",
[
new engine.core.Transform( [0, 0, -6], [0, 0, 0] ),
new box2d.Body({bodyDefinition: bodyDefinition, fixtureDefinition: fixtureDefinition}),
new cubicvr.Model( resources.mesh, resources.material )
]
);
space.add( parentCube );
var task = new engine.FunctionTask( function() {
function getRandom(min, max)
{
return Math.random() * (max - min) + min;
}
var cubePosition = new engine.math.Vector3( space.findNamed( "cube").findComponent( "Transform").position);
// var camera = space.findNamed("camera").findComponent("Camera");
// camera.setTarget(cubePosition);
var cubeY = cubePosition.y;
var cubeX = cubePosition.x;
if (cubeY < -1.5){
var impEvent = new engine.Event('LinearImpulse',{impulse: [getRandom(0,0.1), getRandom(0,1)]});
impEvent.dispatch(parentCube);
}
if (cubeY > 1.5){
var impEvent = new engine.Event('LinearImpulse',{impulse: [getRandom(0,0.1) * -1, getRandom(0, 1) * -1]});
impEvent.dispatch(parentCube);
}
if (cubeX < -1.5){
var impEvent = new engine.Event('LinearImpulse',{impulse: [getRandom(0,1), 0]});
impEvent.dispatch(parentCube);
var angEvent = new engine.Event('AngularImpulse',{impulse: getRandom(0, 0.1)});
angEvent.dispatch(parentCube);
}
if (cubeX > 1.5){
var impEvent = new engine.Event('LinearImpulse',{impulse: [getRandom(0, 1) * -1, 0]});
impEvent.dispatch(parentCube);
var angEvent = new engine.Event('AngularImpulse',{impulse: getRandom(0, 0.1)});
angEvent.dispatch(parentCube);
}
}, {
tags: ["@update"]
});
task.start();
engine.resume();
}
});