Skip to content

Commit bd4ccdd

Browse files
authored
Optimize MotionEvent.computeTargetDirection() for Performance
1 parent 389d91a commit bd4ccdd

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

jme3-core/src/main/java/com/jme3/cinematic/events/MotionEvent.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@
5252
import java.io.IOException;
5353

5454
/**
55-
* A MotionEvent is a control over the spatial that manages the position and direction of the spatial while following a motion Path.
56-
*
55+
* A MotionEvent is a control over the spatial that manages
56+
* the position and direction of the spatial while following a motion Path.
5757
* You must first create a MotionPath and then create a MotionEvent to associate a spatial and the path.
5858
*
5959
* @author Nehon
@@ -70,6 +70,7 @@ public class MotionEvent extends AbstractCinematicEvent implements Control, JmeC
7070
protected Direction directionType = Direction.None;
7171
protected MotionPath path;
7272
private boolean isControl = true;
73+
private final Quaternion tempQuaternion = new Quaternion();
7374
/**
7475
* the distance traveled by the spatial on the path
7576
*/
@@ -79,7 +80,6 @@ public class MotionEvent extends AbstractCinematicEvent implements Control, JmeC
7980
* Enum for the different type of target direction behavior.
8081
*/
8182
public enum Direction {
82-
8383
/**
8484
* The target stays in the starting direction.
8585
*/
@@ -229,18 +229,20 @@ public void write(JmeExporter ex) throws IOException {
229229
@Override
230230
public void read(JmeImporter im) throws IOException {
231231
super.read(im);
232-
InputCapsule in = im.getCapsule(this);
233-
lookAt = (Vector3f) in.readSavable("lookAt", null);
234-
upVector = (Vector3f) in.readSavable("upVector", Vector3f.UNIT_Y);
235-
rotation = (Quaternion) in.readSavable("rotation", null);
236-
directionType = in.readEnum("directionType", Direction.class, Direction.None);
237-
path = (MotionPath) in.readSavable("path", null);
238-
spatial = (Spatial) in.readSavable("spatial", null);
232+
InputCapsule ic = im.getCapsule(this);
233+
lookAt = (Vector3f) ic.readSavable("lookAt", null);
234+
upVector = (Vector3f) ic.readSavable("upVector", Vector3f.UNIT_Y);
235+
rotation = (Quaternion) ic.readSavable("rotation", null);
236+
directionType = ic.readEnum("directionType", Direction.class, Direction.None);
237+
path = (MotionPath) ic.readSavable("path", null);
238+
spatial = (Spatial) ic.readSavable("spatial", null);
239239
}
240240

241241
/**
242-
* This method is meant to be called by the motion path only.
243-
* @return true if needed, otherwise false
242+
* Determines if the motion path needs direction information. This method
243+
* is intended to be called by the motion path only.
244+
*
245+
* @return True if direction information is needed, otherwise false.
244246
*/
245247
public boolean needsDirection() {
246248
return directionType == Direction.Path || directionType == Direction.PathAndRotation;
@@ -249,9 +251,8 @@ public boolean needsDirection() {
249251
private void computeTargetDirection() {
250252
switch (directionType) {
251253
case Path:
252-
Quaternion q = new Quaternion();
253-
q.lookAt(direction, upVector);
254-
spatial.setLocalRotation(q);
254+
tempQuaternion.lookAt(direction, upVector);
255+
spatial.setLocalRotation(tempQuaternion);
255256
break;
256257
case LookAt:
257258
if (lookAt != null) {
@@ -260,10 +261,9 @@ private void computeTargetDirection() {
260261
break;
261262
case PathAndRotation:
262263
if (rotation != null) {
263-
Quaternion q2 = new Quaternion();
264-
q2.lookAt(direction, upVector);
265-
q2.multLocal(rotation);
266-
spatial.setLocalRotation(q2);
264+
tempQuaternion.lookAt(direction, upVector);
265+
tempQuaternion.multLocal(rotation);
266+
spatial.setLocalRotation(tempQuaternion);
267267
}
268268
break;
269269
case Rotation:
@@ -272,6 +272,7 @@ private void computeTargetDirection() {
272272
}
273273
break;
274274
case None:
275+
// no-op
275276
break;
276277
default:
277278
break;
@@ -376,8 +377,7 @@ public Vector3f getDirection() {
376377

377378
/**
378379
* Sets the direction of the spatial, using the Y axis as the up vector.
379-
* Use MotionEvent#setDirection((Vector3f direction,Vector3f upVector) if
380-
* you want a custom up vector.
380+
* If a custom up vector is desired, use {@link #setDirection(Vector3f, Vector3f)}.
381381
* This method is used by the motion path.
382382
*
383383
* @param direction the desired forward direction (not null, unaffected)

0 commit comments

Comments
 (0)