android - min3D library - quaternion rotation issue -
i rendered 3d model using min3d library in application.
used quaternion data (x,y,z,w) rotation , flipping.
the rotation perfect front face , when flip opposite face rotation becomes opposite actual direction.
mean "on opposite face", when rotate right, cube rotates left , vice-versa.
the code used:
@override public void initscene() { light light = new light(); light.position.setallfrom(scene.camera().position); scene.lights().add(light); scene.backgroundcolor().setall(0xffffff); try { iparser parser = parser.createparser(parser.type.obj, getresources(), "---:raw/cube_obj", true); parser.parse(); cube = parser.getparsedobject(); cube.scale().x = cube.scale().y = cube.scale().z = .08f; cube.position().x = .3f; scene.addchild(cube); scene.camera().target = cube.position(); mdoplotting =true; } catch (outofmemoryerror e) { e.printstacktrace(); }
in object3d.java
private quaternion _quaternion = new quaternion(new vector3(0.0,0.0,0.0),math.toradians(0)); public quaternion quaternion() { return _quaternion; }
in quaternion.java
public final class quaternion { public double x; public double y; public double z; public double w; public quaternion(final quaternion q) { this(q.x, q.y, q.z, q.w); } public quaternion(double x, double y, double z, double w) { this.x = x; this.y = y; this.z = z; this.w = w; } public void set(final quaternion q) { this.x = q.x; this.y = q.y; this.z = q.z; this.w = q.w; } public quaternion(vector3 axis, double angle) { set(axis, angle); } public double norm() { return math.sqrt(dot(this)); } public double getw() { return w; } public double getx() { return x; } public double gety() { return y; } public double getz() { return z; } public quaternion set(vector3 axis, double angle) { double s = math.sin(angle / 2); w = math.cos(angle / 2); x = axis.getx() * s; y = axis.gety() * s; z = axis.getz() * s; return this; } public quaternion multhis(quaternion q) { double nw = w * q.w - x * q.x - y * q.y - z * q.z; double nx = w * q.x + x * q.w + y * q.z - z * q.y; double ny = w * q.y + y * q.w + z * q.x - x * q.z; z = w * q.z + z * q.w + x * q.y - y * q.x; w = nw; x = nx; y = ny; return this; } public quaternion setrotation( vector3 v, float angle){ double s = (double) math.sin(angle / 2); w = (double) math.cos(angle / 2); x = v.x*s; y = v.y*s; z = v.z*s; return this; } public void setrotation(double x, double y, double z, double angle){ // float half = angle*0.5f; double s = (double) math.sin(angle / 2); this.x = x*s; this.y = y*s; this.z = z*s; w = (double) math.cos(angle / 2); } public void rotatethis(double x,double y,double z,double w){ this.x = x; this.y = y; this.z = z; this.w = w; } public quaternion scalethis(double scale) { if (scale != 1) { w *= scale; x *= scale; y *= scale; z *= scale; } return this; } public quaternion divthis(double scale) { if (scale != 1) { w /= scale; x /= scale; y /= scale; z /= scale; } return this; } public double dot(quaternion q) { return x * q.x + y * q.y + z * q.z + w * q.w; } public boolean equals(quaternion q) { return x == q.x && y == q.y && z == q.z && w == q.w; } public quaternion interpolatethis(quaternion q, double t) { if (!equals(q)) { double d = dot(q); double qx, qy, qz, qw; if (d < 0f) { qx = -q.x; qy = -q.y; qz = -q.z; qw = -q.w; d = -d; } else { qx = q.x; qy = q.y; qz = q.z; qw = q.w; } double f0, f1; if ((1 - d) > 0.1f) { double angle = (double) math.acos(d); double s = (double) math.sin(angle); double tangle = t * angle; f0 = (double) math.sin(angle - tangle) / s; f1 = (double) math.sin(tangle) / s; } else { f0 = 1 - t; f1 = t; } x = f0 * x + f1 * qx; y = f0 * y + f1 * qy; z = f0 * z + f1 * qz; w = f0 * w + f1 * qw; } return this; } public quaternion normalizethis() { return divthis(norm()); } public quaternion interpolate(quaternion q, double t) { return new quaternion(this).interpolatethis(q, t); } public float[] tomatrix() { float[] matrixs = new float[16]; tomatrix(matrixs); return matrixs; } public final void tomatrix(float[] matrixs) { matrixs[3] = 0.0f; matrixs[7] = 0.0f; matrixs[11] = 0.0f; matrixs[12] = 0.0f; matrixs[13] = 0.0f; matrixs[14] = 0.0f; matrixs[15] = 1.0f; matrixs[0] = (float) (1.0f - (2.0f * ((y * y) + (z * z)))); matrixs[1] = (float) (2.0f * ((x * y) - (z * w))); matrixs[2] = (float) (2.0f * ((x * z) + (y * w))); matrixs[4] = (float) (2.0f * ((x * y) + (z * w))); matrixs[5] = (float) (1.0f - (2.0f * ((x * x) + (z * z)))); matrixs[6] = (float) (2.0f * ((y * z) - (x * w))); matrixs[8] = (float) (2.0f * ((x * z) - (y * w))); matrixs[9] = (float) (2.0f * ((y * z) + (x * w))); matrixs[10] = (float) (1.0f - (2.0f * ((x * x) + (y * y)))); }
}
i used
cube.quaternion().rotatethis( event3d.getx(),event3d.gety(),event3d.getz(),event3d.getw());
for rotation
i not sure because of touch event (if using ontouch user rotating cube), x axis of screen take left of screen right positive. , 3d model/camera rotating positively in anti-clockwise. so, maybe reason why it's in reverse direction. confirm may code how track down user action rotating cube. sorry can't use comment.
Comments
Post a Comment