@newkrok/nape-js 1.0.0 → 2.1.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,139 @@
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
+ Modern TypeScript wrapper for the [Nape](https://github.com/deltaluca/nape) 2D physics engine.
4
+
5
+ - Original Haxe engine by Luca Deltodesco
6
+ - JS compiler by Andrew Bradley ([nape-to-js](https://github.com/cspotcode/nape-to-js))
7
+ - TypeScript wrapper by Istvan Krisztian Somoracz
8
+
9
+ ## Installation
10
+
11
+ ```bash
12
+ npm install @newkrok/nape-js
13
+ ```
14
+
15
+ ## Quick Start
16
+
17
+ ```typescript
18
+ import { Space, Body, BodyType, Vec2, Circle, Polygon } from "@newkrok/nape-js";
19
+
20
+ // Create a physics world with downward gravity
21
+ const space = new Space(new Vec2(0, 600));
22
+
23
+ // Static floor
24
+ const floor = new Body(BodyType.STATIC, new Vec2(400, 550));
25
+ floor.shapes.add(new Polygon(Polygon.box(800, 20)));
26
+ floor.space = space;
27
+
28
+ // Dynamic box
29
+ const box = new Body(BodyType.DYNAMIC, new Vec2(400, 100));
30
+ box.shapes.add(new Polygon(Polygon.box(40, 40)));
31
+ box.space = space;
32
+
33
+ // Dynamic circle
34
+ const ball = new Body(BodyType.DYNAMIC, new Vec2(420, 50));
35
+ ball.shapes.add(new Circle(20));
36
+ ball.space = space;
37
+
38
+ // Game loop
39
+ function update() {
40
+ space.step(1 / 60);
41
+
42
+ for (const body of space.bodies) {
43
+ console.log(`x=${body.position.x.toFixed(1)} y=${body.position.y.toFixed(1)}`);
44
+ }
45
+ }
46
+ ```
47
+
48
+ ### Before (v1) vs After (v2)
49
+
50
+ ```javascript
51
+ // v1 — raw Haxe API
52
+ import initNape from "./js/libs/nape-js.module.js";
53
+ initNape();
54
+ const body = new nape.phys.Body(nape.phys.BodyType.get_DYNAMIC());
55
+ body.get_shapes().add(new nape.shape.Polygon(nape.shape.Polygon.box(100, 100)));
56
+ body.get_position().set_x(200);
57
+ body.set_rotation(Math.PI / 2);
58
+ body.set_space(space);
59
+ ```
60
+
61
+ ```typescript
62
+ // v2 — TypeScript wrapper
63
+ import { Body, BodyType, Polygon } from "@newkrok/nape-js";
64
+ const body = new Body(BodyType.DYNAMIC);
65
+ body.shapes.add(new Polygon(Polygon.box(100, 100)));
66
+ body.position.x = 200;
67
+ body.rotation = Math.PI / 2;
68
+ body.space = space;
69
+ ```
70
+
71
+ ## API Reference
72
+
73
+ ### Core Classes
74
+
75
+ | Class | Description |
76
+ |-------|-------------|
77
+ | `Space` | Physics world — add bodies, step simulation |
78
+ | `Body` | Rigid body with position, velocity, mass |
79
+ | `Vec2` | 2D vector for positions, velocities, forces |
80
+ | `AABB` | Axis-aligned bounding box |
81
+
82
+ ### Shapes
83
+
84
+ | Class | Description |
85
+ |-------|-------------|
86
+ | `Circle` | Circular shape |
87
+ | `Polygon` | Convex polygon (with `Polygon.box()`, `Polygon.rect()`, `Polygon.regular()`) |
88
+ | `Shape` | Base class with material, filter, sensor support |
89
+
90
+ ### Physics Properties
91
+
92
+ | Class | Description |
93
+ |-------|-------------|
94
+ | `Material` | Elasticity, friction, density |
95
+ | `BodyType` | `STATIC`, `DYNAMIC`, `KINEMATIC` |
96
+ | `InteractionFilter` | Bit-mask collision/sensor/fluid filtering |
97
+ | `FluidProperties` | Density, viscosity for fluid shapes |
98
+
99
+ ### Constraints
100
+
101
+ | Class | Description |
102
+ |-------|-------------|
103
+ | `PivotJoint` | Pin two bodies at a shared point |
104
+ | `DistanceJoint` | Constrain distance between anchors |
105
+ | `WeldJoint` | Fix relative position and angle |
106
+ | `AngleJoint` | Constrain relative angle |
107
+ | `MotorJoint` | Apply angular velocity |
108
+ | `LineJoint` | Slide along a line |
109
+ | `PulleyJoint` | Constrain combined distances |
110
+
111
+ ### Callbacks
112
+
113
+ | Class | Description |
114
+ |-------|-------------|
115
+ | `InteractionListener` | Collision/sensor/fluid events |
116
+ | `BodyListener` | Body wake/sleep events |
117
+ | `ConstraintListener` | Constraint events |
118
+ | `PreListener` | Pre-collision filtering |
119
+ | `CbType` | Tag interactors for filtering |
120
+ | `CbEvent` | `BEGIN`, `ONGOING`, `END`, `WAKE`, `SLEEP`, `BREAK` |
121
+
122
+ ### Utilities
123
+
124
+ | Class | Description |
125
+ |-------|-------------|
126
+ | `NapeList<T>` | Iterable list with `for...of` support |
127
+
128
+ ## Development
129
+
130
+ ```bash
131
+ npm install
132
+ npm run build # Compile TypeScript + bundle
133
+ npm test # Run tests (71 tests)
134
+ npm run benchmark # Performance benchmarks
135
+ ```
136
+
137
+ ## License
138
+
139
+ MIT