Skip to content

Commit 7d3cca4

Browse files
committed
implemented global/local/view transformations.
1 parent 7679ebd commit 7d3cca4

7 files changed

Lines changed: 187 additions & 142 deletions

File tree

src/main/java/com/ss/editor/control/transform/AbstractTransformControl.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.jme3.collision.CollisionResult;
55
import com.jme3.math.Quaternion;
66
import com.jme3.math.Transform;
7-
import com.jme3.math.Vector3f;
87
import com.jme3.scene.Geometry;
98
import com.jme3.scene.Node;
109
import com.jme3.scene.control.AbstractControl;
@@ -136,15 +135,6 @@ protected void detectPickedAxis(@NotNull final EditorTransformSupport editorCont
136135
}
137136
}
138137

139-
@NotNull
140-
protected Vector3f getPickedVector(@NotNull final Transform transform, @NotNull final PickedAxis pickedAxis) {
141-
if (pickedAxis == PickedAxis.Y) {
142-
return Vector3f.UNIT_Y;
143-
} else if (pickedAxis == PickedAxis.Z) {
144-
return Vector3f.UNIT_Z;
145-
} else return Vector3f.UNIT_X;
146-
}
147-
148138
@NotNull
149139
protected String getNodeX() {
150140
throw new RuntimeException();

src/main/java/com/ss/editor/control/transform/EditorTransformSupport.java

Lines changed: 101 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.ss.editor.control.transform;
22

3+
import static com.ss.editor.util.GeomUtils.*;
34
import com.jme3.math.Quaternion;
45
import com.jme3.math.Transform;
56
import com.jme3.math.Vector3f;
67
import com.jme3.renderer.Camera;
78
import com.jme3.scene.Node;
89
import com.jme3.scene.Spatial;
9-
import com.ss.editor.util.GeomUtils;
1010
import com.ss.editor.util.LocalObjects;
1111
import org.jetbrains.annotations.NotNull;
1212
import org.jetbrains.annotations.Nullable;
@@ -98,6 +98,20 @@ public void prepareToRotate(@NotNull final Node parent, @NotNull final Node chil
9898
child.setLocalRotation(transform.getRotation());
9999
}
100100

101+
@NotNull
102+
@Override
103+
protected Vector3f getScaleAxis(@NotNull final Transform transform, @NotNull final PickedAxis pickedAxis,
104+
@NotNull final Camera camera) {
105+
106+
final LocalObjects local = LocalObjects.get();
107+
108+
if (pickedAxis == PickedAxis.Y) {
109+
return getUp(transform.getRotation(), local.nextVector());
110+
} else if (pickedAxis == PickedAxis.Z) {
111+
return getDirection(transform.getRotation(), local.nextVector());
112+
} else return getLeft(transform.getRotation(), local.nextVector());
113+
}
114+
101115
@Override
102116
public void prepareToMove(@NotNull final Node parent, @NotNull final Node child,
103117
@NotNull final Transform transform, @NotNull final Camera camera) {
@@ -125,13 +139,18 @@ public void prepareToRotate(@NotNull final Node parent, @NotNull final Node chil
125139
child.setLocalRotation(transform.getRotation());
126140
}
127141

142+
@NotNull
128143
@Override
129-
public void prepareToMove(@NotNull final Node parent, @NotNull final Node child,
130-
@NotNull final Transform transform, @NotNull final Camera camera) {
131-
parent.setLocalRotation(camera.getRotation());
132-
parent.setLocalTranslation(transform.getTranslation());
133-
child.setLocalTranslation(Vector3f.ZERO);
134-
child.setLocalRotation(Quaternion.IDENTITY);
144+
protected Vector3f getScaleAxis(@NotNull final Transform transform, @NotNull final PickedAxis pickedAxis,
145+
@NotNull final Camera camera) {
146+
147+
final LocalObjects local = LocalObjects.get();
148+
149+
if (pickedAxis == PickedAxis.Y) {
150+
return getUp(camera.getRotation(), local.nextVector());
151+
} else if (pickedAxis == PickedAxis.Z) {
152+
return getDirection(camera.getRotation(), local.nextVector());
153+
} else return getLeft(camera.getRotation(), local.nextVector());
135154
}
136155

137156
@NotNull
@@ -142,12 +161,19 @@ protected Vector3f getPickedVector(@NotNull final Transform transform, @NotNull
142161
final LocalObjects local = LocalObjects.get();
143162

144163
if (pickedAxis == PickedAxis.Y) {
145-
return GeomUtils.getUp(camera.getRotation(), local.nextVector());
164+
return getUp(camera.getRotation(), local.nextVector());
146165
} else if (pickedAxis == PickedAxis.Z) {
147-
return GeomUtils.getDirection(camera.getRotation(), local.nextVector());
148-
} else {
149-
return GeomUtils.getLeft(camera.getRotation(), local.nextVector());
150-
}
166+
return getDirection(camera.getRotation(), local.nextVector());
167+
} else return getLeft(camera.getRotation(), local.nextVector());
168+
}
169+
170+
@Override
171+
public void prepareToMove(@NotNull final Node parent, @NotNull final Node child,
172+
@NotNull final Transform transform, @NotNull final Camera camera) {
173+
parent.setLocalRotation(camera.getRotation());
174+
parent.setLocalTranslation(transform.getTranslation());
175+
child.setLocalTranslation(Vector3f.ZERO);
176+
child.setLocalRotation(Quaternion.IDENTITY);
151177
}
152178

153179
@NotNull
@@ -178,7 +204,7 @@ public Quaternion getToolRotation(@NotNull final Transform transform, @NotNull f
178204
}
179205

180206
/**
181-
* Apply rotations to nodes to calculate transformations.
207+
* Prepare nodes to rotate.
182208
*
183209
* @param parent the parent node.
184210
* @param child the child node.
@@ -192,7 +218,21 @@ public void prepareToRotate(@NotNull final Node parent, @NotNull final Node chil
192218
}
193219

194220
/**
195-
* Apply positions to nodes to calculate transformations.
221+
* Prepare nodes to scale.
222+
*
223+
* @param parent the parent node.
224+
* @param child the child node.
225+
* @param transform the base transform.
226+
* @param camera the camera.
227+
*/
228+
public void prepareToScale(@NotNull final Node parent, @NotNull final Node child,
229+
@NotNull final Transform transform, @NotNull final Camera camera) {
230+
parent.setLocalScale(transform.getScale());
231+
child.setLocalScale(Vector3f.UNIT_XYZ);
232+
}
233+
234+
/**
235+
* Prepare nodes to move.
196236
*
197237
* @param parent the parent node.
198238
* @param child the child node.
@@ -227,15 +267,22 @@ protected Vector3f getPickedVector(@NotNull final Transform transform, @NotNull
227267
}
228268

229269
/**
230-
* Gets the rotation to calculate scale transformations.
270+
* Get a vector to calculate scaling by the axis.
231271
*
232-
* @param spatial the spatial.
233-
* @param camera the camera.
234-
* @return the target rotation.
272+
* @param transform the base transform.
273+
* @param pickedAxis the picked Axis.
274+
* @param camera the camera.
275+
* @return the axis vector.
235276
*/
236277
@NotNull
237-
public Quaternion getScaleRotation(@NotNull final Spatial spatial, @NotNull final Camera camera) {
238-
return spatial.getWorldRotation();
278+
protected Vector3f getScaleAxis(@NotNull final Transform transform, @NotNull final PickedAxis pickedAxis,
279+
@NotNull final Camera camera) {
280+
281+
if (pickedAxis == PickedAxis.Y) {
282+
return Vector3f.UNIT_Y;
283+
} else if (pickedAxis == PickedAxis.Z) {
284+
return Vector3f.UNIT_Z;
285+
} else return Vector3f.UNIT_X;
239286
}
240287
}
241288

@@ -278,19 +325,46 @@ public Quaternion getScaleRotation(@NotNull final Spatial spatial, @NotNull fina
278325
Node getCollisionPlane();
279326

280327
/**
281-
* Sets delta vector.
328+
* Set delta of transformation.
282329
*
283-
* @param deltaVector the delta vector.
330+
* @param transformDeltaX the x delta.
284331
*/
285-
void setDeltaVector(@Nullable final Vector3f deltaVector);
332+
void setTransformDeltaX(float transformDeltaX);
286333

287334
/**
288-
* Gets delta vector.
335+
* Set delta of transformation.
289336
*
290-
* @return the delta vector.
337+
* @param transformDeltaY the y delta.
291338
*/
292-
@Nullable
293-
Vector3f getDeltaVector();
339+
void setTransformDeltaY(float transformDeltaY);
340+
341+
/**
342+
* Set delta of transformation.
343+
*
344+
* @param transformDeltaZ the z delta.
345+
*/
346+
void setTransformDeltaZ(float transformDeltaZ);
347+
348+
/**
349+
* Get delta of transformation.
350+
*
351+
* @return the delta x.
352+
*/
353+
float getTransformDeltaX();
354+
355+
/**
356+
* Get delta of transformation.
357+
*
358+
* @return the delta y.
359+
*/
360+
float getTransformDeltaY();
361+
362+
/**
363+
* Get delta of transformation.
364+
*
365+
* @return the delta z.
366+
*/
367+
float getTransformDeltaZ();
294368

295369
/**
296370
* Gets to transform.

src/main/java/com/ss/editor/control/transform/MoveToolControl.java

Lines changed: 6 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import com.ss.editor.control.transform.EditorTransformSupport.PickedAxis;
1616
import com.ss.editor.control.transform.EditorTransformSupport.TransformationMode;
1717
import com.ss.editor.util.LocalObjects;
18-
import com.ss.rlib.geom.util.AngleUtils;
1918
import org.jetbrains.annotations.NotNull;
2019

2120
/**
@@ -198,92 +197,51 @@ public void processTransform() {
198197
final Vector3f contactPoint = result.getContactPoint(); // get a point of collisionPlane
199198

200199
//set new deltaVector if it's not set
201-
if (editorControl.getDeltaVector() == null) {
202-
editorControl.setDeltaVector(translation.subtract(contactPoint));
203-
}
204-
205-
if (Config.DEV_TRANSFORMS_DEBUG) {
206-
System.out.println("contactPoint " + contactPoint);
200+
if (Float.isNaN(editorControl.getTransformDeltaX())) {
201+
editorControl.setTransformDeltaX(translation.getX() - contactPoint.getX());
202+
editorControl.setTransformDeltaY(translation.getY() - contactPoint.getY());
203+
editorControl.setTransformDeltaZ(translation.getZ() - contactPoint.getZ());
207204
}
208205

209206
// add delta of the picked place
210-
contactPoint.addLocal(editorControl.getDeltaVector());
211-
212-
if (Config.DEV_TRANSFORMS_DEBUG) {
213-
System.out.println("contactPoint with delta " + contactPoint);
214-
}
207+
contactPoint.addLocal(editorControl.getTransformDeltaX(), editorControl.getTransformDeltaY(),
208+
editorControl.getTransformDeltaZ());
215209

216210
final Vector3f difference = contactPoint.subtract(translation, local.nextVector());
217211
float distanceToContactPoint = translation.distance(contactPoint);
218212

219-
if (Config.DEV_TRANSFORMS_DEBUG) {
220-
System.out.println("distanceToContactPoint " + distanceToContactPoint);
221-
}
222-
223213
// Picked vector
224214
final PickedAxis pickedAxis = editorControl.getPickedAxis();
225215
final TransformationMode transformationMode = editorControl.getTransformationMode();
226216
final Vector3f pickedVector = transformationMode.getPickedVector(transform, pickedAxis, camera);
227217
final Quaternion rotation = parentNode.getLocalRotation();
228218

229-
if (Config.DEV_TRANSFORMS_DEBUG) {
230-
System.out.println("pickedVector " + pickedVector + ", rotation " + rotation);
231-
}
232-
233219
// the main formula for constraint axis
234220
final Vector3f normalizedDifference = local.nextVector(difference).normalizeLocal();
235221

236-
if (Config.DEV_TRANSFORMS_DEBUG) {
237-
System.out.println("normalizedDifference " + normalizedDifference);
238-
}
239-
240222
float angle = normalizedDifference.angleBetween(rotation.mult(pickedVector, local.nextVector())
241223
.normalizeLocal());
242224

243-
if (Config.DEV_TRANSFORMS_DEBUG) {
244-
System.out.println("angle " + angle + ", degree " + AngleUtils.radiansToDegree(angle));
245-
}
246-
247225
float distanceVec2 = distanceToContactPoint * FastMath.sin(angle);
248226

249-
if (Config.DEV_TRANSFORMS_DEBUG) {
250-
System.out.println("distanceVec2 " + distanceVec2);
251-
}
252-
253227
// fix if angle>90 degrees
254228
Vector3f perpendicularVec = collisionPlane.getLocalRotation()
255229
.mult(Vector3f.UNIT_X, local.nextVector())
256230
.multLocal(distanceVec2);
257231

258-
if (Config.DEV_TRANSFORMS_DEBUG) {
259-
System.out.println("perpendicularVec " + perpendicularVec);
260-
}
261-
262232
Vector3f checkVec = contactPoint.add(perpendicularVec, local.nextVector())
263233
.subtractLocal(contactPoint)
264234
.normalizeLocal();
265235

266-
if (Config.DEV_TRANSFORMS_DEBUG) {
267-
System.out.println("checkVec " + checkVec);
268-
}
269-
270236
float angleCheck = checkVec.angleBetween(normalizedDifference);
271237

272238
if (angleCheck < FastMath.HALF_PI) {
273239
perpendicularVec.negateLocal();
274240
}
275241

276-
if (Config.DEV_TRANSFORMS_DEBUG) {
277-
System.out.println("perpendicularVec2 " + perpendicularVec);
278-
}
279-
280242
// find distance to move
281243
float distanceToMove = contactPoint.addLocal(perpendicularVec).distance(translation);
282244

283-
if (Config.DEV_TRANSFORMS_DEBUG) {
284-
System.out.println("distanceToMove " + distanceToMove);
285-
}
286-
287245
// invert value if it's needed for negative movement
288246
if (angle > FastMath.HALF_PI) {
289247
distanceToMove = -distanceToMove;

src/main/java/com/ss/editor/control/transform/RotationToolControl.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,15 @@ public void processTransform() {
7575
final Vector2f selectedCoords = local.nextVector(transformOnScreen.getX(), transformOnScreen.getY());
7676

7777
//set new deltaVector if it's not set
78-
if (editorControl.getDeltaVector() == null) {
79-
final Vector3f delta = new Vector3f(selectedCoords.getX(), selectedCoords.getY(), 0);
80-
delta.subtractLocal(cursorPos.getX(), cursorPos.getY(), 0);
81-
editorControl.setDeltaVector(delta);
78+
if (Float.isNaN(editorControl.getTransformDeltaX())) {
79+
editorControl.setTransformDeltaX(selectedCoords.getX() - cursorPos.getX());
80+
editorControl.setTransformDeltaY(selectedCoords.getY() - cursorPos.getY());
8281
}
8382

8483
// Picked vector
8584
final TransformationMode transformationMode = editorControl.getTransformationMode();
8685
final Vector3f pickedVector = transformationMode.getPickedVector(transform, editorControl.getPickedAxis(), camera);
87-
final Vector3f deltaVector = notNull(editorControl.getDeltaVector());
86+
final Vector3f deltaVector = local.nextVector(editorControl.getTransformDeltaX(), editorControl.getTransformDeltaY(), 0F);
8887

8988
// rotate according to angle
9089
final Vector2f cursorDirection = selectedCoords.subtractLocal(cursorPos).normalizeLocal();

0 commit comments

Comments
 (0)