It's not C++ code or classes: Chipmunk Physics is an (almost?) ANSI-C
library. It's using some more complex types wrapped as structs (point,
body, etc.).
It's also not object oriented...
Anyway, creating a simple wrapper seems to work:
#### JS Wrappers
var cpv = function (x, y) {
var v = STACKTOP;
STACKTOP += 16;
_cpv(v, x, y);
var o = {};
o.ptr = v;
Object.defineProperty(o, 'x', {
get: cpv.getX,
set: cpv.setX
});
Object.defineProperty(o, 'y', {
get: cpv.getY,
set: cpv.setY
});
return o;
};
cpv.getX = function () {
v = this.ptr;
tempDoubleI32[0] = HEAP32[v >> 2];
tempDoubleI32[1] = HEAP32[v + 4 >> 2];
return tempDoubleF64[0];
};
cpv.getY = function () {
v = this.ptr;
tempDoubleI32[0] = HEAP32[v + 8 >> 2];
tempDoubleI32[1] = HEAP32[v + 12 >> 2];
return tempDoubleF64[0];
};
cpv.setX = function (x) {
v = this.ptr;
tempDoubleF64[0] = x, HEAP32[v >> 2] = tempDoubleI32[0], HEAP32[v +
4 >> 2] = tempDoubleI32[1];
};
cpv.setY = function (y) {
v = this.ptr;
tempDoubleF64[0] = y, HEAP32[v + 8 >> 2] = tempDoubleI32[0],
HEAP32[v + 12 >> 2] = tempDoubleI32[1];
};
var cpvstr = function (p) {
assert(p.ptr !== undefined);
var str = _cpvstr(p.x, p.y);
return Pointer_stringify(str);
};
var pt1 = cpv(13.37, 37.1387);
var pt2 = cpv(5500, 1300);
console.log("pt1: " + cpvstr(pt1));
console.log("pt2: " + cpvstr(pt2));
pt1.x = pt2.y;
pt2.y = pt1.x;
console.log("pt1: " + cpvstr(pt1));
console.log("pt2: " + cpvstr(pt2));
#####
output:
$ node cpVect.js
pt1: (13.370, 37.139)
pt2: (5500.000, 1300.000)
pt1: (1300.000, 37.139)
pt2: (5500.000, 1300.000)
#####
So maybe I'll go that way :) - not exactly the best/fastest solution,
but seems to do the trick.
Thanks,
Rolando