-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
executable file
·812 lines (754 loc) · 38.5 KB
/
Copy pathindex.html
File metadata and controls
executable file
·812 lines (754 loc) · 38.5 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
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
<html>
<head>
<title>Gravity.js Documentation</title>
<script src="https://cdn.jsdelivr.net/npm/@idevgames/keys-js/src/keys.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@idevgames/cursor-js/src/cursor.min.js"></script>
<script src="src/gravity.js"></script>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Bebas+Neue&family=Space+Mono&display=swap" rel="stylesheet" media="print" onload="this.media='all'">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
body{
margin:0px;
padding:0px;
background-color:#060606;
color:#FFF;
font-family: 'Space Mono', monospace;
overflow-x:hidden;
}
hr{
margin:0px;
border-color:#AAAAAA;
}
.pageContainer{
max-width:100%;
overflow:hidden;
}
.container{
background-color:#060606;
width:100%;
min-height:100vh;
position:relative;
margin:auto;
display:flex;
align-items: center;
justify-content: center;
flex-direction: column;
max-width: 1800px;
}
pre{
font-size: 22px;
white-space: pre-line;
background-color: #111;
padding: 20px;
}
.box{
position:relative;
display: inline-block;
width:auto;
margin:auto;
margin:20px;
padding:20px;
max-width: 100%;
}
.boxBG{
background-color: rgba(0, 0, 0, 0.8);
}
.alignCenter{
text-align:center;
}
h1, .gravityText{
font-family: 'Bebas Neue', cursive;
color: #ff4d4d;
text-shadow: 2px 2px 2px #000000;
font-size:72px;
margin:20px;
}
h2{
font-family: 'Bebas Neue', cursive;
color: #FFF;
text-shadow: 2px 2px 2px #000000;
font-size:52px;
}
.listItem{
font-size: 20px;
background-color: #111;
padding: 20px;
display:inline-block;
margin-top:10px;
}
.moreExamples{
display: inline-block;
margin: 10px;
background-color: #F1F1F1;
color: #060606;
font-weight: bold;
padding: 10px 45px;
text-decoration: none;
}
.demo-container{
width:100%;
height:400px;
position:relative;
background: linear-gradient(180deg, #1a1a2e 0%, #16213e 100%);
overflow:hidden;
border:2px solid #ff4d4d;
}
.physics-box{
position:absolute;
width:60px;
height:60px;
background: linear-gradient(135deg, #ff4d4d 0%, #cc0000 100%);
border:2px solid #fff;
box-shadow: 0 0 10px rgba(255,77,77,0.5);
}
.physics-box-small{
position:absolute;
width:50px;
height:40px;
background: linear-gradient(135deg, #ff4d4d 0%, #cc0000 100%);
border:2px solid #fff;
box-shadow: 0 0 10px rgba(255,77,77,0.5);
}
.physics-ball{
position:absolute;
width:60px;
height:60px;
background: linear-gradient(135deg, #ffaa00 0%, #ff6600 100%);
border-radius:50%;
border:2px solid #fff;
box-shadow: 0 0 10px rgba(255,170,0,0.5);
}
.physics-circle{
position:absolute;
width:50px;
height:50px;
background: linear-gradient(135deg, #4d94ff 0%, #0066cc 100%);
border-radius:50%;
border:2px solid #fff;
box-shadow: 0 0 10px rgba(77,148,255,0.5);
}
.cloth-particle{
position:absolute;
width:25px;
height:25px;
background: #FFF;
border-radius:50%;
}
.cloth-particle.pinned{
background: radial-gradient(circle, #000000 0%, #111111 100%);
border-color:#fff;
}
.pointer {
background: transparent;
width: 80px;
height: 80px;
}
.physics-ground{
position:absolute;
bottom:0;
width:100%;
height:40px;
background: linear-gradient(180deg, #2d4a2b 0%, #1f3a1f 100%);
border-top:3px solid #4a7a48;
}
.physics-platform{
position:absolute;
height:20px;
background: linear-gradient(180deg, #8b4513 0%, #654321 100%);
border-top:2px solid #a0522d;
}
.sensor-zone{
position:absolute;
border:3px dashed #ffa500;
background:rgba(255,165,0,0.1);
}
.sensor-zone.gravity-colliding{
background:rgba(255,165,0,0.3);
border-color:#ff6600;
}
.demo-label{
position:absolute;
top:10px;
left:10px;
background:rgba(0,0,0,0.7);
padding:5px 10px;
font-size:14px;
color:#ffa500;
z-index:100;
}
.collision-indicator{
position:absolute;
top:10px;
right:10px;
background:rgba(0,0,0,0.7);
padding:5px 10px;
font-size:14px;
color:#4dff4d;
z-index:100;
}
.reset-button{
display:inline-block;
margin:10px;
background-color:#ff4d4d;
color:#fff;
font-weight:bold;
padding:12px 30px;
text-decoration:none;
border:2px solid #fff;
cursor:pointer;
font-family:'Space Mono', monospace;
font-size:16px;
box-shadow:0 0 10px rgba(255,77,77,0.5);
transition:all 0.3s;
}
.reset-button:hover{
background-color:#cc0000;
box-shadow:0 0 20px rgba(255,77,77,0.8);
}
@media screen and (min-width: 768px) {
h1{
font-size:122px;
}
h2{
font-size:72px;
}
}
</style>
</head>
<body data-gravity-world-gravity="19.8">
<div class="pageContainer">
<div class="container alignCenter">
<div class="box">
<h1 id="logo">Gravity.js</h1>
<hr style="width:200px;background:#ff4d4d;height:3px;border:none;"/>
<h2>Documentation <sup>v1.2.0</sup></h2>
</div>
<div class="box">
<a href="https://codepen.io/collection/JYOKxx" target="_blank" style="text-decoration:none;">
<button class="reset-button" style="background:#000;border:2px solid #4d94ff;">
View Live Demos & Examples on CodePen
</button>
</a>
</div>
</div>
<hr/>
<div class="container alignCenter">
<div class="box">
<h2>What is Gravity.js?</h2>
</div>
<div class="box">
Gravity.js is a lightweight physics engine that renders using CSS. Configure physics bodies, collisions, and dynamics entirely through data attributes - no JavaScript required for basic physics interactions.
</div>
<div class="box">
Using SAT (Separating Axis Theorem) for OBB (Oriented Bounding Box) collision detection, Gravity.js supports true rotation and realistic physics. Bodies can tilt, tumble, and collide at any angle. It integrates seamlessly with State.js for game state management and Keys.js for player input.
</div>
<div class="box">
Perfect for creating browser-based games, interactive experiences, and physics simulations without the overhead of canvas rendering. Native DOM elements become physics bodies!
</div>
</div>
<hr/>
<div class="container alignCenter">
<div class="box">
<h2 class="gravityText">Demo 1: Falling Boxes</h2>
</div>
<div class="demo-container" id="demo1" data-gravity-container>
<div class="demo-label">Dynamic Bodies with Gravity</div>
<div id="box1" class="physics-box" data-gravity data-gravity-type="dynamic" data-gravity-mass="1" data-gravity-restitution="0.4" style="top:50px;left:100px;"></div>
<div id="box2" class="physics-box" data-gravity data-gravity-type="dynamic" data-gravity-mass="1.5" data-gravity-restitution="0.6" style="top:20px;left:250px;"></div>
<div id="box3" class="physics-box" data-gravity data-gravity-type="dynamic" data-gravity-mass="0.8" data-gravity-restitution="0.3" style="top:80px;left:400px;"></div>
<div class="physics-ground" data-gravity data-gravity-type="static"></div>
</div>
<div class="box">
Boxes fall under gravity and bounce on the ground. Different masses and restitution values create varied behaviors.
</div>
<div class="box">
<button class="reset-button" onclick="gravity.resetBodies('#demo1')">Reset Demo</button>
</div>
</div>
<hr/>
<div class="container alignCenter">
<div class="box">
<h2 class="gravityText">Demo 2: Tower of Boxes</h2>
</div>
<div class="demo-container" id="demo2" data-gravity-container>
<div class="demo-label">Stack Physics & Projectile</div>
<div id="tower1" class="physics-box-small" data-gravity data-gravity-type="dynamic" data-gravity-mass="1" data-gravity-restitution="0.1" data-gravity-friction="0.9" style="bottom:40px;left:300px;"></div>
<div id="tower2" class="physics-box-small" data-gravity data-gravity-type="dynamic" data-gravity-mass="1" data-gravity-restitution="0.1" data-gravity-friction="0.9" style="bottom:80px;left:300px;"></div>
<div id="tower3" class="physics-box-small" data-gravity data-gravity-type="dynamic" data-gravity-mass="1" data-gravity-restitution="0.1" data-gravity-friction="0.9" style="bottom:120px;left:300px;"></div>
<div id="tower4" class="physics-box-small" data-gravity data-gravity-type="dynamic" data-gravity-mass="1" data-gravity-restitution="0.1" data-gravity-friction="0.9" style="bottom:160px;left:300px;"></div>
<div id="tower5" class="physics-box-small" data-gravity data-gravity-type="dynamic" data-gravity-mass="1" data-gravity-restitution="0.1" data-gravity-friction="0.9" style="bottom:200px;left:300px;"></div>
<div id="projectile" class="physics-box" data-gravity data-gravity-type="dynamic" data-gravity-mass="1.5" data-gravity-restitution="0.3" data-gravity-friction="0.4" data-gravity-velocity-x="180" data-gravity-velocity-y="30" style="top:160px;left:50px;"></div>
<div class="physics-ground" data-gravity data-gravity-type="static"></div>
</div>
<div class="box">
A tower of stacked boxes with a projectile that knocks them down! Watch the boxes rotate and tumble using SAT (Separating Axis Theorem) for OBB collision detection. High friction keeps boxes stable until impact.
</div>
<div class="box">
<button class="reset-button" onclick="gravity.resetBodies('#demo2')">Reset Demo</button>
</div>
</div>
<hr/>
<div class="container alignCenter">
<div class="box">
<h2 class="gravityText">Demo 3: Platforms</h2>
</div>
<div class="demo-container" id="demo3" data-gravity-container>
<div class="demo-label">Static Platform Bodies & Slopes</div>
<div id="platform-box" class="physics-box" data-gravity data-gravity-type="dynamic" data-gravity-mass="1" data-gravity-restitution="0.2" style="top:20px;left:200px;"></div>
<div class="physics-platform" data-gravity data-gravity-type="static" style="bottom:120px;left:50px;width:150px;transform:rotate(15deg);"></div>
<div class="physics-platform" data-gravity data-gravity-type="static" style="bottom:200px;left:250px;width:150px;transform:rotate(-15deg)"></div>
<div class="physics-platform" data-gravity data-gravity-type="static" style="bottom:150px;left:450px;width:150px;transform:rotate(-10deg);"></div>
<div class="physics-ground" data-gravity data-gravity-type="static"></div>
</div>
<div class="box">
Static platforms with rotation create slopes! Watch the dynamic box slide down angled platforms and bounce between them. OBB collision allows platforms to be rotated at any angle. Use data-gravity-fixed-rotation="true" on dynamic bodies to prevent rotation.
</div>
<div class="box">
<button class="reset-button" onclick="gravity.resetBodies('#demo3')">Reset Demo</button>
</div>
</div>
<hr/>
<div class="container alignCenter">
<div class="box">
<h2 class="gravityText">Demo 4: Keys.js Integration</h2>
</div>
<div class="demo-container" id="demo4" data-gravity-container data-keys data-keys-watch="w,a,s,d,up,down,left,right" data-keys-prevent="w,a,s,d,up,down,left,right" data-keys-var="true">
<div class="demo-label">Use WASD or Arrow Keys to Move</div>
<div id="player" class="physics-box"
data-gravity
data-gravity-type="dynamic"
data-gravity-mass="1"
data-gravity-restitution="0.1"
data-gravity-friction="0.7"
data-gravity-fixed-rotation="true"
data-gravity-force-right="--key-d, --key-right"
data-gravity-force-left="--key-a, --key-left"
data-gravity-force-down="--key-s, --key-down"
data-gravity-force-up="--key-w, --key-up"
data-gravity-force-multiplier="80"
style="top:50px;left:250px;background:#4d94ff;"></div>
<div class="physics-ground" data-gravity data-gravity-type="static"></div>
</div>
<div class="box">
Keys.js sets CSS variables (--key-d, --key-a, etc.) and Gravity.js reads them via comma-separated CSS variable names. Example: data-gravity-force-right="--key-d, --key-right" combines both D and Right Arrow keys. Fully declarative!
</div>
<div class="box">
<button class="reset-button" onclick="gravity.resetBodies('#demo4')">Reset Demo</button>
</div>
</div>
<hr/>
<div class="container alignCenter">
<div class="box">
<h2 class="gravityText">Demo 5: Tearable Cloth</h2>
</div>
<div class="demo-container" id="demo5" style="height:500px;" data-gravity-container data-gravity-world-gravity="9.8" data-cursor data-cursor-var="true" data-cursor-pixels="true">
<div class="demo-label">Move your cursor to interact with the cloth!</div>
<div class="pointer" data-gravity data-gravity-type="kinematic" data-gravity-mass="200" data-gravity-restitution="0.1" data-gravity-position-x="--cursor-x" data-gravity-position-y="--cursor-y" data-gravity-rotation-var="--cursor-rotation"></div>
</div>
<div class="box">
A grid of particles connected by distance constraints. Grab and pull the cloth to tear it! The constraint system uses position-based dynamics with break forces. Each particle is a physics body connected to its neighbors.
</div>
</div>
<hr/>
<div class="container alignCenter">
<div class="box boxBG">
<h2>How To Install?</h2>
</div>
<div class="box boxBG">
<p>Add the gravity.js file into your project and include it in your HTML head tags:</p>
<pre>
<script src="https://cdn.jsdelivr.net/npm/@idevgames/gravity-js/src/gravity.js"></script>
</pre>
<p>Or use npm:</p>
<pre>
npm i @idevgames/gravity-js
</pre>
</div>
</div>
<hr/>
<div class="container alignCenter">
<div class="box boxBG">
<h2>How To Use?</h2>
</div>
<div class="box boxBG">
<p>Add the data attribute "data-gravity" or class "enable-gravity" to your HTML element to make it a physics body:</p>
<pre>
<div data-gravity data-gravity-type="dynamic"></div>
</pre>
<p>The element will automatically become a physics body and respond to gravity and collisions!</p>
</div>
</div>
<hr/>
<div class="container alignCenter">
<div class="box boxBG">
<h2>Physics Body Types</h2>
</div>
<div class="box boxBG">
<p>Three body types are available via <strong>data-gravity-type</strong>:</p>
<div class="listItem">
<strong>dynamic</strong> - Moves and collides (default)
</div>
<div class="listItem">
<strong>static</strong> - Never moves (ground, walls, platforms)
</div>
<div class="listItem">
<strong>kinematic</strong> - Moves but infinite mass (moving platforms)
</div>
</div>
</div>
<hr/>
<div class="container alignCenter">
<div class="box boxBG">
<h2>Shape Types</h2>
</div>
<div class="box boxBG">
<p>Set collision shape with <strong>data-gravity-shape</strong>:</p>
<div class="listItem">
<strong>box</strong> - Rectangle collision (default, uses element dimensions)
</div>
</div>
</div>
<hr/>
<div class="container alignCenter">
<div class="box boxBG">
<h2>Physics Properties</h2>
</div>
<div class="box boxBG">
<p>Customize physics behavior with these data attributes:</p>
<div class="listItem">
<strong>data-gravity-mass="1"</strong> - Body mass (affects momentum)
</div>
<div class="listItem">
<strong>data-gravity-restitution="0.3"</strong> - Bounciness (0-1)
</div>
<div class="listItem">
<strong>data-gravity-friction="0.5"</strong> - Surface friction (0-1)
</div>
<div class="listItem">
<strong>data-gravity-density="1"</strong> - Material density
</div>
<div class="listItem">
<strong>data-gravity-fixed-rotation="true"</strong> - Prevent rotation
</div>
</div>
</div>
<hr/>
<div class="container alignCenter">
<div class="box boxBG">
<h2>Initial Velocity & Forces</h2>
</div>
<div class="box boxBG">
<p><strong>Initial Velocity (Static):</strong></p>
<div class="listItem">
<strong>data-gravity-velocity-x="10"</strong> - Initial horizontal velocity (accepts CSS variables like --cursor-x)
</div>
<div class="listItem">
<strong>data-gravity-velocity-y="-5"</strong> - Initial vertical velocity (accepts CSS variables like --cursor-y)
</div>
</div>
<div class="box boxBG">
<p><strong>Initial Velocity (Directional):</strong></p>
<div class="listItem">
<strong>data-gravity-velocity-right="300"</strong> - Initial rightward velocity (accepts CSS variables)
</div>
<div class="listItem">
<strong>data-gravity-velocity-left="100"</strong> - Initial leftward velocity (accepts CSS variables)
</div>
<div class="listItem">
<strong>data-gravity-velocity-up="200"</strong> - Initial upward velocity (jumps! accepts CSS variables)
</div>
<div class="listItem">
<strong>data-gravity-velocity-down="50"</strong> - Initial downward velocity (accepts CSS variables)
</div>
<p style="margin-top:10px;">Directional velocities are combined: (right - left) for horizontal, (down - up) for vertical. All velocity attributes accept CSS variables (e.g., --cursor-x from Cursor.js). Perfect for State.js to spawn projectiles dynamically!</p>
</div>
<div class="box boxBG">
<p><strong>Container Bounds:</strong></p>
<div class="listItem">
<strong>data-gravity-container</strong> - Add to parent element to create invisible walls/ceiling/floor
</div>
<p style="margin-top:10px;">Dynamic bodies will bounce off container edges. Perfect for keeping objects in view with cursor/mouse interaction!</p>
</div>
<div class="box boxBG">
<p><strong>Continuous Forces (Static):</strong></p>
<div class="listItem">
<strong>data-gravity-force-x="2"</strong> - Continuous horizontal force
</div>
<div class="listItem">
<strong>data-gravity-force-y="0"</strong> - Continuous vertical force
</div>
</div>
<div class="box boxBG">
<p><strong>Dynamic Forces (CSS Variables):</strong></p>
<div class="listItem">
<strong>data-gravity-force-right="--key-d, --key-right"</strong> - Rightward force from CSS variable(s)
</div>
<div class="listItem">
<strong>data-gravity-force-left="--key-a, --key-left"</strong> - Leftward force from CSS variable(s)
</div>
<div class="listItem">
<strong>data-gravity-force-up="--key-w, --key-up"</strong> - Upward force from CSS variable(s)
</div>
<div class="listItem">
<strong>data-gravity-force-down="--key-s, --key-down"</strong> - Downward force from CSS variable(s)
</div>
<div class="listItem">
<strong>data-gravity-force-multiplier="80"</strong> - Scales directional force values
</div>
<p style="margin-top:10px;">Perfect for Keys.js integration! Accepts comma-separated CSS variable names (values are summed). Forces are combined: (right - left) for horizontal, (down - up) for vertical.</p>
</div>
</div>
<hr/>
<div class="container alignCenter">
<div class="box boxBG">
<h2>CSS Variables</h2>
</div>
<div class="box boxBG">
<p>Gravity.js exposes physics state as CSS variables:</p>
<div class="listItem">
--gravity-x, --gravity-y
</div>
<div class="listItem">
--gravity-rotation
</div>
<div class="listItem">
--gravity-velocity-x, --gravity-velocity-y
</div>
<div class="listItem">
--gravity-speed
</div>
<div class="listItem">
--gravity-collision (1 when colliding)
</div>
<p>Use these in your CSS for dynamic effects based on physics state!</p>
</div>
</div>
<hr/>
<div class="container alignCenter">
<div class="box boxBG">
<h2>Collision Detection</h2>
</div>
<div class="box boxBG">
<p>Collisions are detected automatically and trigger:</p>
<div class="listItem">
CSS class <strong>.gravity-colliding</strong> added during collision
</div>
<div class="listItem">
CSS class <strong>.gravity-collision-[id]</strong> for specific collisions
</div>
<div class="listItem">
Custom event <strong>gravitycollision</strong> on collision start
</div>
<div class="listItem">
Custom event <strong>gravityexit</strong> on collision end
</div>
<div class="listItem">
Data attribute <strong>data-collision-with</strong> updated with colliding element ID
</div>
</div>
</div>
<hr/>
<div class="container alignCenter">
<div class="box boxBG">
<h2>Collision Sensors (Triggers)</h2>
</div>
<div class="box boxBG">
<p>Create trigger zones with <strong>data-gravity-sensor="true"</strong>:</p>
<pre>
<div data-gravity data-gravity-type="static" data-gravity-sensor="true"></div>
</pre>
<p>Sensors detect collisions but don't create physical responses. Perfect for trigger zones, checkpoints, and event areas!</p>
</div>
</div>
<hr/>
<div class="container alignCenter">
<div class="box boxBG">
<h2>Constraints & Joints (v1.2.0)</h2>
</div>
<div class="box boxBG">
<p>Connect bodies together with distance constraints using JavaScript:</p>
<pre>
const constraint = gravity.addConstraint(elementIdA, elementIdB, {
length: 100, // Distance to maintain (defaults to current distance)
stiffness: 0.9, // How strongly constraint pulls (0-1, default 0.5)
breakForce: 50 // Tension threshold for breaking (0 = unbreakable, default 0)
});
</pre>
<p><strong>Constraint Parameters:</strong></p>
<div class="listItem">
<strong>length</strong> - Target distance between bodies in pixels (optional, defaults to current distance)
</div>
<div class="listItem">
<strong>stiffness</strong> - Constraint strength from 0 to 1 (higher = more rigid)
</div>
<div class="listItem">
<strong>breakForce</strong> - Maximum tension before breaking (0 = unbreakable)
</div>
<p><strong>Constraint Breaking Event:</strong></p>
<pre>
element.addEventListener('constraintbreak', (event) => {
console.log('Constraint broke!', event.detail.constraint);
});
</pre>
<p>Perfect for ropes, chains, cloth simulation, and destructible structures! See Demo 5 for a tearable cloth example.</p>
</div>
</div>
<hr/>
<div class="container alignCenter">
<div class="box boxBG">
<h2>Collision Groups</h2>
</div>
<div class="box boxBG">
<p>Filter collisions with <strong>data-gravity-group</strong>:</p>
<pre>
<div data-gravity data-gravity-group="player"></div>
<div data-gravity data-gravity-group="enemy"></div>
</pre>
<p>Bodies only collide with others in the same group or the default group.</p>
</div>
</div>
<hr/>
<div class="container alignCenter">
<div class="box boxBG">
<h2>World Settings</h2>
</div>
<div class="box boxBG">
<p>Configure global physics on the body element:</p>
<div class="listItem">
<strong>data-gravity-world-gravity="9.8"</strong> - Global gravity strength
</div>
<div class="listItem">
<strong>data-gravity-world-gravity-x="0"</strong> - Horizontal gravity
</div>
<div class="listItem">
<strong>data-gravity-world-gravity-y="9.8"</strong> - Vertical gravity
</div>
<div class="listItem">
<strong>data-gravity-damping="0.99"</strong> - Velocity damping
</div>
<div class="listItem">
<strong>data-gravity-angular-damping="0.99"</strong> - Rotation damping
</div>
</div>
</div>
<hr/>
<div class="container alignCenter">
<div class="box boxBG">
<h2>JavaScript API</h2>
</div>
<div class="box boxBG">
<p>Optional JavaScript methods for advanced control:</p>
<pre>
gravity.applyForce(elementId, x, y)
gravity.applyImpulse(elementId, x, y)
gravity.setVelocity(elementId, x, y)
gravity.getBody(elementId)
gravity.pauseSimulation()
gravity.resumeSimulation()
</pre>
</div>
</div>
<hr/>
<div class="container alignCenter">
<div class="box boxBG">
<h2>Integration with State.js</h2>
</div>
<div class="box boxBG">
<p>Gravity.js works seamlessly with State.js for game state management:</p>
<pre>
<div id="player" data-gravity data-state data-state-watch="collision-with"></div>
<div id="coin" data-gravity data-gravity-sensor="true"></div>
</pre>
<p>Collision events update data attributes that State.js can watch and react to. Use collision sensors to trigger state changes!</p>
</div>
</div>
<hr/>
<div class="container alignCenter">
<div class="box boxBG">
<h2>Integration with Keys.js</h2>
</div>
<div class="box boxBG">
<p>Combine with Keys.js for player input:</p>
<pre>
document.addEventListener('keydown', (e) => {
if (keys.isKeyDown('space')) {
gravity.applyImpulse('player', 0, -300);
}
if (keys.isKeyDown('right')) {
gravity.applyForce('player', 50, 0);
}
});
</pre>
<p>Create fully interactive games with physics, input, and state management!</p>
</div>
</div>
<hr/>
<div class="container alignCenter">
<div class="box">
<div class="gravityText" style="font-size:62px;">BUILD PHYSICS GAMES</div>
</div>
<div class="box">
<div class="gravityText" style="font-size:62px;">WITH PURE CSS RENDERING</div>
</div>
<div class="box">
<p>Gravity.js + State.js + Keys.js = Complete Browser Game Engine</p>
</div>
</div>
<hr/>
<div class="container">
<div class="box">
<p>Check out the companion libraries:</p>
</div>
<div class="listItem">
<a href="https://github.com/iDev-Games/State-JS" target="_blank" style="color:#FFF;">State.js - Game State Management</a>
</div>
<div class="listItem">
<a href="https://github.com/iDev-Games/Keys-JS" target="_blank" style="color:#FFF;">Keys.js - Keyboard Input</a>
</div>
<div class="box">
<a href="https://dev.to/idevgames" target="_blank" style="color:#FFF;">Keep up to date on dev.to</a>
</div>
</div>
</div>
<script>
let clothConstraints = [];
function createCloth() {
const container = document.getElementById('demo5');
const cols = 10, rows = 8, spacing = 25, startX = 150, startY = 50;
for (let y = 0; y < rows; y++) {
for (let x = 0; x < cols; x++) {
const particle = document.createElement('div');
particle.id = `cloth-${x}-${y}`;
particle.className = 'cloth-particle' + (y === 0 ? ' pinned' : '');
particle.setAttribute('data-gravity', '');
particle.setAttribute('data-gravity-type', y === 0 ? 'static' : 'dynamic');
particle.setAttribute('data-gravity-shape', 'circle');
particle.setAttribute('data-gravity-mass', '0.1');
particle.setAttribute('data-gravity-restitution', '0.1');
particle.setAttribute('data-gravity-friction', '0.1');
particle.style.left = (startX + x * spacing) + 'px';
particle.style.top = (startY + y * spacing) + 'px';
container.appendChild(particle);
}
}
setTimeout(() => {
container.querySelectorAll('.cloth-particle').forEach(p => gravity.setupBody(p));
setTimeout(() => {
for (let y = 0; y < rows; y++) {
for (let x = 0; x < cols; x++) {
const id = `cloth-${x}-${y}`;
if (x < cols - 1) clothConstraints.push(gravity.addConstraint(id, `cloth-${x+1}-${y}`, {stiffness: 0.9, breakForce: 50}));
if (y < rows - 1) clothConstraints.push(gravity.addConstraint(id, `cloth-${x}-${y+1}`, {stiffness: 0.9, breakForce: 50}));
if (x < cols - 1 && y < rows - 1) clothConstraints.push(gravity.addConstraint(id, `cloth-${x+1}-${y+1}`, {stiffness: 0.5, breakForce: 30}));
if (x > 0 && y < rows - 1) clothConstraints.push(gravity.addConstraint(id, `cloth-${x-1}-${y+1}`, {stiffness: 0.5, breakForce: 30}));
}
}
}, 50);
}, 50);
}
createCloth();
</script>
</body>
</html>