Skip to content

Commit b06837c

Browse files
committed
fixed saving positions of linked physics models.
1 parent 07da12c commit b06837c

File tree

5 files changed

+74
-12
lines changed

5 files changed

+74
-12
lines changed

src/main/java/com/ss/editor/part3d/editor/impl/scene/AbstractSceneEditor3DPart.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1569,7 +1569,7 @@ private void notifySelected(@Nullable final Object object) {
15691569
}
15701570

15711571
/**
1572-
* Get a position on a scene for a cursor position on a screen.
1572+
* Get a position on a scene for a cursor position.
15731573
*
15741574
* @param screenX the x position on screen.
15751575
* @param screenY the y position on screen.
@@ -1599,6 +1599,20 @@ private void notifySelected(@Nullable final Object object) {
15991599
}
16001600
}
16011601

1602+
/**
1603+
* Get a normal on a scene for a cursor position.
1604+
*
1605+
* @param screenX the x position on screen.
1606+
* @param screenY the y position on screen.
1607+
* @return the normal on the current scene or null.
1608+
*/
1609+
@JmeThread
1610+
public @Nullable Vector3f getSceneNormalByScreenPos(final float screenX, final float screenY) {
1611+
final Camera camera = getCamera();
1612+
final M currentModel = notNull(getCurrentModel());
1613+
return GeomUtils.getContactNormalFromScreenPos(currentModel, camera, screenX, screenY);
1614+
}
1615+
16021616
/**
16031617
* Get the node to place models.
16041618
*
@@ -1896,7 +1910,9 @@ private void openModelImpl(@NotNull final M model) {
18961910
}
18971911
});
18981912

1913+
PRE_TRANSFORM_HANDLERS.forEach(model, Consumer::accept);
18991914
attachModel(model, modelNode);
1915+
POST_TRANSFORM_HANDLERS.forEach(model, Consumer::accept);
19001916
setCurrentModel(model);
19011917
}
19021918

src/main/java/com/ss/editor/part3d/editor/impl/scene/handler/PhysicsControlTransformationHandler.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,12 @@
33
import com.jme3.bullet.control.PhysicsControl;
44
import com.jme3.bullet.control.RigidBodyControl;
55
import com.jme3.scene.Spatial;
6-
import com.jme3.scene.control.Control;
7-
import com.ss.editor.annotation.JmeThread;
86
import com.ss.editor.util.ControlUtils;
97
import com.ss.editor.util.NodeUtils;
10-
import com.ss.rlib.util.array.Array;
11-
import com.ss.rlib.util.dictionary.DictionaryFactory;
12-
import com.ss.rlib.util.dictionary.ObjectDictionary;
138
import org.jetbrains.annotations.NotNull;
149

1510
import java.util.function.Consumer;
1611

17-
import static com.ss.rlib.util.array.ArrayCollectors.toArray;
18-
1912
/**
2013
* The handler to updated positions for physics controls on spatial transformations.
2114
*
@@ -36,6 +29,7 @@ public void accept(@NotNull final Spatial spatial) {
3629
final boolean kinematicSpatial = bodyControl.isKinematicSpatial();
3730
bodyControl.setKinematic(true);
3831
bodyControl.setKinematicSpatial(true);
32+
bodyControl.clearForces();
3933
bodyControl.update(0);
4034
bodyControl.setKinematic(kinematic);
4135
bodyControl.setKinematicSpatial(kinematicSpatial);

src/main/java/com/ss/editor/ui/component/editor/impl/model/ModelFileEditor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ protected void doOpenFile(@NotNull final Path file) throws IOException {
179179

180180
MaterialUtils.cleanUpMaterialParams(model);
181181

182-
final ModelEditor3DPart editor3DState = getEditor3DPart();
183-
editor3DState.openModel(model);
182+
final ModelEditor3DPart editor3DPart = getEditor3DPart();
183+
editor3DPart.openModel(model);
184184

185185
handleAddedObject(model);
186186

src/main/java/com/ss/editor/ui/component/editor/impl/scene/AbstractSceneFileEditor.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,21 @@
88
import static com.ss.rlib.util.ClassUtils.unsafeCast;
99
import static com.ss.rlib.util.ObjectUtils.notNull;
1010
import static javafx.collections.FXCollections.observableArrayList;
11-
1211
import com.jme3.asset.AssetManager;
1312
import com.jme3.asset.MaterialKey;
1413
import com.jme3.asset.ModelKey;
1514
import com.jme3.audio.AudioNode;
15+
import com.jme3.bounding.BoundingBox;
16+
import com.jme3.bounding.BoundingSphere;
17+
import com.jme3.bounding.BoundingVolume;
18+
import com.jme3.bullet.control.PhysicsControl;
1619
import com.jme3.export.binary.BinaryExporter;
1720
import com.jme3.light.DirectionalLight;
1821
import com.jme3.light.Light;
1922
import com.jme3.light.PointLight;
2023
import com.jme3.light.SpotLight;
2124
import com.jme3.material.Material;
25+
import com.jme3.math.Quaternion;
2226
import com.jme3.math.Vector3f;
2327
import com.jme3.renderer.Camera;
2428
import com.jme3.scene.AssetLinkNode;
@@ -44,6 +48,7 @@
4448
import com.ss.editor.model.scene.WrapperNode;
4549
import com.ss.editor.model.undo.editor.ChangeConsumer;
4650
import com.ss.editor.model.undo.editor.ModelChangeConsumer;
51+
import com.ss.editor.model.undo.impl.AddChildOperation;
4752
import com.ss.editor.part3d.editor.impl.Stats3DPart;
4853
import com.ss.editor.part3d.editor.impl.scene.AbstractSceneEditor3DPart;
4954
import com.ss.editor.plugin.api.RenderFilterExtension;
@@ -58,12 +63,12 @@
5863
import com.ss.editor.ui.control.model.ModelPropertyEditor;
5964
import com.ss.editor.ui.control.property.operation.PropertyOperation;
6065
import com.ss.editor.ui.control.tree.action.impl.multi.RemoveElementsAction;
61-
import com.ss.editor.model.undo.impl.AddChildOperation;
6266
import com.ss.editor.ui.control.tree.node.TreeNode;
6367
import com.ss.editor.ui.css.CssClasses;
6468
import com.ss.editor.ui.event.impl.FileChangedEvent;
6569
import com.ss.editor.ui.util.DynamicIconSupport;
6670
import com.ss.editor.ui.util.UiUtils;
71+
import com.ss.editor.util.ControlUtils;
6772
import com.ss.editor.util.*;
6873
import com.ss.rlib.ui.util.FXUtils;
6974
import com.ss.rlib.util.FileUtils;
@@ -1317,6 +1322,36 @@ private void addNewModel(@NotNull final DragEvent dragEvent, @NotNull final Path
13171322
camera.getHeight() - (float) areaPoint.getY());
13181323
final Vector3f result = local.nextVector(scenePoint)
13191324
.subtractLocal(parent.getWorldTranslation());
1325+
1326+
final boolean isPhysics = NodeUtils.children(loadedModel)
1327+
.flatMap(ControlUtils::controls)
1328+
.anyMatch(PhysicsControl.class::isInstance);
1329+
1330+
if (isPhysics) {
1331+
1332+
NodeUtils.updateWorldBound(loadedModel);
1333+
1334+
final BoundingVolume worldBound = loadedModel.getWorldBound();
1335+
1336+
float height = 0;
1337+
1338+
if (worldBound instanceof BoundingBox) {
1339+
height = ((BoundingBox) worldBound).getYExtent();
1340+
height = Math.min(((BoundingBox) worldBound).getXExtent(), height);
1341+
height = Math.min(((BoundingBox) worldBound).getZExtent(), height);
1342+
} else if (worldBound instanceof BoundingSphere) {
1343+
height = ((BoundingSphere) worldBound).getRadius();
1344+
}
1345+
1346+
height /= 2F;
1347+
1348+
final Quaternion localRotation = assetLinkNode.getLocalRotation();
1349+
final Vector3f up = GeomUtils.getUp(localRotation, local.nextVector());
1350+
up.multLocal(height);
1351+
1352+
result.addLocal(up);
1353+
}
1354+
13201355

13211356
assetLinkNode.setLocalTranslation(result);
13221357

src/main/java/com/ss/editor/util/GeomUtils.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,23 @@ public static boolean canAttach(@NotNull final Node node, @NotNull final Spatial
274274
return collision == null ? null : collision.getContactPoint();
275275
}
276276

277+
/**
278+
* Get a contact normal on spatial from screen position.
279+
*
280+
* @param spatial the spatial.
281+
* @param camera the camera.
282+
* @param screenX the screen X coord.
283+
* @param screenY the screen Y coord.
284+
* @return the contact normal or null.
285+
*/
286+
@FromAnyThread
287+
public static @Nullable Vector3f getContactNormalFromScreenPos(@NotNull final Spatial spatial,
288+
@NotNull final Camera camera, final float screenX,
289+
final float screenY) {
290+
final CollisionResult collision = getCollisionFromScreenPos(spatial, camera, screenX, screenY);
291+
return collision == null ? null : collision.getContactNormal();
292+
}
293+
277294
/**
278295
* Get a geometry on spatial from cursor position.
279296
*

0 commit comments

Comments
 (0)