File size: 28,815 Bytes
feed137
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0782320
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Physics Playground</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.0/chart.min.js"></script>
    <style>
        .car-container {
            transition: transform 0.2s ease;
        }
        .car-dragging {
            cursor: grabbing !important;
            z-index: 10;
        }
        .track-line {
            stroke: #4b5563;
            stroke-width: 2;
            stroke-dasharray: 5,5;
        }
        .car-shadow {
            filter: drop-shadow(0 4px 3px rgba(0,0,0,0.07)) drop-shadow(0 2px 2px rgba(0,0,0,0.06));
        }
    </style>
</head>
<body class="bg-gradient-to-br from-indigo-50 to-purple-100 min-h-screen">
    <div class="container mx-auto px-4 py-8">
        <header class="text-center mb-12">
            <h1 class="text-4xl md:text-5xl font-bold text-gray-800 mb-3">Physics Playground 🚗</h1>
            <p class="text-lg text-gray-600 max-w-2xl mx-auto">Drag the cars to change their positions or velocities. Watch as real-time graphs update to show position, velocity, and acceleration over time.</p>
        </header>

        <div class="grid grid-cols-1 lg:grid-cols-3 gap-8 mb-12">
            <!-- Simulation Canvas -->
            <div class="lg:col-span-2 bg-white rounded-2xl shadow-xl p-6">
                <div class="flex justify-between items-center mb-4">
                    <h2 class="text-xl font-bold text-gray-800">Simulation</h2>
                    <div class="flex space-x-2">
                        <button id="resetBtn" class="px-4 py-2 bg-indigo-100 text-indigo-700 rounded-lg hover:bg-indigo-200 transition flex items-center">
                            <i data-feather="refresh-ccw" class="mr-2"></i> Reset
                        </button>
                        <button id="playPauseBtn" class="px-4 py-2 bg-green-100 text-green-700 rounded-lg hover:bg-green-200 transition flex items-center">
                            <i data-feather="pause" class="mr-2"></i> Pause
                        </button>
                    </div>
                </div>
                
                <div id="simulationCanvas" class="relative h-64 bg-gradient-to-r from-blue-50 to-indigo-50 rounded-xl border-2 border-dashed border-gray-200 overflow-hidden">
                    <!-- Track -->
                    <div class="absolute top-1/2 left-0 right-0 h-1 bg-gray-300 transform -translate-y-1/2"></div>
                    
                    <!-- Distance markers -->
                    <div class="absolute top-1/2 left-0 transform -translate-y-1/2 -translate-x-1/2">
                        <div class="text-xs text-gray-500">0m</div>
                    </div>
                    <div class="absolute top-1/2 left-1/4 transform -translate-y-1/2 -translate-x-1/2">
                        <div class="text-xs text-gray-500">25m</div>
                    </div>
                    <div class="absolute top-1/2 left-1/2 transform -translate-y-1/2 -translate-x-1/2">
                        <div class="text-xs text-gray-500">50m</div>
                    </div>
                    <div class="absolute top-1/2 left-3/4 transform -translate-y-1/2 -translate-x-1/2">
                        <div class="text-xs text-gray-500">75m</div>
                    </div>
                    <div class="absolute top-1/2 right-0 transform -translate-y-1/2 translate-x-1/2">
                        <div class="text-xs text-gray-500">100m</div>
                    </div>
                    
                    <!-- Cars -->
                    <div id="car1" class="car-container absolute top-1/2 cursor-grab active:cursor-grabbing transform -translate-y-1/2" style="left: 20%;">
                        <div class="car-shadow">
                            <svg width="60" height="30" viewBox="0 0 60 30">
                                <rect x="5" y="8" width="50" height="14" rx="7" fill="#3b82f6"/>
                                <circle cx="15" cy="24" r="5" fill="#1e40af"/>
                                <circle cx="45" cy="24" r="5" fill="#1e40af"/>
                                <rect x="0" y="10" width="10" height="10" rx="2" fill="#3b82f6"/>
                                <rect x="50" y="10" width="10" height="10" rx="2" fill="#3b82f6"/>
                            </svg>
                        </div>
                        <div class="text-center mt-2 text-sm font-medium text-blue-600">Car 1</div>
                    </div>
                    
                    <div id="car2" class="car-container absolute top-1/2 cursor-grab active:cursor-grabbing transform -translate-y-1/2" style="left: 40%;">
                        <div class="car-shadow">
                            <svg width="60" height="30" viewBox="0 0 60 30">
                                <rect x="5" y="8" width="50" height="14" rx="7" fill="#ef4444"/>
                                <circle cx="15" cy="24" r="5" fill="#b91c1c"/>
                                <circle cx="45" cy="24" r="5" fill="#b91c1c"/>
                                <rect x="0" y="10" width="10" height="10" rx="2" fill="#ef4444"/>
                                <rect x="50" y="10" width="10" height="10" rx="2" fill="#ef4444"/>
                            </svg>
                        </div>
                        <div class="text-center mt-2 text-sm font-medium text-red-600">Car 2</div>
                    </div>
                </div>
                
                <div class="mt-6 grid grid-cols-1 md:grid-cols-2 gap-4">
                    <div class="bg-blue-50 p-4 rounded-lg">
                        <h3 class="font-bold text-blue-800 mb-2">Car 1 Controls</h3>
                        <div class="flex items-center space-x-4">
                            <div>
                                <label class="text-sm text-gray-600">Position (m)</label>
                                <input type="range" id="position1" min="0" max="100" value="20" class="w-full">
                                <div class="text-center text-blue-700 font-mono" id="position1Value">20m</div>
                            </div>
                            <div>
                                <label class="text-sm text-gray-600">Velocity (m/s)</label>
                                <input type="range" id="velocity1" min="-10" max="10" value="3" class="w-full">
                                <div class="text-center text-blue-700 font-mono" id="velocity1Value">3m/s</div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="bg-red-50 p-4 rounded-lg">
                        <h3 class="font-bold text-red-800 mb-2">Car 2 Controls</h3>
                        <div class="flex items-center space-x-4">
                            <div>
                                <label class="text-sm text-gray-600">Position (m)</label>
                                <input type="range" id="position2" min="0" max="100" value="40" class="w-full">
                                <div class="text-center text-red-700 font-mono" id="position2Value">40m</div>
                            </div>
                            <div>
                                <label class="text-sm text-gray-600">Velocity (m/s)</label>
                                <input type="range" id="velocity2" min="-10" max="10" value="-2" class="w-full">
                                <div class="text-center text-red-700 font-mono" id="velocity2Value">-2m/s</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- Data Panel -->
            <div class="bg-white rounded-2xl shadow-xl p-6">
                <h2 class="text-xl font-bold text-gray-800 mb-4">Current Data</h2>
                
                <div class="space-y-4">
                    <div class="bg-blue-50 p-4 rounded-lg">
                        <div class="flex justify-between items-center">
                            <h3 class="font-bold text-blue-800">Car 1</h3>
                            <div class="flex space-x-2">
                                <span class="px-2 py-1 bg-blue-100 text-blue-800 rounded text-xs">v = <span id="car1Velocity">3</span> m/s</span>
                                <span class="px-2 py-1 bg-blue-100 text-blue-800 rounded text-xs">a = <span id="car1Acceleration">0</span> m/s²</span>
                            </div>
                        </div>
                        <div class="mt-2">
                            <div class="text-sm text-gray-600">Position: <span id="car1Position" class="font-mono">20</span> m</div>
                            <div class="w-full bg-gray-200 rounded-full h-2 mt-1">
                                <div id="car1Progress" class="bg-blue-600 h-2 rounded-full" style="width: 20%"></div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="bg-red-50 p-4 rounded-lg">
                        <div class="flex justify-between items-center">
                            <h3 class="font-bold text-red-800">Car 2</h3>
                            <div class="flex space-x-2">
                                <span class="px-2 py-1 bg-red-100 text-red-800 rounded text-xs">v = <span id="car2Velocity">-2</span> m/s</span>
                                <span class="px-2 py-1 bg-red-100 text-red-800 rounded text-xs">a = <span id="car2Acceleration">0</span> m/s²</span>
                            </div>
                        </div>
                        <div class="mt-2">
                            <div class="text-sm text-gray-600">Position: <span id="car2Position" class="font-mono">40</span> m</div>
                            <div class="w-full bg-gray-200 rounded-full h-2 mt-1">
                                <div id="car2Progress" class="bg-red-600 h-2 rounded-full" style="width: 40%"></div>
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="mt-6 bg-gray-50 p-4 rounded-lg">
                    <h3 class="font-bold text-gray-800 mb-2">Simulation Controls</h3>
                    <div class="grid grid-cols-2 gap-3">
                        <button id="accelerate1" class="px-3 py-2 bg-blue-100 text-blue-700 rounded hover:bg-blue-200 transition text-sm">Accelerate Car 1</button>
                        <button id="decelerate1" class="px-3 py-2 bg-blue-100 text-blue-700 rounded hover:bg-blue-200 transition text-sm">Decelerate Car 1</button>
                        <button id="accelerate2" class="px-3 py-2 bg-red-100 text-red-700 rounded hover:bg-red-200 transition text-sm">Accelerate Car 2</button>
                        <button id="decelerate2" class="px-3 py-2 bg-red-100 text-red-700 rounded hover:bg-red-200 transition text-sm">Decelerate Car 2</button>
                    </div>
                </div>
                
                <div class="mt-6">
                    <h3 class="font-bold text-gray-800 mb-2">Physics Concepts</h3>
                    <ul class="text-sm text-gray-600 space-y-1">
                        <li class="flex items-start">
                            <span class="text-green-500 mr-2"></span>
                            <span>Position changes with velocity over time</span>
                        </li>
                        <li class="flex items-start">
                            <span class="text-green-500 mr-2"></span>
                            <span>Velocity changes with acceleration</span>
                        </li>
                        <li class="flex items-start">
                            <span class="text-green-500 mr-2"></span>
                            <span>Negative velocity means moving left</span>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
        
        <!-- Graphs Section -->
        <div class="bg-white rounded-2xl shadow-xl p-6 mb-12">
            <h2 class="text-xl font-bold text-gray-800 mb-6">Real-time Physics Graphs</h2>
            
            <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
                <div class="bg-gray-50 p-4 rounded-lg">
                    <h3 class="font-bold text-gray-800 mb-4 text-center">Position vs Time</h3>
                    <canvas id="positionChart"></canvas>
                </div>
                
                <div class="bg-gray-50 p-4 rounded-lg">
                    <h3 class="font-bold text-gray-800 mb-4 text-center">Velocity vs Time</h3>
                    <canvas id="velocityChart"></canvas>
                </div>
                
                <div class="bg-gray-50 p-4 rounded-lg">
                    <h3 class="font-bold text-gray-800 mb-4 text-center">Acceleration vs Time</h3>
                    <canvas id="accelerationChart"></canvas>
                </div>
            </div>
        </div>
        
        <footer class="text-center text-gray-600 text-sm py-6">
            <p>Drag cars to change their positions or use controls to adjust velocities. Watch how physics laws govern motion!</p>
        </footer>
    </div>

    <script>
        // Initialize Feather Icons
        feather.replace();
        
        // Simulation state
        const state = {
            cars: [
                { id: 'car1', position: 20, velocity: 3, acceleration: 0, color: '#3b82f6' },
                { id: 'car2', position: 40, velocity: -2, acceleration: 0, color: '#ef4444' }
            ],
            isRunning: true,
            lastTime: 0,
            dataPoints: {
                position: [[0, 20], [0, 40]],
                velocity: [[0, 3], [0, -2]],
                acceleration: [[0, 0], [0, 0]]
            }
        };
        
        // DOM Elements
        const elements = {
            car1: document.getElementById('car1'),
            car2: document.getElementById('car2'),
            playPauseBtn: document.getElementById('playPauseBtn'),
            resetBtn: document.getElementById('resetBtn'),
            position1: document.getElementById('position1'),
            position2: document.getElementById('position2'),
            velocity1: document.getElementById('velocity1'),
            velocity2: document.getElementById('velocity2'),
            accelerate1: document.getElementById('accelerate1'),
            decelerate1: document.getElementById('decelerate1'),
            accelerate2: document.getElementById('accelerate2'),
            decelerate2: document.getElementById('decelerate2')
        };
        
        // Initialize Charts
        const positionCtx = document.getElementById('positionChart').getContext('2d');
        const velocityCtx = document.getElementById('velocityChart').getContext('2d');
        const accelerationCtx = document.getElementById('accelerationChart').getContext('2d');
        
        const positionChart = new Chart(positionCtx, {
            type: 'line',
            data: {
                labels: [0],
                datasets: [{
                    label: 'Car 1',
                    data: [20],
                    borderColor: '#3b82f6',
                    backgroundColor: 'rgba(59, 130, 246, 0.1)',
                    tension: 0.4,
                    fill: false
                }, {
                    label: 'Car 2',
                    data: [40],
                    borderColor: '#ef4444',
                    backgroundColor: 'rgba(239, 68, 68, 0.1)',
                    tension: 0.4,
                    fill: false
                }]
            },
            options: {
                responsive: true,
                maintainAspectRatio: true,
                plugins: {
                    legend: { position: 'top' }
                },
                scales: {
                    x: { title: { display: true, text: 'Time (s)' } },
                    y: { title: { display: true, text: 'Position (m)' } }
                }
            }
        });
        
        const velocityChart = new Chart(velocityCtx, {
            type: 'line',
            data: {
                labels: [0],
                datasets: [{
                    label: 'Car 1',
                    data: [3],
                    borderColor: '#3b82f6',
                    backgroundColor: 'rgba(59, 130, 246, 0.1)',
                    tension: 0.4,
                    fill: false
                }, {
                    label: 'Car 2',
                    data: [-2],
                    borderColor: '#ef4444',
                    backgroundColor: 'rgba(239, 68, 68, 0.1)',
                    tension: 0.4,
                    fill: false
                }]
            },
            options: {
                responsive: true,
                maintainAspectRatio: true,
                plugins: {
                    legend: { position: 'top' }
                },
                scales: {
                    x: { title: { display: true, text: 'Time (s)' } },
                    y: { title: { display: true, text: 'Velocity (m/s)' } }
                }
            }
        });
        
        const accelerationChart = new Chart(accelerationCtx, {
            type: 'line',
            data: {
                labels: [0],
                datasets: [{
                    label: 'Car 1',
                    data: [0],
                    borderColor: '#3b82f6',
                    backgroundColor: 'rgba(59, 130, 246, 0.1)',
                    tension: 0.4,
                    fill: false
                }, {
                    label: 'Car 2',
                    data: [0],
                    borderColor: '#ef4444',
                    backgroundColor: 'rgba(239, 68, 68, 0.1)',
                    tension: 0.4,
                    fill: false
                }]
            },
            options: {
                responsive: true,
                maintainAspectRatio: true,
                plugins: {
                    legend: { position: 'top' }
                },
                scales: {
                    x: { title: { display: true, text: 'Time (s)' } },
                    y: { title: { display: true, text: 'Acceleration (m/s²)' } }
                }
            }
        });
        
        // Drag functionality for cars
        function initDrag(carElement, carIndex) {
            let isDragging = false;
            let offsetX, offsetY;
            
            carElement.addEventListener('mousedown', (e) => {
                isDragging = true;
                carElement.classList.add('car-dragging');
                offsetX = e.clientX - carElement.getBoundingClientRect().left;
                offsetY = e.clientY - carElement.getBoundingClientRect().top;
                e.preventDefault();
            });
            
            document.addEventListener('mousemove', (e) => {
                if (!isDragging) return;
                
                const canvasRect = document.getElementById('simulationCanvas').getBoundingClientRect();
                const x = e.clientX - canvasRect.left - offsetX;
                const position = Math.max(0, Math.min(100, (x / canvasRect.width) * 100));
                
                state.cars[carIndex].position = position;
                updateCarPosition(carIndex);
                updateUI();
            });
            
            document.addEventListener('mouseup', () => {
                if (isDragging) {
                    isDragging = false;
                    carElement.classList.remove('car-dragging');
                }
            });
        }
        
        // Update car position on canvas
        function updateCarPosition(carIndex) {
            const car = state.cars[carIndex];
            const carElement = document.getElementById(car.id);
            carElement.style.left = `${car.position}%`;
        }
        
        // Update UI elements
        function updateUI() {
            // Update car data displays
            document.getElementById('car1Position').textContent = Math.round(state.cars[0].position);
            document.getElementById('car1Velocity').textContent = state.cars[0].velocity.toFixed(1);
            document.getElementById('car1Acceleration').textContent = state.cars[0].acceleration.toFixed(1);
            document.getElementById('car1Progress').style.width = `${state.cars[0].position}%`;
            
            document.getElementById('car2Position').textContent = Math.round(state.cars[1].position);
            document.getElementById('car2Velocity').textContent = state.cars[1].velocity.toFixed(1);
            document.getElementById('car2Acceleration').textContent = state.cars[1].acceleration.toFixed(1);
            document.getElementById('car2Progress').style.width = `${state.cars[1].position}%`;
            
            // Update sliders
            elements.position1.value = state.cars[0].position;
            elements.velocity1.value = state.cars[0].velocity;
            elements.position2.value = state.cars[1].position;
            elements.velocity2.value = state.cars[1].velocity;
            
            // Update slider value displays
            document.getElementById('position1Value').textContent = `${Math.round(state.cars[0].position)}m`;
            document.getElementById('velocity1Value').textContent = `${state.cars[0].velocity}m/s`;
            document.getElementById('position2Value').textContent = `${Math.round(state.cars[1].position)}m`;
            document.getElementById('velocity2Value').textContent = `${state.cars[1].velocity}m/s`;
        }
        
        // Update charts with new data
        function updateCharts(time) {
            // Add new data points
            positionChart.data.labels.push(time.toFixed(1));
            velocityChart.data.labels.push(time.toFixed(1));
            accelerationChart.data.labels.push(time.toFixed(1));
            
            positionChart.data.datasets[0].data.push(state.cars[0].position);
            positionChart.data.datasets[1].data.push(state.cars[1].position);
            
            velocityChart.data.datasets[0].data.push(state.cars[0].velocity);
            velocityChart.data.datasets[1].data.push(state.cars[1].velocity);
            
            accelerationChart.data.datasets[0].data.push(state.cars[0].acceleration);
            accelerationChart.data.datasets[1].data.push(state.cars[1].acceleration);
            
            // Limit data points to last 20
            if (positionChart.data.labels.length > 20) {
                positionChart.data.labels.shift();
                velocityChart.data.labels.shift();
                accelerationChart.data.labels.shift();
                
                positionChart.data.datasets[0].data.shift();
                positionChart.data.datasets[1].data.shift();
                velocityChart.data.datasets[0].data.shift();
                velocityChart.data.datasets[1].data.shift();
                accelerationChart.data.datasets[0].data.shift();
                accelerationChart.data.datasets[1].data.shift();
            }
            
            // Update charts
            positionChart.update();
            velocityChart.update();
            accelerationChart.update();
        }
        
        // Physics simulation
        function simulatePhysics(deltaTime) {
            if (!state.isRunning) return;
            
            // Update positions based on velocity
            state.cars.forEach(car => {
                car.position += car.velocity * deltaTime;
                
                // Boundary checks
                if (car.position < 0) {
                    car.position = 0;
                    car.velocity = Math.abs(car.velocity) * 0.5; // Bounce with energy loss
                } else if (car.position > 100) {
                    car.position = 100;
                    car.velocity = -Math.abs(car.velocity) * 0.5; // Bounce with energy loss
                }
            });
            
            // Update UI
            updateCarPosition(0);
            updateCarPosition(1);
            updateUI();
        }
        
        // Animation loop
        function animationLoop(timestamp) {
            if (!state.lastTime) state.lastTime = timestamp;
            const deltaTime = (timestamp - state.lastTime) / 1000; // Convert to seconds
            state.lastTime = timestamp;
            
            simulatePhysics(deltaTime);
            
            // Update charts every 0.5 seconds
            if (timestamp % 500 < 20) { // Roughly every 0.5 seconds
                const time = timestamp / 1000;
                updateCharts(time);
            }
            
            requestAnimationFrame(animationLoop);
        }
        
        // Event Listeners
        elements.playPauseBtn.addEventListener('click', () => {
            state.isRunning = !state.isRunning;
            const icon = elements.playPauseBtn.querySelector('i');
            icon.setAttribute('data-feather', state.isRunning ? 'pause' : 'play');
            feather.replace();
            elements.playPauseBtn.innerHTML = elements.playPauseBtn.innerHTML.replace(
                state.isRunning ? 'Play' : 'Pause', 
                state.isRunning ? 'Pause' : 'Play'
            );
        });
        
        elements.resetBtn.addEventListener('click', () => {
            state.cars[0].position = 20;
            state.cars[0].velocity = 3;
            state.cars[0].acceleration = 0;
            
            state.cars[1].position = 40;
            state.cars[1].velocity = -2;
            state.cars[1].acceleration = 0;
            
            state.lastTime = 0;
            
            updateCarPosition(0);
            updateCarPosition(1);
            updateUI();
            
            // Reset charts
            positionChart.data.labels = [0];
            velocityChart.data.labels = [0];
            accelerationChart.data.labels = [0];
            
            positionChart.data.datasets[0].data = [20];
            positionChart.data.datasets[1].data = [40];
            velocityChart.data.datasets[0].data = [3];
            velocityChart.data.datasets[1].data = [-2];
            accelerationChart.data.datasets[0].data = [0];
            accelerationChart.data.datasets[1].data = [0];
            
            positionChart.update();
            velocityChart.update();
            accelerationChart.update();
        });
        
        // Slider event listeners
        elements.position1.addEventListener('input', () => {
            state.cars[0].position = parseFloat(elements.position1.value);
            updateCarPosition(0);
            updateUI();
        });
        
        elements.velocity1.addEventListener('input', () => {
            state.cars[0].velocity = parseFloat(elements.velocity1.value);
            updateUI();
        });
        
        elements.position2.addEventListener('input', () => {
            state.cars[1].position = parseFloat(elements.position2.value);
            updateCarPosition(1);
            updateUI();
        });
        
        elements.velocity2.addEventListener('input', () => {
            state.cars[1].velocity = parseFloat(elements.velocity2.value);
            updateUI();
        });
        
        // Acceleration controls
        elements.accelerate1.addEventListener('click', () => {
            state.cars[0].acceleration = 1;
            state.cars[0].velocity += 0.5;
            updateUI();
        });
        
        elements.decelerate1.addEventListener('click', () => {
            state.cars[0].acceleration = -1;
            state.cars[0].velocity -= 0.5;
            updateUI();
        });
        
        elements.accelerate2.addEventListener('click', () => {
            state.cars[1].acceleration = 1;
            state.cars[1].velocity += 0.5;
            updateUI();
        });
        
        elements.decelerate2.addEventListener('click', () => {
            state.cars[1].acceleration = -1;
            state.cars[1].velocity -= 0.5;
            updateUI();
        });
        
        // Initialize drag functionality
        initDrag(elements.car1, 0);
        initDrag(elements.car2, 1);
        
        // Start animation loop
        requestAnimationFrame(animationLoop);
        
        // Initial UI update
        updateUI();
    </script>
</body>
</html>