Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Furthermore, JavaScript friendly APIs make your game development experience a br

- Cocos2d-JS v3.0 uses Cocos2d-x 3.2 final as base of JSB solution
- Cocos2d-JS v3.0 is compatible with Cocos Code IDE v1.0.0 RC2+
- Cocos2d-JS v3.0 is compatible with Cocos Studio v1.2 - v1.5.0.1
- Cocos2d-JS v3.0 is compatible with Cocos Studio v1.2 - v2.0

With any problems you might have, our communities are happy to help:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4917,6 +4917,26 @@ bool JSB_cpSegmentShape_setNeighbors(JSContext *cx, uint32_t argc, jsval *vp) {
return true;
}

static bool js_get_cpSegmentShape_a_tangent(JSContext *cx, JS::HandleObject obj, JS::HandleId id, JS::MutableHandleValue vp)
{
struct jsb_c_proxy_s *proxy = jsb_get_c_proxy_for_jsobject(obj);
cpSegmentShape* shape = (cpSegmentShape*) proxy->handle;
cpVect vec = shape->a_tangent;
jsval ret = cpVect_to_jsval( cx, vec);
vp.set(ret);
return true;
}

static bool js_get_cpSegmentShape_b_tangent(JSContext *cx, JS::HandleObject obj, JS::HandleId id, JS::MutableHandleValue vp)
{
struct jsb_c_proxy_s *proxy = jsb_get_c_proxy_for_jsobject(obj);
cpSegmentShape* shape = (cpSegmentShape*) proxy->handle;
cpVect vec = shape->b_tangent;
jsval ret = cpVect_to_jsval( cx, vec);
vp.set(ret);
return true;
}

void JSB_cpSegmentShape_createClass(JSContext *cx, JSObject* globalObj, const char* name )
{
JSB_cpSegmentShape_class = (JSClass *)calloc(1, sizeof(JSClass));
Expand All @@ -4932,6 +4952,8 @@ void JSB_cpSegmentShape_createClass(JSContext *cx, JSObject* globalObj, const ch
JSB_cpSegmentShape_class->flags = JSCLASS_HAS_PRIVATE;

static JSPropertySpec properties[] = {
{"a_tangent", 0, JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_SHARED, JSOP_WRAPPER(js_get_cpSegmentShape_a_tangent), JSOP_NULLWRAPPER},
{"b_tangent", 0, JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_SHARED, JSOP_WRAPPER(js_get_cpSegmentShape_b_tangent), JSOP_NULLWRAPPER},
{0, 0, 0, 0, 0}
};
static JSFunctionSpec funcs[] = {
Expand Down Expand Up @@ -5014,6 +5036,59 @@ bool JSB_cpPolyShape_getVert(JSContext *cx, uint32_t argc, jsval *vp) {
return true;
}

static bool js_get_cpPolyShape_verts(JSContext *cx, JS::HandleObject obj, JS::HandleId id, JS::MutableHandleValue vp)
{
struct jsb_c_proxy_s *proxy = jsb_get_c_proxy_for_jsobject(obj);
cpPolyShape* shape = (cpPolyShape*) proxy->handle;
int numVerts = shape->numVerts;
cpVect* verts = shape->verts;

JSObject *jsretArr = JS_NewArrayObject(cx, 0, NULL);
int i = 0;
while (i < numVerts) {
cpVect vec = verts[i];

JS::RootedValue x(cx);
JS::RootedValue y(cx);
x = DOUBLE_TO_JSVAL(vec.x);
y = DOUBLE_TO_JSVAL(vec.y);
JS_SetElement(cx, jsretArr, i*2, &x);
JS_SetElement(cx, jsretArr, i*2+1, &y);
i++;
}
vp.set(OBJECT_TO_JSVAL(jsretArr));
return true;
}

static bool js_get_cpPolyShape_planes(JSContext *cx, JS::HandleObject obj, JS::HandleId id, JS::MutableHandleValue vp)
{
struct jsb_c_proxy_s *proxy = jsb_get_c_proxy_for_jsobject(obj);
cpPolyShape* shape = (cpPolyShape*) proxy->handle;
int numVerts = shape->numVerts;
cpSplittingPlane* planes = shape->planes;

JSObject *jsretArr = JS_NewArrayObject(cx, 0, NULL);
int i = 0;
while(i < numVerts){
cpSplittingPlane *plane = planes + i;
JS::RootedValue elem(cx);

JSObject *jsobj = jsb_get_jsobject_for_proxy(plane);
if(!jsobj)
{
jsobj = JS_NewObject(cx, JSB_cpSplittingPlane_class, JSB_cpSplittingPlane_object, NULL);
jsb_set_jsobject_for_proxy(jsobj, plane);
jsb_set_c_proxy_for_jsobject(jsobj, plane, JSB_C_FLAG_DO_NOT_CALL_FREE);
}

elem = OBJECT_TO_JSVAL(jsobj);
JS_SetElement(cx, jsretArr, i, &elem);
i++;
}
vp.set(OBJECT_TO_JSVAL(jsretArr));
return true;
}

void JSB_cpPolyShape_createClass(JSContext *cx, JSObject* globalObj, const char* name )
{
JSB_cpPolyShape_class = (JSClass *)calloc(1, sizeof(JSClass));
Expand All @@ -5029,6 +5104,8 @@ void JSB_cpPolyShape_createClass(JSContext *cx, JSObject* globalObj, const char*
JSB_cpPolyShape_class->flags = JSCLASS_HAS_PRIVATE;

static JSPropertySpec properties[] = {
{"verts", 0, JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_SHARED, JSOP_WRAPPER(js_get_cpPolyShape_verts), JSOP_NULLWRAPPER},
{"planes", 0, JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_SHARED, JSOP_WRAPPER(js_get_cpPolyShape_planes), JSOP_NULLWRAPPER},
{0, 0, 0, 0, 0}
};
static JSFunctionSpec funcs[] = {
Expand All @@ -5045,6 +5122,70 @@ void JSB_cpPolyShape_createClass(JSContext *cx, JSObject* globalObj, const char*
// JS_SetPropertyAttributes(cx, globalObj, name, JSPROP_ENUMERATE | JSPROP_READONLY, &found);
}

// SplittingPlane
JSObject *JSB_cpSplittingPlane_object = NULL;
JSClass *JSB_cpSplittingPlane_class = NULL;

// Destructor
void JSB_cpSplittingPlane_finalize(JSFreeOp *fop, JSObject *jsthis)
{
jsb_c_proxy_s *proxy = jsb_get_c_proxy_for_jsobject(jsthis);
if( proxy ) {
CCLOGINFO("jsbindings: finalizing JS object %p (cpSplittingPlane), handle: %p", jsthis, proxy->handle);

jsb_del_jsobject_for_proxy(proxy->handle);
jsb_del_c_proxy_for_jsobject(jsthis);

//no need to free cpSplittingPlane, cpSplittingPlane will be freed by it's shape
}
}

bool js_get_cpSplitting_n(JSContext *cx, JS::HandleObject obj, JS::HandleId id, JS::MutableHandleValue vp)
{
struct jsb_c_proxy_s *proxy = jsb_get_c_proxy_for_jsobject(obj);
cpSplittingPlane* plane = (cpSplittingPlane*) proxy->handle;
cpVect vec = plane->n;
vp.set(cpVect_to_jsval(cx, vec));
return true;
}

bool js_get_cpSplitting_d(JSContext *cx, JS::HandleObject obj, JS::HandleId id, JS::MutableHandleValue vp)
{
struct jsb_c_proxy_s *proxy = jsb_get_c_proxy_for_jsobject(obj);
cpSplittingPlane* plane = (cpSplittingPlane*) proxy->handle;
vp.set(DOUBLE_TO_JSVAL(plane->d));
return true;
}

void JSB_cpSplittingPlane_createClass(JSContext *cx, JSObject* globalObj, const char* name )
{
JSB_cpSplittingPlane_class = (JSClass *)calloc(1, sizeof(JSClass));
JSB_cpSplittingPlane_class->name = name;
JSB_cpSplittingPlane_class->addProperty = JS_PropertyStub;
JSB_cpSplittingPlane_class->delProperty = JS_DeletePropertyStub;
JSB_cpSplittingPlane_class->getProperty = JS_PropertyStub;
JSB_cpSplittingPlane_class->setProperty = JS_StrictPropertyStub;
JSB_cpSplittingPlane_class->enumerate = JS_EnumerateStub;
JSB_cpSplittingPlane_class->resolve = JS_ResolveStub;
JSB_cpSplittingPlane_class->convert = JS_ConvertStub;
JSB_cpSplittingPlane_class->finalize = JSB_cpSplittingPlane_finalize;
JSB_cpSplittingPlane_class->flags = JSCLASS_HAS_PRIVATE;

static JSPropertySpec properties[] = {
{"n", 0, JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_SHARED, JSOP_WRAPPER(js_get_cpSplitting_n), JSOP_NULLWRAPPER},
{"d", 0, JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_SHARED, JSOP_WRAPPER(js_get_cpSplitting_d), JSOP_NULLWRAPPER},
{0, 0, 0, 0, 0}
};
static JSFunctionSpec funcs[] = {
JS_FS_END
};
static JSFunctionSpec st_funcs[] = {
JS_FS_END
};

JSB_cpSplittingPlane_object = JS_InitClass(cx, globalObj, NULL, JSB_cpSplittingPlane_class, NULL,0,properties,funcs,NULL,st_funcs);
}

bool JSB_cpSegmentQueryInfo_hitPoint(JSContext *cx, uint32_t argc, jsval *vp){
JSB_PRECONDITION2( argc == 2, cx, false, "Invalid number of arguments" );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,7 @@ void JSB_cpSegmentQueryInfo_createClass(JSContext *cx, JSObject* globalObj, cons
extern JSObject *JSB_cpNearestPointQueryInfo_object;
extern JSClass *JSB_cpNearestPointQueryInfo_class;
void JSB_cpNearestPointQueryInfo_createClass(JSContext *cx, JSObject* globalObj, const char* name );
extern JSObject *JSB_cpSplittingPlane_object;
extern JSClass *JSB_cpSplittingPlane_class;
void JSB_cpSplittingPlane_createClass(JSContext *cx, JSObject* globalObj, const char* name );
#endif // JSB_INCLUDE_CHIPMUNK
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ JSB_cpSegmentShape_createClass(cx, chipmunk, "SegmentShape");
JSB_cpPolyShape_createClass(cx, chipmunk, "PolyShape");
JSB_cpSegmentQueryInfo_createClass(cx, chipmunk, "SegmentQueryInfo");
JSB_cpNearestPointQueryInfo_createClass(cx, chipmunk, "NearestPointQueryInfo");
JSB_cpSplittingPlane_createClass(cx, chipmunk, "SplittingPlane");
#endif // JSB_INCLUDE_CHIPMUNK
11 changes: 11 additions & 0 deletions frameworks/js-bindings/bindings/script/jsb_chipmunk.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,17 @@ _proto.type = "circle";
cc.defineGetterSetter(_proto, "r", _proto.getRadius);
cc.defineGetterSetter(_proto, "c", _proto.getOffset);

//SegmentShape properties
_proto = cp.SegmentShape.prototype;
_proto.type = "segment";
cc.defineGetterSetter(_proto, "a", _proto.getA);
cc.defineGetterSetter(_proto, "b", _proto.getB);
cc.defineGetterSetter(_proto, "n", _proto.getNormal);
cc.defineGetterSetter(_proto, "r", _proto.getRadius);

//PolyShape properties
_proto = cp.PolyShape.prototype;
_proto.type = "poly";
// Constraint properties
Object.defineProperties(cp.Constraint.prototype,
{
Expand Down
53 changes: 52 additions & 1 deletion samples/js-tests/src/ChipmunkTest/ChipmunkTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -1792,6 +1792,56 @@ var Issue1073 = ChipmunkDemo.extend({
}
});

var Issue1083 = ChipmunkDemo.extend({
ctor:function(){
this._super();
this._subtitle = 'Chipmunk Demo';
this._title = 'Issue 1083';

var space = this.space;

//add a segment
var mass = 1;
var length = 100;
var a = v(-length/2, 0), b = v(length/2, 0);
var body = space.addBody(new cp.Body(mass, cp.momentForSegment(mass, a, b)));
body.setPos(v(320, 340));
var segment = new cp.SegmentShape(body, a, b, 20);
space.addShape(segment);

//add a poly
var mass = 1;
var NUM_VERTS = 5;
var verts = new Array(NUM_VERTS * 2);
for(var i=0; i<NUM_VERTS*2; i+=2){
var angle = -Math.PI*i/NUM_VERTS;
verts[i] = 30*Math.cos(angle);
verts[i+1] = 30*Math.sin(angle);
}
var body = space.addBody(new cp.Body(mass, cp.momentForPoly(mass, verts, v(0,0))));
body.setPos(v(350+60, 220+60));
var poly = new cp.PolyShape(body, verts, v(0,0));
space.addShape(poly);

cc.assert(segment.a.x == -length/2, "SegmentShape assertion failed : a.x");
cc.assert(segment.a.y == 0, "SegmentShape assertion failed : a.y");
cc.assert(segment.b.x == length/2, "SegmentShape assertion failed : b.x");
cc.assert(segment.b.y == 0, "SegmentShape assertion failed : b.y");
var nomal = cp.v.perp(cp.v.normalize(cp.v.sub(b, a)));
cc.assert(segment.n.x == nomal.x, "SegmentShape assertion failed : n.x");
cc.assert(segment.n.y == nomal.y, "SegmentShape assertion failed : n.y");
cc.assert(segment.r == 20, "SegmentShape assertion failed : r");

for(var i = 0; i < verts.length; ++i){
cc.assert(verts[i] == poly.verts[i],"PolyShape assertion failed : verts");
}

var plane = poly.planes[0];
cc.assert(plane.d.toFixed(4) == 24.2705, "PolyShape assertion failed : planes d");
cc.assert(plane.n.x.toFixed(4) == 0.8090, "PolyShape assertion failed : planes n");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need a onExit function to clean up the space, otherwise the test will crash if we click several time the restart button.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why need an onExit to clean. It's not Javascirpt idiom, if it's needed, it's a BUG.
And I tested, the test does crash after clicking several times.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a bug in binding of planes, if comment planes in the test code, it will not crash.

});

// Chipmunk Demos
var arrayOfChipmunkTest = [

Expand All @@ -1812,7 +1862,8 @@ var arrayOfChipmunkTest = [
ChipmunkCollisionMemoryLeakTest,
ChipmunkSpriteAnchorPoint,

Issue1073
Issue1073,
Issue1083
];

if( cc.sys.isNative ) {
Expand Down