JavaFX 3D Rotation around Scene Fixed Axes -
creating virtual trackball
using javafx want create virtual trackball device x , y mouse drag events rotate virtual trackball in intuitive way.
intuitive (at least me) means, scene axes being:
- x increasing left right
- y increasing top bottom
- z increasing perpendicular screen towards viewer
i want vertical mouse drag events cause trackball roll around scene x axis, , mouse horizontal drag events cause trackball rotate around scene y axis.
starting oracle javafx smampleapp 3d, have modified things scene comprises fixed axis x:red, y:green, z:blue, camera perspectivecamera trained on axis origin, , trackball (which, cube can watch how behaves when rotated).
- mouse dragged movement in x direction, rotates trackball around trackball's y-axis
- mouse dragged movement in y direction, rotates trackball around trackball's x-axis
first rotate trackball 45 degress around y axis (by dragging mouse horizontally). if drag mouse vertically, trackball rotates it's x axis. however, trackball's x axis has been rotated through 45 degrees previous rotation, , not behaviour want, rotate trackball around fixed x axis (i.e. fixed red axis appears in scene)
this code based on original code from: https://docs.oracle.com/javase/8/javafx/graphics-tutorial/sampleapp3d.htm
the code xform @ https://docs.oracle.com/javase/8/javafx/graphics-tutorial/sampleapp3d-code.htm#cjaggifg
how need change code achieve aims?
package moleculesampleapp; import javafx.application.application; import javafx.scene.*; import javafx.scene.paint.color; import javafx.stage.stage; import javafx.scene.paint.phongmaterial; import javafx.scene.shape.box; import javafx.scene.shape.shape3d; public class moleculesampleapp1 extends application { group root = new group(); xform axisxform = new xform(); xform boxxform = new xform(); xform worldxform = new xform(); xform cameraxform = new xform(); phongmaterial redmaterial,greenmaterial,bluematerial; perspectivecamera camera = new perspectivecamera(true); private static double camera_initial_distance = -450; private static double camera_initial_x_angle = -10.0; private static double camera_initial_y_angle = 0.0; private static double camera_near_clip = 0.1; private static double camera_far_clip = 10000.0; private static double axis_length = 250.0; private static double mouse_speed = 0.1; private static double rotation_speed = 2.0; double mouseposx, mouseposy; double mouseoldx, mouseoldy; double mousedeltax, mousedeltay; private void handlemouse(scene scene) { scene.setonmousepressed(me -> { mouseposx = me.getscenex(); mouseposy = me.getsceney(); mouseoldx = me.getscenex(); mouseoldy = me.getsceney(); }); scene.setonmousedragged(me -> { mouseoldx = mouseposx; mouseoldy = mouseposy; mouseposx = me.getscenex(); mouseposy = me.getsceney(); mousedeltax = (mouseposx - mouseoldx); mousedeltay = (mouseposy - mouseoldy); if (me.isprimarybuttondown()) { boxxform.ry.setangle(boxxform.ry.getangle() - mousedeltax * mouse_speed * rotation_speed); // left right boxxform.rx.setangle(boxxform.rx.getangle() + mousedeltay * mouse_speed * rotation_speed); // down } }); } private void handlekeyboard(scene scene) { scene.setonkeypressed(event -> { switch (event.getcode()) { case z: camera.settranslatez(camera_initial_distance); cameraxform.ry.setangle(camera_initial_y_angle); cameraxform.rx.setangle(camera_initial_x_angle); boxxform.reset(); break; } }); } phongmaterial creatematerial(color diffusecolor, color specularcolor) { phongmaterial material = new phongmaterial(diffusecolor); material.setspecularcolor(specularcolor); return material; } @override public void start(stage primarystage) { root.getchildren().add(worldxform); root.setdepthtest(depthtest.enable); // create materials redmaterial = creatematerial(color.darkred,color.red); greenmaterial = creatematerial(color.darkgreen,color.green); bluematerial = creatematerial(color.darkblue,color.blue); // build camera root.getchildren().add(camera); cameraxform.getchildren().add(camera); camera.setnearclip(camera_near_clip); camera.setfarclip(camera_far_clip); camera.settranslatez(camera_initial_distance); cameraxform.ry.setangle(camera_initial_y_angle); cameraxform.rx.setangle(camera_initial_x_angle); // build axes box xaxis = new box(axis_length, 1, 1); box yaxis = new box(1, axis_length, 1); box zaxis = new box(1, 1, axis_length); xaxis.setmaterial(redmaterial); yaxis.setmaterial(greenmaterial); zaxis.setmaterial(bluematerial); axisxform.getchildren().addall(xaxis, yaxis, zaxis); worldxform.getchildren().addall(axisxform); // build shiney red box shape3d box = new box(80, 80, 80); box.setmaterial(redmaterial); boxxform.getchildren().add(box); worldxform.getchildren().addall(boxxform); scene scene = new scene(root, 1024, 768, true); scene.setfill(color.grey); handlekeyboard(scene); handlemouse(scene); primarystage.settitle("molecule sample application"); primarystage.setscene(scene); primarystage.show(); scene.setcamera(camera); } public static void main(string[] args) { launch(args); } }
if understand question correctly thing have replace line.
xform cameraxform = new xform(rotateorder.zyx);
this changes rotation order of single rotations , should give need.
Comments
Post a Comment