# How to move a spatial with TPF to 5 units (Solved)

Hi.

How to move geom Spatial with TPF and SimpleUpdate(float tpf) to 5 units.

I tried:

[java]

@Override

public void simpleUpdate(float tpf) {

Vector3f maLocation = new Vector3f(5, 0, 0);

Vector3f maSpeed = new Vector3f(2, 0, 0);

if (geom.getWorldTranslation().x >= maLocation.x) ;

else geom.move(maSpeed.xtpf, maSpeed.ytpf, maSpeed.z*tpf);

System.out.println(geom.getWorldTranslation());

}

[/java]

but I think I should make something like:

[java]

geom.setLocalTranslation(maLocation.x, maLocation.y, maLocation.x);

–or–

geom.setLocalTranslation(5, 0, 0);

[/java]

But it moves without TPF and speed.

tpf means “time per frame”, so you have to accumulate:

[java]Vector3f maLocation = new Vector3f(5, 0, 0);

float amount=0;

float scale=1;

public void simpleUpdate(float tpf) {

amount+=tpf*scale;

geom.setLocalTranslation(maLocation.mult(amount));

}[/java]

Depends on how fast you want to move your spatial. Usually you have some constant, which defines your moving speed. Here is my way to do this (not necessary the best):

[java]

private enum State

{

WAITING, MOVING;

}

private final float SPEED = 2f;

private State currentState = State.MOVING;

Vector3f moveLocation = new Vector3f(5, 0, 0);

@Override

public void update(float tpf)

{

if (currentState == State.MOVING)

{

float distance = SPEED * tpf;

float remainingDist = spatial.getLocalTranslation().distance(moveLocation);

if (distance >= remainingDist)

{

spatial.setLocalTranslation(moveLocation);

currentState = State.WAITING;

}

else

{

Vector3f trans = spatial.getLocalTranslation();

trans.interpolate(moveLocation, distance / remainingDist);

spatial.setLocalTranslation(trans);

}

}

}

[/java]

Oh, this should probably be implemented in Control, or at least I usually implement this in control. Hope it helps. Thank you very much for your replies!!!

Normen, I tried your code, but the Spatial does not move and has such transformation parameters from (0.0003, 0, 0) to (0.0035, 0, 0).

Code (I placed everything to simpleUpdate):

[java]

public void simpleUpdate(float tpf) {

Vector3f maLocation = new Vector3f(5, 0, 0);

float amount=0;

float scale=1;

amount+=tpf*scale;

geom.setLocalTranslation(maLocation.mult(amount));

}

[/java]

Anyway, thanks to you as now I have learned how to add,multiply vectors.

inShadow, I will test your way today evening. Can you tell me what the difference between update and simpleUpdate methods? Also, what methods of Control are you using and where i can get it?

Thank you.

mifth said:
Normen, I tried your code, but the Spatial does not move and has such transformation parameters Code (I placed everything to simpleUpdate):

Thats the problem, you are not supposed to put these vars in simpleupdate, just use the example as I posted it. Your way they will be inited on each update and thus not get bigger..

Should I place them before simpleInitApp()?

make them fields of the class, yeah.

For my way, you can use simpleUpdate() instead of update(). If you use simpleUpdate() I assume you are putting the code directly in SimpleApplication. There is really nothing wrong with that, but as soon as your code gets larger, you will want to split it to proper classes and unclutter your SimpleApplication. For that you can use either AppStates (for global stuff that needs its update callback) or Controls (for moving objects or similar stuff). For your case, I would suggest making a new class, which should extend AbstractControl. There will be errors until you implement all the missing methods (don’t worry, your editor will help you do that :)). In simpleInitApp() you then simply create a geometry and addControl() to it, and thats everything. I bet its everything in the wiki (I rarely use it).

Oh look, its here: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:custom_controls Hi guys again. Thank you all for your replies. I tested your versions:

Normen version: the spatial moves, but it does not stop in 5 units… it moves and moves constantly. Anyway thank you.

Code:

[java]

package mygame;

import com.jme3.app.SimpleApplication;

import com.jme3.material.Material;

import com.jme3.math.ColorRGBA;

import com.jme3.math.Vector3f;

import com.jme3.scene.Geometry;

import com.jme3.scene.shape.Box;

public class Main2 extends SimpleApplication {

public static void main(String[] args) {

Main2 app = new Main2();

app.start();

}

protected Geometry geom;

Vector3f maLocation = new Vector3f(5, 0, 0);

float amount = 0;

float scale = 1;

@Override

public void simpleInitApp() {

Box b = new Box(Vector3f.ZERO, 1, 1, 1);

geom = new Geometry("Box", b);

geom.updateModelBound();

Material mat = new Material(assetManager, "Common/MatDefs/Misc/SolidColor.j3md");

mat.setColor("m_Color", ColorRGBA.Blue);

geom.setMaterial(mat);

rootNode.attachChild(geom);

flyCam.setMoveSpeed(30);

}

public void move (float tpf) {

amount += tpf*scale;

geom.setLocalTranslation(maLocation.mult(amount));

System.out.println(geom.getWorldTranslation());

}

@Override

public void simpleUpdate(float tpf) {

move(tpf);

}

}

[/java]

inShadow version: works like a charm! Thank you! I will be learning Control! I think it would be cool to put your version in JME3Tests for such noobs like me!

Code:

[java]

package mygame;

import com.jme3.app.SimpleApplication;

import com.jme3.material.Material;

import com.jme3.math.ColorRGBA;

import com.jme3.math.Vector3f;

import com.jme3.scene.Geometry;

import com.jme3.scene.shape.Box;

public class Main extends SimpleApplication {

public static void main(String[] args) {

Main app = new Main();

app.start();

}

protected Geometry geom;

@Override

public void simpleInitApp() {

Box b = new Box(Vector3f.ZERO, 1, 1, 1);

geom = new Geometry("Box", b);

geom.updateModelBound();

Material mat = new Material(assetManager, "Common/MatDefs/Misc/SolidColor.j3md");

mat.setColor("m_Color", ColorRGBA.Blue);

geom.setMaterial(mat);

rootNode.attachChild(geom);

flyCam.setMoveSpeed(30);

}

private enum State

{

WAITING, MOVING;

}

private final float SPEED = 2f;

private State currentState = State.MOVING;

Vector3f moveLocation = new Vector3f(5, 0, 0);

@Override

public void simpleUpdate(float tpf)

{

if (currentState == State.MOVING)

{

float distance = SPEED * tpf;

float remainingDist = geom.getLocalTranslation().distance(moveLocation);

if (distance >= remainingDist)

{

geom.setLocalTranslation(moveLocation);

currentState = State.WAITING;

}

else

{

Vector3f trans = geom.getLocalTranslation();

trans.interpolate(moveLocation, distance / remainingDist);

geom.setLocalTranslation(trans);

}

}

}

}

[/java]

Thank you all!