Browse Source

Merge pull request #16 from Dokthar/master

fix #1 : Rotating models in SceneComposer in SDK not done correctly
Thank you :)
MeFisto94 9 years ago
parent
commit
e3a86336ac

+ 3 - 6
jme3-scenecomposer/src/com/jme3/gde/scenecomposer/SceneEditTool.java

@@ -89,10 +89,7 @@ public abstract class SceneEditTool {
         this.onTopToolNode.attachChild(axisMarker);
         setDefaultAxisMarkerColors();
 
-
-        if (toolController.getSelectionShape() != null) {
-            axisMarker.setLocalTranslation(toolController.getSelectedSpatial().getWorldTranslation());
-        }
+        doUpdateToolsTransformation();
 
     }
 
@@ -134,7 +131,7 @@ public abstract class SceneEditTool {
             axisMarker.setLocalTranslation(toolController.getSelectedSpatial().getWorldTranslation());
             switch (transformType) {
                 case local:
-                    axisMarker.setLocalRotation(toolController.getSelectedSpatial().getLocalRotation());
+                    axisMarker.setLocalRotation(toolController.getSelectedSpatial().getWorldRotation());
                     break;
                 case global:
                     axisMarker.setLocalRotation(Quaternion.IDENTITY);
@@ -512,4 +509,4 @@ public abstract class SceneEditTool {
     public void setTransformType(SceneComposerToolController.TransformationType transformType) {
         this.transformType = transformType;
     }
-}
+}

+ 8 - 2
jme3-scenecomposer/src/com/jme3/gde/scenecomposer/tools/MoveTool.java

@@ -37,7 +37,7 @@ public class MoveTool extends SceneEditTool {
     private PickManager pickManager;
 
     public MoveTool() {
-        axisPickType = AxisMarkerPickType.axisAndPlane;
+        axisPickType = SceneEditTool.AxisMarkerPickType.axisAndPlane;
         setOverrideCameraControl(true);
 
     }
@@ -133,7 +133,13 @@ public class MoveTool extends SceneEditTool {
             } else if (pickedMarker.equals(ARROW_X) || pickedMarker.equals(ARROW_Y) || pickedMarker.equals(ARROW_Z)) {
                 diff = pickManager.getTranslation(constraintAxis);
             }
-            Vector3f position = startPosition.add(diff);
+            Vector3f position;
+            Spatial parent = toolController.getSelectedSpatial().getParent();
+            if (parent != null) {
+                position = startPosition.add(parent.getWorldRotation().inverse().mult(diff));
+            } else {
+                position = startPosition.add(diff);
+            }
             lastPosition = position;
             toolController.getSelectedSpatial().setLocalTranslation(position);
             updateToolsTransformation();

+ 23 - 13
jme3-scenecomposer/src/com/jme3/gde/scenecomposer/tools/PickManager.java

@@ -66,22 +66,30 @@ public class PickManager {
     public void setTransformation(Quaternion planeRotation, SceneComposerToolController.TransformationType type, Camera camera) {
         Quaternion rot = new Quaternion();
         transformationType = type;
-        if (transformationType == SceneComposerToolController.TransformationType.local) {
-            rot.set(spatial.getWorldRotation());
-            rot.multLocal(planeRotation);
-            origineRotation = spatial.getWorldRotation().clone();
-        } else if (transformationType == SceneComposerToolController.TransformationType.global) {
-            rot.set(planeRotation);
-            origineRotation = new Quaternion(Quaternion.IDENTITY);
-        } else if (transformationType == SceneComposerToolController.TransformationType.camera) {
-            rot.set(camera.getRotation());  
-            origineRotation = camera.getRotation();
+        if (null != transformationType) {
+            switch (transformationType) {
+                case local:
+                    rot.set(spatial.getWorldRotation());
+                    rot.multLocal(planeRotation);
+                    origineRotation = spatial.getWorldRotation().clone();
+                    break;
+                case global:
+                    rot.set(planeRotation);
+                    origineRotation = new Quaternion(Quaternion.IDENTITY);
+                    break;
+                case camera:
+                    rot.set(camera.getRotation());
+                    origineRotation = camera.getRotation();
+                    break;
+                default:
+                    break;
+            }
         }
         plane.setLocalRotation(rot);
     }
     
     /**
-     * 
+     *
      * @param camera
      * @param screenCoord
      * @return true if the the new picked location is set, else return false.
@@ -146,7 +154,9 @@ public class PickManager {
 
     /**
      * Get the Rotation into a specific custom space.
-     * @param transforme the rotation to the custom space (World to Custom space)
+     *
+     * @param transforme the rotation to the custom space (World to Custom
+     * space)
      * @return the Rotation in the custom space
      */
     public Quaternion getRotation(Quaternion transforme) {
@@ -185,5 +195,5 @@ public class PickManager {
     public Vector3f getLocalTranslation(Vector3f axisConstrainte) {
         return getTranslation(origineRotation.inverse().mult(axisConstrainte));
     }
-    
+
 }

+ 4 - 2
jme3-scenecomposer/src/com/jme3/gde/scenecomposer/tools/RotateTool.java

@@ -26,12 +26,13 @@ public class RotateTool extends SceneEditTool {
 
     private Vector3f pickedMarker;
     private Quaternion startRotate;
+    private Quaternion startWorldRotate;
     private Quaternion lastRotate;
     private boolean wasDragging = false;
     private PickManager pickManager;
 
     public RotateTool() {
-        axisPickType = AxisMarkerPickType.planeOnly;
+        axisPickType = SceneEditTool.AxisMarkerPickType.planeOnly;
         setOverrideCameraControl(true);
     }
 
@@ -71,6 +72,7 @@ public class RotateTool extends SceneEditTool {
                     pickManager.initiatePick(toolController.getSelectedSpatial(), PickManager.PLANE_YZ, getTransformType(), camera, screenCoord);
                 }
                 startRotate = toolController.getSelectedSpatial().getLocalRotation().clone();
+                startWorldRotate = toolController.getSelectedSpatial().getWorldRotation().clone();
                 wasDragging = true;
             }
         }
@@ -110,7 +112,7 @@ public class RotateTool extends SceneEditTool {
             }
 
             if (pickedMarker.equals(QUAD_XY) || pickedMarker.equals(QUAD_XZ) || pickedMarker.equals(QUAD_YZ)) {
-                Quaternion rotation = startRotate.mult(pickManager.getRotation(startRotate.inverse()));
+                Quaternion rotation = startRotate.mult(pickManager.getRotation(startWorldRotate.inverse()));
                 toolController.getSelectedSpatial().setLocalRotation(rotation);
                 lastRotate = rotation;
             }

+ 5 - 5
jme3-scenecomposer/src/com/jme3/gde/scenecomposer/tools/ScaleTool.java

@@ -20,7 +20,7 @@ import org.openide.util.Lookup;
 
 /**
  *
- * @author sploreg
+ * @author sploreg, dokthar
  */
 public class ScaleTool extends SceneEditTool {
 
@@ -117,15 +117,15 @@ public class ScaleTool extends SceneEditTool {
             }
             if (pickedMarker.equals(QUAD_XY) || pickedMarker.equals(QUAD_XZ) || pickedMarker.equals(QUAD_YZ)) {
                 constraintAxis = pickManager.getStartOffset().normalize();
-                float diff = pickManager.getTranslation(constraintAxis).dot(constraintAxis);
-                diff *= 0.5f;
-                Vector3f scale = startScale.add(new Vector3f(diff, diff, diff));
+                float diff = pickManager.getLocalTranslation(constraintAxis).dot(constraintAxis);
+                diff += 1f;
+                Vector3f scale = startScale.mult(diff);
                 lastScale = scale;
                 toolController.getSelectedSpatial().setLocalScale(scale);
             } else if (pickedMarker.equals(ARROW_X) || pickedMarker.equals(ARROW_Y) || pickedMarker.equals(ARROW_Z)) {
                 // Get the translation in the spatial Space
                 Quaternion worldToSpatial = toolController.getSelectedSpatial().getWorldRotation().inverse();
-                Vector3f diff = pickManager.getTranslation(worldToSpatial.mult(constraintAxis));
+                Vector3f diff = worldToSpatial.mult(pickManager.getTranslation(constraintAxis));
                 diff.multLocal(0.5f);
                 Vector3f scale = startScale.add(diff);
                 lastScale = scale;

+ 7 - 1
jme3-scenecomposer/src/com/jme3/gde/scenecomposer/tools/shortcuts/MoveShortcut.java

@@ -159,7 +159,13 @@ public class MoveShortcut extends ShortcutTool {
             } else {
                 diff = pickManager.getTranslation(currentAxis);
             }
-            Vector3f position = startPosition.add(diff);
+            Vector3f position;
+            Spatial parent = toolController.getSelectedSpatial().getParent();
+            if (parent != null) {
+                position = startPosition.add(parent.getWorldRotation().inverse().mult(diff));
+            } else {
+                position = startPosition.add(diff);
+            }
             finalPosition = position;
             toolController.getSelectedSpatial().setLocalTranslation(position);
             updateToolsTransformation();

+ 4 - 2
jme3-scenecomposer/src/com/jme3/gde/scenecomposer/tools/shortcuts/RotateShortcut.java

@@ -34,6 +34,7 @@ public class RotateShortcut extends ShortcutTool {
     private boolean pickEnabled;
     private Quaternion startRotation;
     private Quaternion finalRotation;
+    private Quaternion startWorldRotate;
 
     @Override
 
@@ -55,6 +56,7 @@ public class RotateShortcut extends ShortcutTool {
     private void init(Spatial selectedSpatial) {
         spatial = selectedSpatial;
         startRotation = spatial.getLocalRotation().clone();
+        startWorldRotate = spatial.getWorldRotation().clone();
         currentAxis = Vector3f.UNIT_XYZ;
         pickManager = Lookup.getDefault().lookup(PickManager.class);
         pickEnabled = false;
@@ -143,8 +145,8 @@ public class RotateShortcut extends ShortcutTool {
         }
 
         if (pickManager.updatePick(camera, screenCoord)) {
-
-            Quaternion rotation = startRotation.mult(pickManager.getRotation(startRotation.inverse()));
+            
+            Quaternion rotation = startRotation.mult(pickManager.getRotation(startWorldRotate.inverse()));           
             toolController.getSelectedSpatial().setLocalRotation(rotation);
             finalRotation = rotation;
             updateToolsTransformation();

+ 3 - 3
jme3-scenecomposer/src/com/jme3/gde/scenecomposer/tools/shortcuts/ScaleShortcut.java

@@ -145,9 +145,9 @@ public class ScaleShortcut extends ShortcutTool {
             Vector3f scale = startScale;
             if (currentAxis.equals(Vector3f.UNIT_XYZ)) {
                 Vector3f constraintAxis = pickManager.getStartOffset().normalize();
-                float diff = pickManager.getTranslation(constraintAxis).dot(constraintAxis);
-                diff *= 0.5f;
-                scale = startScale.add(new Vector3f(diff, diff, diff));
+                float diff = pickManager.getLocalTranslation(constraintAxis).dot(constraintAxis);
+                diff += 1f;
+                scale = startScale.mult(diff);
             } else {
                 // Get the translation in the spatial Space
                 Quaternion worldToSpatial = toolController.getSelectedSpatial().getWorldRotation().inverse();