ancel
November 3, 2011, 5:23pm
1
I’m just try to move a box to another but the setLocalTranslation() wont work for me : (.
[java]import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
/**
test
@author normenhansen
*/
public class Main extends SimpleApplication {
public static void main(String[] args) {
Main app = new Main();
app.start();
}
Geometry geom_Player;
Person p = new Person();
Box Player;
Box b;
@Override
public void simpleInitApp() {
flyCam.setMoveSpeed(100f);
b = new Box(Vector3f.ZERO, 1, 1, 1);
Player = new Box(new Vector3f(-20,2,2),1, 1, 1);
Geometry geom = new Geometry("Box", b);
geom_Player = new Geometry("Player_Geom", Player);
Material Wood = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
Wood.setColor("Color", ColorRGBA.Brown);
Material player_mat = new Material(assetManager,"Common/MatDefs/Misc/Unshaded.j3md");
player_mat.setColor("Color", ColorRGBA.White);
geom.setMaterial(Wood);
geom_Player.setMaterial(player_mat);
rootNode.attachChild(geom);
rootNode.attachChild(geom_Player);
}
@Override
public void simpleUpdate(float tpf) {
System.out.println("Player is at location: " + Player.getCenter() );
System.out.println("The p.goTo(Player.getCenter(), b.getCenter()): " + p.goTo(Player.getCenter(), b.getCenter()));
geom_Player.setLocalTranslation(p.goTo(Player.getCenter(), b.getCenter()));
// geom_Player.setLocalTranslation(0,-2,2); //This wont even work
}
@Override
public void simpleRender(RenderManager rm) {
//TODO: add render code
}
}[/java]
Some output:
The p.goTo(Player.getCenter(), b.getCenter()): (-19.0, 2.0, 2.0)
Player is at location: (-20.0, 2.0, 2.0)
The p.goTo(Player.getCenter(), b.getCenter()): (-19.0, 2.0, 2.0)
Player is at location: (-20.0, 2.0, 2.0)
normen
November 3, 2011, 5:29pm
2
1 Like
ancel
November 3, 2011, 7:46pm
3
Ah, that would explain why some of my functions don’t work.
Last question.
What function would i use to get the cords of the pivot?
My goTo function is getting the difference not the “Global” xyz values.
Main
[java]package mygame;
import com.jme3.scene.Node;
import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
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(3);
app.setSettings(settings);
app.start();
}
Person p = new Person();
Box Player_Box;
Node pivot = new Node(“pivot”);
Geometry geo_Player;
static float x;
Box b;
@Override
public void simpleInitApp() {
flyCam.setMoveSpeed(20f);
b = new Box(Vector3f.ZERO, 1, 1, 1);
Player_Box = new Box(new Vector3f(2,100,2),0.1f,0.1f,0.1f);
geo_Player = new Geometry(“Player”, Player_Box);
Geometry geom = new Geometry(“Box”, b);
Material mat_white = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
mat_white.setColor(“Color”, ColorRGBA.White);
Material mat = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
mat.setColor(“Color”, ColorRGBA.Blue);
geom.setMaterial(mat);
geo_Player.setMaterial(mat_white);
pivot.setLocalTranslation(2, 100, 2);
rootNode.attachChild(pivot);
rootNode.attachChild(geom);
pivot.attachChild(geo_Player);
}
@Override
public void simpleUpdate(float tpf) {
Vector3f newpos = p.goTo(pivot.getWorldTranslation(),b.getCenter() );//getLocalTranslation() dose not work
System.out.println(“New Pos” + newpos);
System.out.println(“geo_player.getLocalTranslation()” +pivot.getWorldTranslation());
pivot.move(newpos);
}
@Override
public void simpleRender(RenderManager rm) {
//TODO: add render code
}
}
[/java]
Function in person
[java] public Vector3f goTo(Vector3f currentPosition,Vector3f target)
{
float C_x,C_y,C_z,T_x,T_y,T_z;
C_x = currentPosition.x;
C_y = currentPosition.y;
C_z = currentPosition.z;
T_x = target.x;
T_y = target.y;
T_z = target.z;
//
if (C_x > T_x && C_x !=T_x)
{
System.out.println("C_x = " + C_x);
C_x -= speed_mov;
System.out.println("AFTER C_x = " + C_x);
}else if(C_x < T_x && C_x !=T_x)
{
System.out.println("C_x = " + C_x);
C_x += speed_mov;
System.out.println("AFTER C_x = " + C_x);
}
return new Vector3f (C_x,C_y,C_z);
}[/java]
output:
C_x = 2.0
AFTER C_x = 1.999
03-Nov-2011 19:35:31 com.jme3.scene.Node attachChild
New Pos(1.999, 100.0, 2.0)
INFO: Child (Player) attached to this node (pivot)
geo_player.getLocalTranslation()(2.0, 100.0, 2.0)
03-Nov-2011 19:35:31 com.jme3.renderer.lwjgl.LwjglRenderer updateUniformLocation
INFO: Uniform m_VertexColor is not declared in shader [ShaderSource[name=Common/MatDefs/Misc/Unshaded.vert, defines, type=Vertex], ShaderSource[name=Common/MatDefs/Misc/Unshaded.frag, defines, type=Fragment]].
C_x = 3.999
AFTER C_x = 3.9980001
New Pos(3.9980001, 200.0, 4.0)
geo_player.getLocalTranslation()(3.999, 200.0, 4.0)
C_x = 7.997
AFTER C_x = 7.9960003
New Pos(7.9960003, 400.0, 8.0)
geo_player.getLocalTranslation()(7.997, 400.0, 8.0)
C_x = 15.993
btw you should using setLocalTranslation(). About “What function would i use to get the cords of the pivot?” :
[java]
pivot.getLocalTranslation();
[/java]
Hello, I up this old topic for a simple question.
I create some large objects in a separate Thread, and attach them in the main thread like documented.
But I noticed a strange behaviour if I use setLocalTranslation() inside the Thread (the object is not attached) : sometimes the location is set incorrectly.
I pulled setLocalTranslation() out, and now execute it in the main thread, before attaching my object : it works fine.
Is it normal ? And if so, is there a list of the methods you can’t call inside a Thread ?
normen
March 27, 2012, 9:11pm
6
@ozonegrif said:
But I noticed a strange behaviour if I use setLocalTranslation() inside the Thread (the object is not attached) : sometimes the location is set incorrectly.
That should not matter if the object is not attached, what do you mean by "incorrectly"? How do you attach on the OpenGL thread? I am 98% sure the issue is in your code but you might want to write a test case if you really think you found some issue.
1 Like
That should not matter if the object is not attached. I am 98% sure the issue is in your code
Thank you normen. If you say it's in my code, then it is :)
It's certainly a stupid multithreading conflict somewhere.
I will go deeper !
fyi , I found the problem. It was in my code. I forgot to clone a vector in one of my math tools , and occasionnaly two threads used the same vector at the same time.
I dream of immutable vectors sometimes (…I know, you told me it’s not possible for performance reasons )
normen
March 28, 2012, 8:20am
9
Yeah, they’re not worth the occasional rogue reference. Glad you found the issue.