I’m trying to move one object to another bit by bit (i know i posted the same’ish thread before(sorry)) but its going alone the x too far.
The player class has one method that takes in two vector3fs(Player,Target) and returns a value to be used to get closer (by 0.01f) to its target.
[java]package mygame;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
public class Player {
Player()
{}
public Vector3f HeadTowards(Node player,Node target)
{
float players_x = player.getLocalTranslation().getX();
float targets_x = target.getLocalTranslation().getX();
// float targets_y = target.y;
// float targets_z = target.z;
if (players_x > targets_x)
{
players_x = players_x - 0.01f;
}
if(players_x < targets_x)
{
players_x = players_x + 0.01f;
}
return new Vector3f(players_x,0,0);
}
}
[/java]
[java]package mygame;
import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.math.ColorRGBA;
import com.jme3.scene.Node;
import com.jme3.system.AppSettings;
public class Main extends SimpleApplication {
public static void main(String[] args){
Main app = new Main();
AppSettings settings = new AppSettings(true);
settings.setFrameRate(10);
app.setSettings(settings);
app.start();
}
Player p = new Player();
Node pivot = new Node(“Player”);
Node wood = new Node(“Wood”);
@Override
public void simpleInitApp() {
flyCam.setMoveSpeed(6f);
Box Player = new Box( new Vector3f(0,0,0),0.5f,0.5f,0.5f);
Box box2 = new Box(new Vector3f(0,0,0), 1,1,1);
//Set the player block to blue
Geometry blue = new Geometry(“Box”, Player);
Material mat1 = new Material(assetManager,
“Common/MatDefs/Misc/Unshaded.j3md”);
mat1.setColor(“Color”, ColorRGBA.Blue);
blue.setMaterial(mat1);
//Set the Wood to green
Geometry red = new Geometry(“Box”, box2);
Material mat2 = new Material(assetManager,
“Common/MatDefs/Misc/Unshaded.j3md”);
mat2.setColor(“Color”, ColorRGBA.Green);
red.setMaterial(mat2);
pivot.attachChild(blue);
wood.attachChild(red);
rootNode.attachChild(wood);
rootNode.attachChild(pivot);
//Set the players x to be 100 away from the wood
pivot.move(new Vector3f(100,0,0));
wood.move(0,0,0);
}
@Override
public void simpleUpdate(float tpf) {
Vector3f newPos = p.HeadTowards(pivot, wood);
System.out.println("NewPos: " +newPos);
pivot.move(newPos);
System.out.println("X: "+ pivot.getLocalTransform().getTranslation().x);
}
}
[/java]
It should only be going down by 0.01 but its going up by alot. : (
OutPut:
NewPos: (199.98, 0.0, 0.0)
X: 399.96997
NewPos: (399.95996, 0.0, 0.0)
X: 799.92993
NewPos: (799.9199, 0.0, 0.0)
X: 1599.8499
NewPos: (1599.8398, 0.0, 0.0)
X: 3199.6897
NewPos: (3199.6797, 0.0, 0.0)
X: 6399.369
NewPos: (6399.3594, 0.0, 0.0)
X: 12798.729
NewPos: (12798.719, 0.0, 0.0)
X: 25597.447
NewPos: (25597.438, 0.0, 0.0)
X: 51194.883
NewPos: (51194.87, 0.0, 0.0)
X: 102389.75
NewPos: (102389.74, 0.0, 0.0)
X: 204779.5
add it to the HeadTowards() method :
else if(players_x == targets_x) return Vector3f.ZERO.clone();
You aren’t moving by 0.01. You are moving by current x + 0.01… so you double each time, basically.
Note: in case it wasn’t obvious from my previous post, move() is moving relative to the current location.
yes
You also don’t consider the tpf if I’m not mistaken…
I’ve changed the update function and it works : D.
Thanks for putting up with me : P.
[java] public void simpleUpdate(float tpf) {
Vector3f newPos = p.HeadTowards(pivot, wood);
System.out.println("NewPos: " +newPos);
pivot.setLocalTranslation(newPos);
System.out.println("X: "+ pivot.getLocalTransform().getTranslation().x);
}[/java]
@normen
I’ve to go over the basic tutorials again, i probable missed out on that as well while i was rushing.
@pspeed
Thanks that sorted my problem.
@glaucomardano
Good idea.