• Stars
    star
    537
  • Rank 82,649 (Top 2 %)
  • Language
    JavaScript
  • Created almost 13 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Port of slembcke/Chipmunk-Physics to Javascript

Chipmunk for Javascript!

This is a port of the Chipmunk Physics library to Javascript!

Check out the sexy demos! (Surprisingly, works best in Safari)

Caveats

  • 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.
  • I haven't implemented chipmunk's spatial hash, and I have no intention to do so.
  • The feature set is lagged a little from the real Chipmunk library. Chipmunk-js currently corresponds to Chipmunk 6.1.1 published in August 2012.

Usage

The API for Chipmunk-js is almost identical to chipmunk-physics. Except:

  • The cp prefix has been replaced by a cp namespace. (Eg, cpvadd(a, b) becomes cp.vadd(a, b).)
  • Most functions are wrapped by their containing objects
  • Some functions took an array-and-length. Arrays are now all simply javascript arrays, and the length argument has been removed. Eg:
cpMomentForPoly(mass, numVerts, *verts, offset);

becomes:

cp.momentForPoly(mass, verts, offset);
  • Lots of trivial getters and setters have been removed.
  • Lists of verticies are described using a flattened JS array of [x1,y1, x2,y2, ...] instead of an array of objects. For example, use [0,0, 0,1, 1,1, 1,0] instead of [cp.v(0,0), cp.v(0,1), cp.v(1,1), cp.v(1,0)].

On a website

<script src="cp.min.js"></script>

<script>
var space = new cp.Space();
space.addBody(new cp.Body(...))
// ...
</script>

If any exceptions are thrown or anything, use cp.js instead of cp.min.js and post the stack trace you get to the issue page.

From nodejs

npm install chipmunk

cp = require('chipmunk');

var space = new cp.Space();
space.addBody(new cp.Body(...))
// ...

Example

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.

In C, this code looked like:

  cpSpace *space = cpSpaceNew();
  space->iterations = 10;
  
  cpVect offset = cpv(-320, -240);
  for(int i=0; i<(bouncy_terrain_count - 1); i++){
    cpVect a = bouncy_terrain_verts[i], b = bouncy_terrain_verts[i+1];
    cpShape *shape = cpSpaceAddShape(space, cpSegmentShapeNew(cpSpaceGetStaticBody(space), cpvadd(a, offset), cpvadd(b, offset), 0.0f));
    cpShapeSetElasticity(shape, 1.0f);
  }

In javascript, the equivalent code is:

  var space = new cp.Space();
  space.iterations = 10;
  
  var offset = cp.v(-320, -240);
  for(var i=0; i<(bouncy_terrain_verts.length - 1); i++){
    var a = bouncy_terrain_verts[i], b = bouncy_terrain_verts[i+1];
    var shape = space.addShape(new cp.SegmentShape(space.staticBody, cp.vadd(a, offset), cp.vadd(b, offset), 0));
    shape.setElasticity(1);
  }

License

Like Chipmunk, chipmunk-js is MIT licensed.

Copyright (c) 2007 Scott Lembcke and Joseph Gentle

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

More Repositories

1

ShareJS

Collaborative editing in any app
JavaScript
4,929
star
2

noisejs

Javascript 2D Perlin & Simplex noise functions
JavaScript
1,569
star
3

diamond-types

The world's fastest CRDT. WIP.
Rust
1,194
star
4

node-browserchannel

An implementation of a google browserchannel server in node.js
CoffeeScript
287
star
5

librope

UTF-8 rope library for C
C
254
star
6

statecraft

Manage state with finesse
TypeScript
177
star
7

sephsplace

My own version of r/place, done in a weekend
JavaScript
130
star
8

jumprope-rs

Rust
104
star
9

reference-crdts

Simple, tiny spec-compliant reference implementations of Yjs and Automerge's list types.
TypeScript
98
star
10

jumprope

Fast string editing in Javascript using skip lists
JavaScript
86
star
11

steamdance

A multiplayer steam-based CPU simulator
JavaScript
45
star
12

editing-traces

Real world text editing traces for benchmarking CRDT and Rope data structures
JavaScript
39
star
13

schemaboi

Mergable, efficient data schema for intercompatible apps
TypeScript
33
star
14

braid-protocol

TypeScript
28
star
15

textot.rs

Text operational transform library, for rust. Compatible with libot, ottypes/text.
Rust
26
star
16

sharedb

ShareJS server, in C.
C
24
star
17

appstate

CoffeeScript
24
star
18

replica

Local first application platform built using CRDTs
TypeScript
18
star
19

wangjs

A javascript Wang tile implementation
CoffeeScript
17
star
20

eg-walker-reference

Simple reimplementation of the diamond types sequence CRDT in simple, pure, unoptimized typescript.
TypeScript
16
star
21

mail-viewer

Pure browser email viewer
JavaScript
15
star
22

diamond-js

Javascript wrapper bindings for diamond types
14
star
23

fdb-tuple

Pure javascript FoundationDB tuple encoder and decoder
TypeScript
12
star
24

node-timerstub

Stubbed out timer objects for fast unit testing in nodejs
CoffeeScript
11
star
25

gmail-jmap

Simple JMAP wrapper for gmail
TypeScript
10
star
26

braid-db

Simple database prototype
Rust
10
star
27

unicount

JavaScript
9
star
28

react-static-example

JavaScript
9
star
29

mime-to-jmap

Tools to convert RFC2822 email messages to JMAP
C
9
star
30

unsplash

JavaScript
8
star
31

egwalker-paper

Eg-walker paper, experiments and data.
Rust
8
star
32

tanksalot

CoffeeScript
7
star
33

lightwave

Fork of Torben Weis's lightwave experimental branch - http://code.google.com/p/lightwave/
Go
7
star
34

TP2

An implementation of OT for text which supports TP2 (for p2p editing)
CoffeeScript
6
star
35

text-crdt-rust2

Rust
6
star
36

miniohm

JavaScript
6
star
37

simple-crdt-text

Simple typescript reference implementation for a simple RGA based plain text CRDT
TypeScript
6
star
38

axidraw

Python
5
star
39

livedb-foundation

CoffeeScript
5
star
40

dt-simple-wiki

World's simplest wiki on top of diamond types
JavaScript
5
star
41

crdt-examples

CRDT examples from a DWEB talk
JavaScript
5
star
42

glassbeadtimer

Svelte
4
star
43

killdinohitler

Stealth jam game
CoffeeScript
4
star
44

lovehot

Lua
4
star
45

keykitten

Stores your public key.
4
star
46

ARTIST

Michael Brough and Andi McClure's 10 second artist game, allowing a starting image
Lua
4
star
47

skiplistrs

Rust
4
star
48

map2

Tuple map type for javascript (a,b) -> x
TypeScript
3
star
49

gossip

Simple proof-of-concept gossip protocol for CRDTs
TypeScript
3
star
50

tp2stuff

CoffeeScript
3
star
51

set2

Tuple set type for javascript
JavaScript
3
star
52

resolvable

TypeScript
2
star
53

libot-swift

Swift
2
star
54

roboname

A simple robot-themed name generator for nodejs
CoffeeScript
2
star
55

braid-explorer

UI experiment with braid for data first software
TypeScript
2
star
56

space

Spaaaaaaaace
JavaScript
2
star
57

robot-party

Robots run robots.
CoffeeScript
2
star
58

streamtoiter

TypeScript
2
star
59

Attila

a little game prototype
CoffeeScript
2
star
60

wave-prototype

Simple prototype of a wave-like email service
TypeScript
2
star
61

braid-kernel

TypeScript
2
star
62

canvasproxy

CoffeeScript
2
star
63

space-server

C
2
star
64

census-couch

A simple couchapp site for exploring census data. Way incomplete.
JavaScript
1
star
65

state-client

TypeScript
1
star
66

Clickboss

Click on the boss.
CoffeeScript
1
star
67

FactoryFactoryImpl

The code from my minecraft machine that makes anything
1
star
68

braid-cli

JavaScript
1
star
69

mars2

JavaScript
1
star
70

boilerplate-sim

Boilerplate simulator reference implementation
CoffeeScript
1
star
71

ietf-sync-prototype

JavaScript
1
star
72

boilerplate-gif

CoffeeScript
1
star
73

mtgox

CoffeeScript
1
star
74

boilerplate-jit

CoffeeScript
1
star
75

raider

Simple ECS-style game prototype for multiplayer dungeon raid type things. https://home.seph.codes/public/game/
Rust
1
star
76

state-server

TypeScript
1
star
77

Cocontroller

Userland USB controller for XBox360 controllers on a mac.
Objective-C
1
star
78

boilerplate-compiler

CoffeeScript
1
star
79

visbindiff

CoffeeScript
1
star
80

rle-utils

Javascript utilities for internal run-length encoding
TypeScript
1
star
81

mars

CSS
1
star
82

boilerbot

CoffeeScript
1
star