Merge pull request #12424 from pandamicro/physics3d

Fix Physics3D test and js bindings
This commit is contained in:
pandamicro 2015-06-18 16:09:49 +08:00
commit aa566a3722
8 changed files with 290 additions and 17 deletions

View File

@ -3891,6 +3891,89 @@ bool js_cocos2dx_ccmat4MultiplyVec3(JSContext *cx, uint32_t argc, jsval *vp)
return false;
}
bool js_cocos2dx_ccmat4GetInversed(JSContext *cx, uint32_t argc, jsval *vp)
{
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
if(argc == 1)
{
cocos2d::Mat4 arg0;
bool ok = jsval_to_matrix(cx, args.get(0), &arg0);
JSB_PRECONDITION2(ok, cx, false, "Error processing arguments");
jsval jsret = matrix_to_jsval(cx, arg0.getInversed());
args.rval().set(jsret);
return true;
}
JS_ReportError(cx, "wrong number of arguments: %d, was expecting %d", argc, 1);
return false;
}
bool js_cocos2dx_ccmat4TransformVector(JSContext *cx, uint32_t argc, jsval *vp)
{
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
if(argc >= 2)
{
cocos2d::Mat4 arg0;
cocos2d::Vec4 arg1;
cocos2d::Vec4 ret;
bool ok = jsval_to_matrix(cx, args.get(0), &arg0);
ok &= jsval_to_vector4(cx, args.get(1), &arg1);
JSB_PRECONDITION2(ok, cx, false, "Error processing arguments");
arg0.transformVector(arg1, &ret);
jsval jsret = vector4_to_jsval(cx, ret);
args.rval().set(jsret);
return true;
}
else if (argc >= 5)
{
cocos2d::Mat4 arg0;
double arg1 = 0.0,arg2 = 0.0,arg3 = 0.0, arg4 = 0.0;
cocos2d::Vec3 ret;
bool ok = jsval_to_matrix(cx, args.get(0), &arg0) &&
JS::ToNumber(cx, args.get(1), &arg1) &&
JS::ToNumber(cx, args.get(2), &arg2) &&
JS::ToNumber(cx, args.get(3), &arg3) &&
JS::ToNumber(cx, args.get(4), &arg4);
JSB_PRECONDITION2(ok, cx, false, "Error processing arguments");
arg0.transformVector(arg1, arg2, arg3, arg4, &ret);
jsval jsret = vector3_to_jsval(cx, ret);
args.rval().set(jsret);
return true;
}
JS_ReportError(cx, "wrong number of arguments: %d, was expecting %d", argc, 3);
return false;
}
bool js_cocos2dx_ccmat4TransformPoint(JSContext *cx, uint32_t argc, jsval *vp)
{
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
if(argc >= 2)
{
cocos2d::Mat4 arg0;
cocos2d::Vec3 arg1;
cocos2d::Vec3 ret;
bool ok = jsval_to_matrix(cx, args.get(0), &arg0);
ok &= jsval_to_vector3(cx, args.get(1), &arg1);
JSB_PRECONDITION2(ok, cx, false, "Error processing arguments");
arg0.transformPoint(arg1, &ret);
jsval jsret = vector3_to_jsval(cx, ret);
args.rval().set(jsret);
return true;
}
JS_ReportError(cx, "wrong number of arguments: %d, was expecting %d", argc, 3);
return false;
}
bool js_cocos2dx_ccquatMultiply(JSContext *cx, uint32_t argc, jsval *vp)
{
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
@ -5167,15 +5250,15 @@ bool js_cocos2dx_Camera_unproject(JSContext *cx, uint32_t argc, jsval *vp)
js_proxy_t *proxy = jsb_get_js_proxy(obj);
cocos2d::Camera* cobj = (cocos2d::Camera *)(proxy ? proxy->ptr : NULL);
JSB_PRECONDITION2( cobj, cx, false, "js_cocos2dx_Camera_unproject : Invalid Native Object");
if (argc == 2) {
if (argc >= 2) {
cocos2d::Size arg0;
cocos2d::Vec3 arg1;
cocos2d::Vec3 arg2;
cocos2d::Vec3 ret;
ok &= jsval_to_ccsize(cx, args.get(0), &arg0);
ok &= jsval_to_vector3(cx, args.get(1), &arg1);
JSB_PRECONDITION2(ok, cx, false, "js_cocos2dx_Camera_unproject : Error processing arguments");
cobj->unproject(arg0, &arg1, &arg2);
args.rval().set(vector3_to_jsval(cx, arg2));
cobj->unproject(arg0, &arg1, &ret);
args.rval().set(vector3_to_jsval(cx, ret));
return true;
}
else if (argc == 1)
@ -5187,7 +5270,7 @@ bool js_cocos2dx_Camera_unproject(JSContext *cx, uint32_t argc, jsval *vp)
args.rval().set(vector3_to_jsval(cx, ret));
return true;
}
JS_ReportError(cx, "js_cocos2dx_Camera_unproject : wrong number of arguments: %d, was expecting %d", argc, 2);
JS_ReportError(cx, "js_cocos2dx_Camera_unproject : wrong number of arguments: %d, was expecting %d", argc, 3);
return false;
}
@ -6237,6 +6320,9 @@ void register_cocos2dx_js_core(JSContext* cx, JS::HandleObject global)
JS_DefineFunction(cx, tmpObj, "mat4CreateRotation", js_cocos2dx_ccmat4CreateRotation, 1, JSPROP_READONLY | JSPROP_PERMANENT);
JS_DefineFunction(cx, tmpObj, "mat4Multiply", js_cocos2dx_ccmat4Multiply, 2, JSPROP_READONLY | JSPROP_PERMANENT);
JS_DefineFunction(cx, tmpObj, "mat4MultiplyVec3", js_cocos2dx_ccmat4MultiplyVec3, 2, JSPROP_READONLY | JSPROP_PERMANENT);
JS_DefineFunction(cx, tmpObj, "mat4GetInversed", js_cocos2dx_ccmat4GetInversed, 1, JSPROP_READONLY | JSPROP_PERMANENT);
JS_DefineFunction(cx, tmpObj, "mat4TransformVector", js_cocos2dx_ccmat4TransformVector, 3, JSPROP_READONLY | JSPROP_PERMANENT);
JS_DefineFunction(cx, tmpObj, "mat4TransformPoint", js_cocos2dx_ccmat4TransformPoint, 3, JSPROP_READONLY | JSPROP_PERMANENT);
JS_DefineFunction(cx, tmpObj, "quatMultiply", js_cocos2dx_ccquatMultiply, 2, JSPROP_READONLY | JSPROP_PERMANENT);
js_register_cocos2dx_EventKeyboard(cx, ccObj);

View File

@ -2718,6 +2718,22 @@ jsval vector3_to_jsval(JSContext *cx, const cocos2d::Vec3& v)
return JSVAL_NULL;
}
jsval vector4_to_jsval(JSContext *cx, const cocos2d::Vec4& v)
{
JS::RootedObject proto(cx);
JS::RootedObject parent(cx);
JS::RootedObject tmp(cx, JS_NewObject(cx, NULL, proto, parent));
if (!tmp) return JSVAL_NULL;
bool ok = JS_DefineProperty(cx, tmp, "x", v.x, JSPROP_ENUMERATE | JSPROP_PERMANENT) &&
JS_DefineProperty(cx, tmp, "y", v.y, JSPROP_ENUMERATE | JSPROP_PERMANENT) &&
JS_DefineProperty(cx, tmp, "z", v.z, JSPROP_ENUMERATE | JSPROP_PERMANENT) &&
JS_DefineProperty(cx, tmp, "w", v.z, JSPROP_ENUMERATE | JSPROP_PERMANENT);
if (ok) {
return OBJECT_TO_JSVAL(tmp);
}
return JSVAL_NULL;
}
jsval blendfunc_to_jsval(JSContext *cx, const cocos2d::BlendFunc& v)
{
JS::RootedObject proto(cx);

View File

@ -337,6 +337,7 @@ jsval std_vector_float_to_jsval( JSContext *cx, const std::vector<float>& v);
jsval matrix_to_jsval(JSContext *cx, const cocos2d::Mat4& v);
jsval vector2_to_jsval(JSContext *cx, const cocos2d::Vec2& v);
jsval vector3_to_jsval(JSContext *cx, const cocos2d::Vec3& v);
jsval vector4_to_jsval(JSContext *cx, const cocos2d::Vec4& v);
jsval blendfunc_to_jsval(JSContext *cx, const cocos2d::BlendFunc& v);
jsval vector_vec2_to_jsval(JSContext *cx, const std::vector<cocos2d::Vec2>& v);

View File

@ -61,6 +61,10 @@ bool jsval_to_physics3DRigidBodyDes(JSContext* cx, JS::HandleValue v, Physics3DR
jsval_to_matrix(cx, tmp, &m4);
des->originalTransform = m4;
}
if(JS_GetProperty(cx, jsobj, "disableSleep", &tmp))
{
des->disableSleep = tmp.toBoolean();
}
return true;
}
@ -299,6 +303,90 @@ bool js_cocos2dx_physics3d_Physics3dShape_createHeightfield(JSContext *cx, uint3
return false;
}
bool jsval_to_Physics3DWorld_HitResult(JSContext *cx, JS::HandleValue v, cocos2d::Physics3DWorld::HitResult* ret)
{
JS::RootedObject tmp(cx);
JS::RootedValue jshitPosition(cx);
JS::RootedValue jshitNormal(cx);
JS::RootedValue jshitObject(cx);
bool ok = v.isObject() &&
JS_ValueToObject(cx, v, &tmp) &&
JS_GetProperty(cx, tmp, "hitPosition", &jshitPosition) &&
JS_GetProperty(cx, tmp, "hitNormal", &jshitNormal) &&
JS_GetProperty(cx, tmp, "hitObj", &jshitObject) &&
jsval_to_vector3(cx, jshitPosition, &(ret->hitPosition)) &&
jsval_to_vector3(cx, jshitNormal, &(ret->hitNormal));
JSB_PRECONDITION2(ok, cx, false, "jsval_to_Physics3DWorld_HitResult : Error processing arguments");
js_proxy_t *proxy = jsb_get_js_proxy(jshitObject.toObjectOrNull());
ret->hitObj = (cocos2d::Physics3DObject *)(proxy ? proxy->ptr : nullptr);
return true;
}
jsval Physics3DWorld_HitResult_to_jsval(JSContext *cx, const cocos2d::Physics3DWorld::HitResult& v)
{
JS::RootedObject proto(cx);
JS::RootedObject parent(cx);
JS::RootedObject tmp(cx, JS_NewObject(cx, nullptr, proto, parent));
if (!tmp) return JSVAL_NULL;
JS::RootedValue hitPosition(cx, vector3_to_jsval(cx, v.hitPosition));
JS::RootedValue hitNormal(cx, vector3_to_jsval(cx, v.hitNormal));
JS::RootedValue hitobject(cx);
if (v.hitObj)
{
js_proxy_t *jsProxy = js_get_or_create_proxy<cocos2d::Physics3DObject>(cx, (cocos2d::Physics3DObject*)v.hitObj);
hitobject.set(OBJECT_TO_JSVAL(jsProxy->obj));
}
bool ok = JS_DefineProperty(cx, tmp, "hitPosition", hitPosition, JSPROP_ENUMERATE | JSPROP_PERMANENT) &&
JS_DefineProperty(cx, tmp, "hitNormal", hitNormal, JSPROP_ENUMERATE | JSPROP_PERMANENT) &&
JS_DefineProperty(cx, tmp, "hitObj", hitobject, JSPROP_ENUMERATE | JSPROP_PERMANENT);
if (ok) {
return OBJECT_TO_JSVAL(tmp);
}
return JSVAL_NULL;
}
bool js_cocos2dx_physics3d_Physics3DWorld_rayCast(JSContext *cx, uint32_t argc, jsval *vp)
{
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
bool ok = true;
JS::RootedObject obj(cx, args.thisv().toObjectOrNull());
js_proxy_t *proxy = jsb_get_js_proxy(obj);
cocos2d::Physics3DWorld* cobj = (cocos2d::Physics3DWorld *)(proxy ? proxy->ptr : NULL);
JSB_PRECONDITION2( cobj, cx, false, "js_cocos2dx_physics3d_Physics3DWorld_rayCast : Invalid Native Object");
if (argc >= 2) {
cocos2d::Vec3 arg0;
cocos2d::Vec3 arg1;
cocos2d::Physics3DWorld::HitResult ret;
ok &= jsval_to_vector3(cx, args.get(0), &arg0);
ok &= jsval_to_vector3(cx, args.get(1), &arg1);
JSB_PRECONDITION2(ok, cx, false, "js_cocos2dx_physics3d_Physics3DWorld_rayCast : Error processing arguments");
bool succeed = cobj->rayCast(arg0, arg1, &ret);
if (succeed)
{
jsval jsret = JSVAL_NULL;
jsret = Physics3DWorld_HitResult_to_jsval(cx, ret);
args.rval().set(jsret);
}
else
{
args.rval().set(JSVAL_FALSE);
}
return true;
}
JS_ReportError(cx, "js_cocos2dx_physics3d_Physics3DWorld_rayCast : wrong number of arguments: %d, was expecting %d", argc, 3);
return false;
}
void register_all_cocos2dx_physics3d_manual(JSContext *cx, JS::HandleObject global)
{
JS::RootedObject ccObj(cx);
@ -319,6 +407,8 @@ void register_all_cocos2dx_physics3d_manual(JSContext *cx, JS::HandleObject glob
JS_DefineFunction(cx, tmpObj, "createHeightfield", js_cocos2dx_physics3d_Physics3dShape_createHeightfield, 8, JSPROP_READONLY | JSPROP_PERMANENT);
JS_DefineFunction(cx, JS::RootedObject(cx, jsb_cocos2d_Physics3DObject_prototype), "setCollisionCallback", jsb_cocos2d_Physics3DObject_setCollisionCallback, 2, JSPROP_READONLY | JSPROP_PERMANENT);
JS_DefineFunction(cx, JS::RootedObject(cx, jsb_cocos2d_Physics3DWorld_prototype), "rayCast", js_cocos2dx_physics3d_Physics3DWorld_rayCast, 2, JSPROP_READONLY | JSPROP_PERMANENT);
}
#endif //CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION

View File

@ -41,3 +41,18 @@ cc.Physics3DComponent.PhysicsSyncFlag = {
PHYSICS_TO_NODE : 2, // align physics transform to the node
NODE_AND_NODE : 1 | 2, //pre simulation, align the physics object to the node and align the node transform according to physics object after simulation
};
cc.HitResult = function(){
this.hitPosition = cc.math.vec3();
this.hitNormal = cc.math.vec3();
this.hitObj = null;
}
cc.hitResult = function(){
return new cc.HitResult();
};
cc.Physics3DObject.PhysicsObjType = {
UNKNOWN : 0,
RIGID_BODY : 1,
};

View File

@ -41,6 +41,7 @@ var Physics3DTestDemo = cc.Layer.extend({
_camera:null,
_angle:0.0,
_needShootBox:false,
_listener: null,
ctor:function () {
this._super();
@ -52,7 +53,7 @@ var Physics3DTestDemo = cc.Layer.extend({
this._camera.setCameraFlag(cc.CameraFlag.USER1);
this.addChild(this._camera);
cc.eventManager.addListener({
this._listener = cc.eventManager.addListener({
event:cc.EventListener.TOUCH_ALL_AT_ONCE,
onTouchesBegan:this.onTouchesBegan.bind(this),
onTouchesMoved:this.onTouchesMoved.bind(this),
@ -285,10 +286,16 @@ var BasicPhysics3DDemo = Physics3DTestDemo.extend({
var Physics3DConstraintDemo = Physics3DTestDemo.extend({
_subtitle:"Physics3D Constraint",
_constraint: null,
_world: null,
ctor:function(){
this._super();
this._listener.onTouchesBegan = this.onTouchesBegan.bind(this);
this._listener.onTouchesMoved = this.onTouchesMoved.bind(this);
this._listener.onTouchesEnded = this.onTouchesEnded.bind(this);
//PhysicsSprite3d = Sprite3D + Physics3DComponent
var rbDes = cc.physics3DRigidBodyDes();
rbDes.disableSleep = true;
@ -309,10 +316,11 @@ var Physics3DConstraintDemo = Physics3DTestDemo.extend({
component.setSyncFlag(cc.Physics3DComponent.PhysicsSyncFlag.PHYSICS_TO_NODE);
physicsScene.setPhysics3DDebugCamera(this._camera);
this._world = physicsScene.getPhysics3DWorld();
//create point to point constraint
var constraint = cc.Physics3DPointToPointConstraint.create(rigidBody, cc.math.vec3(2.5, 2.5, 2.5));
physicsScene.getPhysics3DWorld().addPhysics3DConstraint(constraint);
this._world.addPhysics3DConstraint(constraint);
//create hinge constraint
rbDes.mass = 1;
@ -322,7 +330,7 @@ var Physics3DConstraintDemo = Physics3DTestDemo.extend({
sprite = new jsb.Sprite3D("Sprite3DTest/box.c3t");
sprite.setTexture("Sprite3DTest/plane.png");
sprite.setScaleX(8);
sprite.setScaleZ(8);
sprite.setScaleY(8);
sprite.setPosition3D(cc.math.vec3(5, 0, 0));
sprite.addComponent(component);
sprite.setCameraMask(cc.CameraFlag.USER1);
@ -330,7 +338,7 @@ var Physics3DConstraintDemo = Physics3DTestDemo.extend({
component.syncNodeToPhysics();
rigidBody.setAngularVelocity(cc.math.vec3(0, 3, 0));
constraint = cc.Physics3DHingeConstraint.create(rigidBody, cc.math.vec3(4, 4, 0.5), cc.math.vec3(0, 1, 0));
physicsScene.getPhysics3DWorld().addPhysics3DConstraint(constraint);
this._world.addPhysics3DConstraint(constraint);
//create slider constraint
rbDes.mass = 1;
@ -361,10 +369,10 @@ var Physics3DConstraintDemo = Physics3DTestDemo.extend({
this.addChild(sprite);
component.syncNodeToPhysics();
var frameInA = [-4.37114e-08, 1, 0, 0, -1, -4.37114e-08, 0, 0, 0, 0, 1, 0, 0, -5, 0, 1];
var frameInB = [-4.37114e-08, 1, 0, 0, -1, -4.37114e-08, 0, 0, 0, 0, 1, 0, 0, 5, 0, 1];
var frameInA = [-4.37114e-8, 1, 0, 0, -1, -4.37114e-8, 0, 0, 0, 0, 1, 0, 0, -5, 0, 1];
var frameInB = [-4.37114e-8, 1, 0, 0, -1, -4.37114e-8, 0, 0, 0, 0, 1, 0, 0, 5, 0, 1];
constraint = cc.Physics3DSliderConstraint.create(rigidBody, rigidBodyB, frameInA, frameInB, false);
physicsScene.getPhysics3DWorld().addPhysics3DConstraint(constraint);
this._world.addPhysics3DConstraint(constraint);
constraint.setLowerLinLimit(-5);
constraint.setUpperLinLimit(5);
@ -382,9 +390,9 @@ var Physics3DConstraintDemo = Physics3DTestDemo.extend({
this.addChild(sprite);
component.syncNodeToPhysics();
frameInA = [-4.37114e-08, 1, 0, 0, -1, -4.37114e-08, 0, 0, 0, 0, 1, 0, 0, -10, 0, 1];
frameInA = [-4.37114e-8, 1, 0, 0, -1, -4.37114e-8, 0, 0, 0, 0, 1, 0, 0, -10, 0, 1];
constraint = cc.Physics3DConeTwistConstraint.create(rigidBody, frameInA);
physicsScene.getPhysics3DWorld().addPhysics3DConstraint(constraint, true);
this._world.addPhysics3DConstraint(constraint, true);
constraint.setLimit(cc.degreesToRadians(10), cc.degreesToRadians(10), cc.degreesToRadians(40));
//create 6 dof constraint
@ -403,11 +411,68 @@ var Physics3DConstraintDemo = Physics3DTestDemo.extend({
frameInA = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
constraint = cc.Physics3D6DofConstraint.create(rigidBody, frameInA, false);
physicsScene.getPhysics3DWorld().addPhysics3DConstraint(constraint);
this._world.addPhysics3DConstraint(constraint);
constraint.setAngularLowerLimit(cc.math.vec3(0, 0, 0));
constraint.setAngularUpperLimit(cc.math.vec3(0, 0, 0));
constraint.setLinearLowerLimit(cc.math.vec3(-10, 0, 0));
constraint.setLinearUpperLimit(cc.math.vec3(10, 0, 0));
},
onTouchesBegan:function(touches, event){
if(this._camera){
cc.log("STARTED " + touches.length);
var touch = touches[0];
var location = touch.getLocationInView();
var nearP = cc.math.vec3(location.x, location.y, 0);
var farP = cc.math.vec3(location.x, location.y, 1);
var size = director.getWinSize();
nearP = this._camera.unproject(size, nearP);
farP = this._camera.unproject(size, farP);
var result = this._world.rayCast(nearP, farP);
if(result !== false && result.hitObj.getObjType() == cc.Physics3DObject.PhysicsObjType.RIGID_BODY)
{
var mat = cc.math.mat4GetInversed(result.hitObj.getWorldTransform());
var position = cc.math.mat4TransformPoint(mat, result.hitPosition);
this._constraint = cc.Physics3DPointToPointConstraint.create(result.hitObj, position);
this._world.addPhysics3DConstraint(this._constraint, true);
this._pickingDistance = cc.math.vec3Length(cc.math.vec3Sub(result.hitPosition, nearP));
return;
}
}
Physics3DTestDemo.prototype.onTouchesBegan.call(this, touches, event);
this._needShootBox = false;
},
onTouchesMoved:function(touches, event){
if(this._constraint){
var touch = touches[0];
var location = touch.getLocationInView();
var nearP = cc.math.vec3(location.x, location.y, 0);
var farP = cc.math.vec3(location.x, location.y, 1);
var size = director.getWinSize();
nearP = this._camera.unproject(size, nearP);
farP = this._camera.unproject(size, farP);
var dir = cc.math.vec3Normalize(cc.math.vec3Sub(farP, nearP));
this._constraint.setPivotPointInB(cc.math.vec3Add(nearP, cc.math.vec3(dir.x * this._pickingDistance, dir.y * this._pickingDistance, dir.z * this._pickingDistance)));
return;
}
Physics3DTestDemo.prototype.onTouchesMoved.call(this, touches, event);
},
onTouchesEnded:function(touches, event){
if(this._constraint){
this._world.removePhysics3DConstraint(this._constraint);
this._constraint = null;
return;
}
Physics3DTestDemo.prototype.onTouchesEnded.call(this, touches, event);
}
});

@ -1 +1 @@
Subproject commit 23e98f0b9942a2ef305f00c8e8771b1029fbbd00
Subproject commit 301f566839fa6505d225dfc376bc3fab4673204f

View File

@ -42,7 +42,7 @@ classes = Physics3DWorld Physics3DShape PhysicsSprite3D Physics3DObject Physics3
skip = Physics3DShape::[createCompoundShape createMesh createHeightfield],
Physics3DObject::[setCollisionCallback],
Physics3DWorld::[getPhysicsObjects],
Physics3DWorld::[getPhysicsObjects rayCast],
Physics3DConeTwistConstraint::[setMotorTarget setMotorTargetInConstraintSpace],
PhysicsSprite3D::[create],
Physics3DRigidBody::[create]