@newkrok/nape-js 1.0.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,25 +1,145 @@
1
- # Nape Physics Engine compiled to JavaScript
2
-
3
- - Original code by Luca Deltodesco https://github.com/deltaluca/nape
4
- - JS compiler by Andrew Bradley https://github.com/cspotcode/nape-to-js
5
-
6
- # How to use?
7
- Here is a simple example which demonstrate the getter/setter problem and solution.
8
- ```
9
- import initNape from "./js/libs/nape-js.module.js";
10
-
11
- initNape();
12
- const space = new nape.space.Space(new nape.geom.Vec2(0, 350));
13
-
14
- const body = new nape.phys.Body(nape.phys.BodyType.get_STATIC());
15
- body
16
- .get_shapes()
17
- .add(new nape.shape.Polygon(nape.shape.Polygon.box(100, 100)));
18
- body.get_position().set_x(200);
19
- body.get_position().set_y(50);
20
- body.set_rotation(Math.PI / 2);
21
- body.set_space(space);
22
- ```
23
-
24
- # Some additional info
25
- Since it's more than 2MB and it's really just a compiled version by haxe, it's not really easy to use, probably it will be only interesting for you when you have a Haxe project and you want to easily rewrite it in vanilla JavaScript.
1
+ # @newkrok/nape-js
2
+
3
+ [![npm version](https://img.shields.io/npm/v/@newkrok/nape-js.svg)](https://www.npmjs.com/package/@newkrok/nape-js)
4
+ [![npm downloads](https://img.shields.io/npm/dm/@newkrok/nape-js.svg)](https://www.npmjs.com/package/@newkrok/nape-js)
5
+ [![tests](https://img.shields.io/badge/tests-141%20passed-brightgreen.svg)](https://github.com/NewKrok/nape-js)
6
+ [![bundle size](https://img.shields.io/badge/gzip-414%20KB-blue.svg)](https://github.com/NewKrok/nape-js)
7
+ [![license](https://img.shields.io/npm/l/@newkrok/nape-js.svg)](https://github.com/NewKrok/nape-js/blob/master/LICENSE)
8
+
9
+ Modern TypeScript wrapper for the [Nape](https://github.com/deltaluca/nape) 2D physics engine.
10
+
11
+ - Original Haxe engine by Luca Deltodesco
12
+ - JS compiler by Andrew Bradley ([nape-to-js](https://github.com/cspotcode/nape-to-js))
13
+ - TypeScript wrapper by Istvan Krisztian Somoracz
14
+
15
+ ## Installation
16
+
17
+ ```bash
18
+ npm install @newkrok/nape-js
19
+ ```
20
+
21
+ ## Quick Start
22
+
23
+ ```typescript
24
+ import { Space, Body, BodyType, Vec2, Circle, Polygon } from "@newkrok/nape-js";
25
+
26
+ // Create a physics world with downward gravity
27
+ const space = new Space(new Vec2(0, 600));
28
+
29
+ // Static floor
30
+ const floor = new Body(BodyType.STATIC, new Vec2(400, 550));
31
+ floor.shapes.add(new Polygon(Polygon.box(800, 20)));
32
+ floor.space = space;
33
+
34
+ // Dynamic box
35
+ const box = new Body(BodyType.DYNAMIC, new Vec2(400, 100));
36
+ box.shapes.add(new Polygon(Polygon.box(40, 40)));
37
+ box.space = space;
38
+
39
+ // Dynamic circle
40
+ const ball = new Body(BodyType.DYNAMIC, new Vec2(420, 50));
41
+ ball.shapes.add(new Circle(20));
42
+ ball.space = space;
43
+
44
+ // Game loop
45
+ function update() {
46
+ space.step(1 / 60);
47
+
48
+ for (const body of space.bodies) {
49
+ console.log(`x=${body.position.x.toFixed(1)} y=${body.position.y.toFixed(1)}`);
50
+ }
51
+ }
52
+ ```
53
+
54
+ ### Before (v1) vs After (v2)
55
+
56
+ ```javascript
57
+ // v1 — raw Haxe API
58
+ import initNape from "./js/libs/nape-js.module.js";
59
+ initNape();
60
+ const body = new nape.phys.Body(nape.phys.BodyType.get_DYNAMIC());
61
+ body.get_shapes().add(new nape.shape.Polygon(nape.shape.Polygon.box(100, 100)));
62
+ body.get_position().set_x(200);
63
+ body.set_rotation(Math.PI / 2);
64
+ body.set_space(space);
65
+ ```
66
+
67
+ ```typescript
68
+ // v2 — TypeScript wrapper
69
+ import { Body, BodyType, Polygon } from "@newkrok/nape-js";
70
+ const body = new Body(BodyType.DYNAMIC);
71
+ body.shapes.add(new Polygon(Polygon.box(100, 100)));
72
+ body.position.x = 200;
73
+ body.rotation = Math.PI / 2;
74
+ body.space = space;
75
+ ```
76
+
77
+ ## API Reference
78
+
79
+ ### Core Classes
80
+
81
+ | Class | Description |
82
+ |-------|-------------|
83
+ | `Space` | Physics world — add bodies, step simulation |
84
+ | `Body` | Rigid body with position, velocity, mass |
85
+ | `Vec2` | 2D vector for positions, velocities, forces |
86
+ | `AABB` | Axis-aligned bounding box |
87
+
88
+ ### Shapes
89
+
90
+ | Class | Description |
91
+ |-------|-------------|
92
+ | `Circle` | Circular shape |
93
+ | `Polygon` | Convex polygon (with `Polygon.box()`, `Polygon.rect()`, `Polygon.regular()`) |
94
+ | `Shape` | Base class with material, filter, sensor support |
95
+
96
+ ### Physics Properties
97
+
98
+ | Class | Description |
99
+ |-------|-------------|
100
+ | `Material` | Elasticity, friction, density |
101
+ | `BodyType` | `STATIC`, `DYNAMIC`, `KINEMATIC` |
102
+ | `InteractionFilter` | Bit-mask collision/sensor/fluid filtering |
103
+ | `FluidProperties` | Density, viscosity for fluid shapes |
104
+
105
+ ### Constraints
106
+
107
+ | Class | Description |
108
+ |-------|-------------|
109
+ | `PivotJoint` | Pin two bodies at a shared point |
110
+ | `DistanceJoint` | Constrain distance between anchors |
111
+ | `WeldJoint` | Fix relative position and angle |
112
+ | `AngleJoint` | Constrain relative angle |
113
+ | `MotorJoint` | Apply angular velocity |
114
+ | `LineJoint` | Slide along a line |
115
+ | `PulleyJoint` | Constrain combined distances |
116
+
117
+ ### Callbacks
118
+
119
+ | Class | Description |
120
+ |-------|-------------|
121
+ | `InteractionListener` | Collision/sensor/fluid events |
122
+ | `BodyListener` | Body wake/sleep events |
123
+ | `ConstraintListener` | Constraint events |
124
+ | `PreListener` | Pre-collision filtering |
125
+ | `CbType` | Tag interactors for filtering |
126
+ | `CbEvent` | `BEGIN`, `ONGOING`, `END`, `WAKE`, `SLEEP`, `BREAK` |
127
+
128
+ ### Utilities
129
+
130
+ | Class | Description |
131
+ |-------|-------------|
132
+ | `NapeList<T>` | Iterable list with `for...of` support |
133
+
134
+ ## Development
135
+
136
+ ```bash
137
+ npm install
138
+ npm run build # Compile TypeScript + bundle
139
+ npm test # Run tests (141 tests)
140
+ npm run benchmark # Performance benchmarks
141
+ ```
142
+
143
+ ## License
144
+
145
+ MIT