Create script.js
Browse files
script.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// モジュールの設定
|
| 2 |
+
const { Engine, Render, Runner, Bodies, World, Events } = Matter;
|
| 3 |
+
|
| 4 |
+
// エンジンとワールドの作成
|
| 5 |
+
const engine = Engine.create();
|
| 6 |
+
const world = engine.world;
|
| 7 |
+
|
| 8 |
+
// レンダラの作成
|
| 9 |
+
const render = Render.create({
|
| 10 |
+
element: document.body,
|
| 11 |
+
engine: engine,
|
| 12 |
+
canvas: document.getElementById('world'),
|
| 13 |
+
options: {
|
| 14 |
+
width: window.innerWidth,
|
| 15 |
+
height: window.innerHeight,
|
| 16 |
+
wireframes: false
|
| 17 |
+
}
|
| 18 |
+
});
|
| 19 |
+
|
| 20 |
+
Render.run(render);
|
| 21 |
+
const runner = Runner.create();
|
| 22 |
+
Runner.run(runner, engine);
|
| 23 |
+
|
| 24 |
+
// シミュレーションに必要な変数
|
| 25 |
+
let ball = null;
|
| 26 |
+
let vxData = [];
|
| 27 |
+
let vyData = [];
|
| 28 |
+
let xData = [];
|
| 29 |
+
let tData = [];
|
| 30 |
+
let startTime = null;
|
| 31 |
+
|
| 32 |
+
// クリックイベントで小球を生成
|
| 33 |
+
document.addEventListener('click', (event) => {
|
| 34 |
+
if (ball) {
|
| 35 |
+
World.remove(world, ball);
|
| 36 |
+
plotGraphs();
|
| 37 |
+
}
|
| 38 |
+
const x = event.clientX;
|
| 39 |
+
const y = event.clientY;
|
| 40 |
+
const velocityX = Math.random() * 10 - 5; // -5から5のランダムな速度
|
| 41 |
+
const velocityY = Math.random() * 10 - 5;
|
| 42 |
+
ball = Bodies.circle(x, y, 20, { restitution: 0.8 });
|
| 43 |
+
Matter.Body.setVelocity(ball, { x: velocityX, y: velocityY });
|
| 44 |
+
World.add(world, ball);
|
| 45 |
+
|
| 46 |
+
// データの初期化
|
| 47 |
+
vxData = [];
|
| 48 |
+
vyData = [];
|
| 49 |
+
xData = [];
|
| 50 |
+
tData = [];
|
| 51 |
+
startTime = new Date().getTime();
|
| 52 |
+
});
|
| 53 |
+
|
| 54 |
+
// シミュレーションのアップデート
|
| 55 |
+
Events.on(engine, 'beforeUpdate', () => {
|
| 56 |
+
if (ball) {
|
| 57 |
+
const elapsedTime = (new Date().getTime() - startTime) / 1000;
|
| 58 |
+
vxData.push(ball.velocity.x);
|
| 59 |
+
vyData.push(ball.velocity.y);
|
| 60 |
+
xData.push(ball.position.x);
|
| 61 |
+
tData.push(elapsedTime);
|
| 62 |
+
|
| 63 |
+
// 画面外に出た場合、ボールを削除
|
| 64 |
+
if (ball.position.x < 0 || ball.position.x > window.innerWidth ||
|
| 65 |
+
ball.position.y < 0 || ball.position.y > window.innerHeight) {
|
| 66 |
+
World.remove(world, ball);
|
| 67 |
+
ball = null;
|
| 68 |
+
plotGraphs();
|
| 69 |
+
}
|
| 70 |
+
}
|
| 71 |
+
});
|
| 72 |
+
|
| 73 |
+
// グラフ描画の関数
|
| 74 |
+
function plotGraphs() {
|
| 75 |
+
plotChart('vxChart', 'Velocity-X vs Time', tData, vxData);
|
| 76 |
+
plotChart('vyChart', 'Velocity-Y vs Time', tData, vyData);
|
| 77 |
+
plotChart('xtChart', 'Position-X vs Time', tData, xData);
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
function plotChart(canvasId, label, xData, yData) {
|
| 81 |
+
const ctx = document.getElementById(canvasId).getContext('2d');
|
| 82 |
+
new Chart(ctx, {
|
| 83 |
+
type: 'line',
|
| 84 |
+
data: {
|
| 85 |
+
labels: xData,
|
| 86 |
+
datasets: [{
|
| 87 |
+
label: label,
|
| 88 |
+
data: yData,
|
| 89 |
+
borderColor: 'rgba(75, 192, 192, 1)',
|
| 90 |
+
borderWidth: 1,
|
| 91 |
+
fill: false
|
| 92 |
+
}]
|
| 93 |
+
},
|
| 94 |
+
options: {
|
| 95 |
+
scales: {
|
| 96 |
+
x: {
|
| 97 |
+
beginAtZero: true
|
| 98 |
+
},
|
| 99 |
+
y: {
|
| 100 |
+
beginAtZero: true
|
| 101 |
+
}
|
| 102 |
+
}
|
| 103 |
+
}
|
| 104 |
+
});
|
| 105 |
+
}
|