Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 113 lines (80 sloc) 3.511 kb
2fcc969 @josephg Added a gitignore file and readme
authored
1 # Chipmunk for Javascript!
2
3 This is a port of the [Chipmunk Physics](http://chipmunk-physics.net/) library to Javascript!
4
5 ## Caveats
6
e034661 @josephg Updated README and bumped version to 0.1.0
authored
7 - It is slower than the C version (duh, its in javascript). Specifically, physics simulations using chipmunk-js runs about 3 times slower than the C equivalent.
8 - I haven't implemented chipmunk's spatial hash, and I have no intention to do so.
2fcc969 @josephg Added a gitignore file and readme
authored
9
10 # Usage
11
12 The API for Chipmunk-js is almost identical to chipmunk-physics. Except:
13
14 - The `cp` prefix has gone
15 - Most functions are wrapped by their containing objects
16 - Some functions took an array-and-length. Arrays are now all simply javascript arrays, and the length argument has been removed. Eg:
17
169065c @josephg Tweaked readme
authored
18 ```c
2fcc969 @josephg Added a gitignore file and readme
authored
19 cpMomentForPoly(mass, numVerts, *verts, offset);
169065c @josephg Tweaked readme
authored
20 ```
2fcc969 @josephg Added a gitignore file and readme
authored
21
169065c @josephg Tweaked readme
authored
22 is now simply:
2fcc969 @josephg Added a gitignore file and readme
authored
23
169065c @josephg Tweaked readme
authored
24 ```javascript
2fcc969 @josephg Added a gitignore file and readme
authored
25 momentForPoly(mass, verts, offset);
169065c @josephg Tweaked readme
authored
26 ```
2fcc969 @josephg Added a gitignore file and readme
authored
27
e034661 @josephg Updated README and bumped version to 0.1.0
authored
28 - Lots of trivial getters and setters have been removed. Access the property directly.
e22c965 @josephg More readme stuff
authored
29
87f1ef8 @josephg Added more stuff to the readme
authored
30 ## On a website
31
32 ```html
33 <script src="cp.min.js"></script>
34
35 <script>
36 var space = new cp.Space();
37 space.addBody(new cp.Body(...))
38 // ...
39 </script>
40 ```
41
42 If any exceptions are thrown or anything, use `cp.js` instead and post the stack trace you get to the issue page.
43
44 ## From nodejs
45
46 `npm install chipmunk`
47
48 ```javascript
49 cp = require('chipmunk');
50
51 var space = new cp.Space();
52 space.addBody(new cp.Body(...))
53 // ...
54 ```
55
56 # Example
2fcc969 @josephg Added a gitignore file and readme
authored
57
58 This code creates a new space, sets 10 physics iterations per update (these increase simulation stability). Then it adds a bunch of line segments to the space.
59
60 In C, this code looked like:
61
62 ```C
87f1ef8 @josephg Added more stuff to the readme
authored
63 cpSpace *space = cpSpaceNew();
169065c @josephg Tweaked readme
authored
64 space->iterations = 10;
65
66 cpVect offset = cpv(-320, -240);
67 for(int i=0; i<(bouncy_terrain_count - 1); i++){
68 cpVect a = bouncy_terrain_verts[i], b = bouncy_terrain_verts[i+1];
e22c965 @josephg More readme stuff
authored
69 cpShape *shape = cpSpaceAddShape(space, cpSegmentShapeNew(cpSpaceGetStaticBody(space), cpvadd(a, offset), cpvadd(b, offset), 0.0f));
2fcc969 @josephg Added a gitignore file and readme
authored
70 cpShapeSetElasticity(shape, 1.0f);
169065c @josephg Tweaked readme
authored
71 }
2fcc969 @josephg Added a gitignore file and readme
authored
72 ```
73
74 In javascript, the equivalent code is:
75
76 ```javascript
87f1ef8 @josephg Added more stuff to the readme
authored
77 var space = new cp.Space();
169065c @josephg Tweaked readme
authored
78 space.iterations = 10;
79
6189306 @josephg Minor tweaks to readme
authored
80 var offset = cp.v(-320, -240);
169065c @josephg Tweaked readme
authored
81 for(var i=0; i<(bouncy_terrain_verts.length - 1); i++){
82 var a = bouncy_terrain_verts[i], b = bouncy_terrain_verts[i+1];
83 var shape = space.addShape(new cp.SegmentShape(space.staticBody, cp.vadd(a, offset), cp.vadd(b, offset), 0));
84 shape.setElasticity(1);
85 }
2fcc969 @josephg Added a gitignore file and readme
authored
86 ```
87
88 # License
89
90 Like Chipmunk, chipmunk-js is MIT licensed.
91
92 ```
93 Copyright (c) 2007 Scott Lembcke and Joseph Gentle
94
95 Permission is hereby granted, free of charge, to any person obtaining a copy
96 of this software and associated documentation files (the "Software"), to deal
97 in the Software without restriction, including without limitation the rights
98 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
99 copies of the Software, and to permit persons to whom the Software is
100 furnished to do so, subject to the following conditions:
101
102 The above copyright notice and this permission notice shall be included in
103 all copies or substantial portions of the Software.
104
105 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
106 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
107 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
108 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
109 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
110 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
111 SOFTWARE.
112 ```
Something went wrong with that request. Please try again.