-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.html
148 lines (124 loc) · 3.84 KB
/
test.html
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
<!DOCTYPE html>
<body>
<div id="mingine-root"></div>
</body>
<script type="module">
import * as mg from "./dist/JavaScript.js"
window.mg = mg;
function createTearDrop(o) {
o.collider = !o.collide
? mg.createColliderNull()
: mg.createColliderComposite(
mg.createColliderCircle(o.radius, mg.vo()),
mg.createColliderRect(mg.vo(), mg.v(o.radius, o.radius)),
);
o.blOffset = mg.v(o.radius, o.radius);
return mg.createObject(o);
}
const renderScale = 100;
let collidedThisFrame = false;
const engine = mg.createEngine(mg.createScene({
scale: renderScale,
rootStyles: {border: "1px solid gray"},
canvasSize: mg.v(2, 2),
postTickHooks: [
// box collision test
([s, _t]) => {
const objects = s.getObjects();
const bouncingBall = objects.find(o => o.id === "BOUNCING_BALL");
const rotatingRect = objects.find(o => o.id === "ROTATING_RECT");
collidedThisFrame ||= engine.queryCollision(bouncingBall, rotatingRect);
}
],
postFrameHooks: [
([s]) => {
const objects = s.getObjects();
const rotatingRect = objects.find(o => o.id === "ROTATING_RECT");
rotatingRect.styles.borderColor = collidedThisFrame ? "red" : "";
collidedThisFrame = false;
}
],
// floor
worldColliders: [ mg.createColliderPlane(mg.v(1, 0)) ],
objects: [
mg.createRect({
id: "ROTATING_RECT",
width: 0.4,
height: 0.3,
styles: {
// TODO: renderer system to handle (some!) styles
width: `${0.4 * renderScale}px`,
height: `${0.3 * renderScale}px`,
border: "1px solid black"
},
collide: true,
mass: Infinity,//1,
angVelocity: 0.75,
angle: 0.85,
pos: mg.v(1, 0.75),
forces: [
mg.forceModels.spring(7, mg.v(1, 0.75), mg.vo()),
],
eventHandlers: [
([type, event, gObj]) => {
if (type !== "click") return;
gObj.styles.background = "rgba(255, 255, 0, 128)";
setTimeout(() => {
gObj.styles.background = "";
}, 500);
}
]
}),
createTearDrop({
id: "BOUNCING_BALL",
radius: 0.1,
styles: {
width: `${0.2 * renderScale}px`,
height: `${0.2 * renderScale}px`,
border: "1px solid black",
borderTop: "1px solid red",
borderRight: "1px solid red",
borderRadius: "999px 0 999px 999px"
},
collide: true,
mass: 0.5,
// mass^2 * area
momentOfInertia: (0.5 * 0.5) * (0.1 * 0.1),
pos: mg.v(0, 1.8),
restitutionCoeff: 0.6,
forces: [
mg.forceModels.weight(mg.consts.earthGravity),
mg.forceModels.spring(5.5, mg.v(1, 1.5), mg.v(0.1, 0.1)),
mg.forceModels.stillAirDrag(mg.consts.earthAirDensity, Math.PI * 0.1 * 0.1, 0.47),
mg.forceModels.simpleDamping(0.25, 0.05)
]
}),
/*mg.createRect({
id: "FALLING_RECT",
width: 0.4,
height: 0.3,
styles: {
// TODO: renderer system to handle (some!) styles
width: `${0.4 * renderScale}px`,
height: `${0.3 * renderScale}px`,
border: "1px solid black"
},
collide: true,
mass: 1,
angVelocity: 0.75,
angle: 1.1,
pos: mg.v(1, 0.75),
restitutionCoeff: 0.6,
forces: [
mg.forceModels.weight(mg.consts.earthGravity),
mg.forceModels.spring(50, mg.v(1, 1), mg.v(0.2, 0.15)),
mg.forceModels.simpleDamping(0.25, 0.05)
]
})*/
]
}));
const root = document.getElementById("mingine-root");
engine.mount(root);
engine.start();
window.engine = engine;
</script>