saharan / OimoPhysics

A cross-platform 3D physics engine
MIT License
863 stars 68 forks source link

How can I get the RigidBody rotation(XYZ) #1

Closed xieshengfly closed 7 years ago

xieshengfly commented 11 years ago

Hello! OimoPhysics is wonderful and fast. I have a problem that I dont know how to get the RigidBody rotation(XYZ). The rotation is A Mat33. Thanks.

saharan commented 11 years ago

Hello, thank you for using OimoPhysics! So far, there are no methods to get euler angles from Mat33, but you may be able to get euler angles by using Matrix3D.decompose() after copying matrix elements to Matrix3D from Mat33.

xieshengfly commented 11 years ago

Hello saharan.I use OimoPhysics in Alternativa3D.It goes well except a little bug!The rotation is not nicety sometime.

bug

Mat44.as private var rowData:Vector.=new Vector.(16); public function getRowData():Vector.{ rowData[0]=e00; rowData[1]=e01; rowData[2]=e02; rowData[3]=e03; rowData[4]=e10; rowData[5]=e11; rowData[6]=e12; rowData[7]=e13; rowData[8]=e20; rowData[9]=e21; rowData[10]=e22; rowData[11]=e23; rowData[12]=e30; rowData[13]=e31; rowData[14]=e32; rowData[15]=e33; return rowData; }


Alternativa3D.as public function update():void { controller.update(); world.step(); i=world.rigidBodies.length; while(i--){ tempRigidBody=world.rigidBodies[i]; if(tempRigidBody&&tempRigidBody.object3D){ m44.copyMat33(tempRigidBody.rotation); m44.e30 = tempRigidBody.position.x; m44.e31 = tempRigidBody.position.y; m44.e32 = tempRigidBody.position.z; matrix3D.copyRawDataFrom(m44.getRowData()); tempRigidBody.object3D.matrix2=matrix3D;
} } camera3D.render(stage3D);

}

Object3D.as public function set matrix2(value:Matrix3D):void { var v:Vector. = value.decompose(); var t:Vector3D = v[0]; var r:Vector3D = v[1]; var s:Vector3D = v[2]; _x = t.x; _y = t.z; _z = t.y; _rotationX = r.x; _rotationY = r.z; _rotationZ = r.y; // _scaleX = s.x; // _scaleY = s.z; // _scaleZ = s.y; transformChanged = true;

}

saharan commented 11 years ago

hmm... I think that Matrix3D is transposed, because there are elements of translation in e30 - 32. So I suggest you to try the following code:

public function getRowData():Vector.<Number> {
    rowData[0] = e00;
    rowData[1] = e10;
    rowData[2] = e20;
    rowData[3] = e30;
    rowData[4] = e01;
    rowData[5] = e11;
    rowData[6] = e21;
    rowData[7] = e31;
    rowData[8] = e02;
    rowData[9] = e12;
    rowData[10] = e22;
    rowData[11] = e32;
    rowData[12] = e03;
    rowData[13] = e13;
    rowData[14] = e23;
    rowData[15] = e33;
    return rowData;
}

...and change

m44.e30 = tempRigidBody.position.x;
m44.e31 = tempRigidBody.position.y;
m44.e32 = tempRigidBody.position.z;

to

m44.e03 = tempRigidBody.position.x;
m44.e13 = tempRigidBody.position.y;
m44.e23 = tempRigidBody.position.z;

good luck :)