-
Notifications
You must be signed in to change notification settings - Fork 241
/
HelloWorld.ts
91 lines (73 loc) · 2.63 KB
/
HelloWorld.ts
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
/*
* Planck.js
*
* Copyright (c) Erin Catto, Ali Shakiba
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/*
* This is a simple example of building and running a simulation
* using Planck.js. Here we create a large ground box and a small dynamic
* box.
* There are no graphics for this example. Planck.js is meant to be used
* with your rendering engine in your game engine.
*
* To run this example simply run `node HelloWorld.js` from command line.
*/
import { Box, World, } from "./src";
// Define the gravity vector.
var gravity = {x: 0.0, y: -10.0};
// Construct a world object, which will hold and simulate the rigid bodies.
var world = new World({
gravity: gravity
});
// Define the ground body.
var groundBodyDef = {
position: {x: 0.0, y: -10.0}
};
// Call the body factory which allocates memory for the ground body
// from a pool and creates the ground box shape (also from a pool).
// The body is also added to the world.
var groundBody = world.createBody(groundBodyDef);
// Define the ground box shape.
// The extents are the half-widths of the box.
var groundBox = new Box(50.0, 10.0);
// Add the ground fixture to the ground body.
groundBody.createFixture(groundBox, 0.0);
// Define the dynamic body. We set its position and call the body factory.
var body = world.createBody({
type: "dynamic",
position: {x:0.0, y: 4.0},
});
// Define another box shape for our dynamic body.
var dynamicBox = new Box(1.0, 1.0);
// Define the dynamic body fixture.
var fixtureDef = {
shape: dynamicBox,
// Set the box density to be non-zero, so it will be dynamic.
density: 1.0,
// Override the default friction.
friction: 0.3,
};
// Add the shape to the body.
body.createFixture(fixtureDef);
// Prepare for simulation. Typically we use a time step of 1/60 of a
// second (60Hz) and 10 iterations. This provides a high quality simulation
// in most game scenarios.
var timeStep = 1.0 / 60.0;
var velocityIterations = 6;
var positionIterations = 2;
// This is our little game loop.
for (var i = 0; i < 60; ++i) {
// Instruct the world to perform a single step of simulation.
// It is generally best to keep the time step and iterations fixed.
world.step(timeStep, velocityIterations, positionIterations);
// Now print the position and angle of the body.
var position = body.getPosition();
var angle = body.getAngle();
console.log(position.x.toFixed(2), position.y.toFixed(2), angle.toFixed(2));
}
console.log(Math.abs(position.x) < 0.01);
console.log(Math.abs(position.y - 1.01) < 0.01);
console.log(Math.abs(angle) < 0.01);