Skip to content

Commit 8bb96dc

Browse files
committed
Add DataDriver (experimental)
1 parent 7c400fa commit 8bb96dc

File tree

9 files changed

+1586
-964
lines changed

9 files changed

+1586
-964
lines changed

.changeset/gentle-colts-hunt.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"planck": minor
3+
---
4+
5+
Add DataDriver (experimental for demo use-case)

docs/pages/data-driver.md

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
## Data Driver
2+
3+
When developing a game or an application you often need a data-model to represent your game state and objects. A physics engine is useful for simulating the physical movements and interactions between objects, however it is not always the best tool for representing your game data-model. In addition, a physics engine has very complex internal state, which would add unnecessarily complexity to your game if physics engine internal state is directly combined with the game data-model.
4+
5+
This is where the DataDriver comes in. A DataDriver translates your game data-model to physics engine objects, while keeping the two separate. This allows you to create a simple and clean game data-model, while still benefiting from the physics engine.
6+
7+
Planck's DataDriver class is inspired by D3, and reactive-programming in general. Given two arrays of objects, and a key-function, it find the elements that are added or removed from the array, and calls the corresponding callback functions to map them to physics simulation.
8+
9+
Another benefit of using a DataDriver is that you can simply use your game data-model to persist or transfer game state and objects, without serializing the physics engine state.
10+
11+
Here is a simple example of how to use the DataDriver:
12+
13+
```javascript
14+
const world = new World();
15+
16+
// create a driver, with key and callback functions
17+
const driver = new DataDriver({
18+
key: function(d) {
19+
return d.id;
20+
},
21+
create: function(d) {
22+
const body = world.createBody({
23+
type: "dynamic",
24+
angle: d.angle,
25+
userData: d
26+
});
27+
body.createFixture({
28+
shape: new Circle(),
29+
density: 1.0
30+
});
31+
return body;
32+
},
33+
update: function(d, body) {
34+
body.setAngle(d.angle);
35+
},
36+
destroy: function(d) {
37+
world.destroyBody(d);
38+
}
39+
});
40+
41+
// this will create blue and red bodies
42+
driver.data([
43+
{ id: "player-blue", type: "player", angle: 0.12 * Math.PI },
44+
{ id: "player-red", type: "player", angle: 0.53 * Math.PI }
45+
]);
46+
47+
// this will remove red, update blue, and create yellow
48+
driver.data([
49+
{ id: "player-red", type: "player", angle: 0.58 * Math.PI },
50+
{ id: "player-yellow", type: "player", angle: 0.72 * Math.PI },
51+
]);
52+
```
53+
54+
DataDriver is used in multiple demo games in the Planck.js repository, please see the /example directory for more information.
55+
56+
DataDriver was introduced in Planck v1.3.

0 commit comments

Comments
 (0)