Not updating world position

I just started making a gun manager class for my User AppState. What I am doing is creating a Node for each gun then attaching it to the hand of my model using the skeleton attachment node. If I do this in the User AppState it updates the position of the gun but when I try it using the new class is shows on the model correctly but the world translation does not update. why might this be?



[java]

private Node pistol;

SkeletonControl skelControl = app.getStateManager().getState(UserAppState.class).getPlayerModel().getControl(SkeletonControl.class);

Hand = skelControl.getAttachmentsNode("Ulna.R");

Hand.detachAllChildren();

Hand.attachChild(pistol);



pistol.getWorldTranslation() //World translation = (0,0,0)

[/java]



Thanks!

In your update loop you need to do

[java]pistol.setLocalTranslation(objectToFollow.getLocalTranslation());[/java]

That way on each pass of the update loop the pistol’s location will be set to the location of objectToFollow.

Isn’t that what attachment nodes are meant to do automatically?

1 Like

Thanks for a solution,

but then I guess my question is, how come I didn’t need to do this when using the same code in my base class?

I’m just trying to figure out what’s different.

@zarch said:
Isn't that what attachment nodes are meant to do automatically?

This was my assumption as well.

You can’t just change the local translation variable and expect the system to notice it, you have to always set the local values via their setters, getLocalTranslation().set() is a no-go.

The initialization of my app state was getting triggered twice which was causing my problem. it didn’t effect it when it was in the base class but was noticeable branching off.



Thanks for the help.

@constantine said:
The initialization of my app state was getting triggered twice which was causing my problem. it didn't effect it when it was in the base class but was noticeable branching off.

Thanks for the help.


??? Did you call it yourself or something?

initialize() is only called once by the state manager.

I did call it myself. In simpleInitApp() I create all the app states and add them to the state manager. Correct me if I’m wrong but the problem I was having is that they don’t initialize themselves until the next update pass so when I was trying to use some app states in other calls that were also in the simpleInitApp() I was getting null errors and the only way I found to fix it was to call initialize myself.

@constantine said:
I did call it myself. In simpleInitApp() I create all the app states and add them to the state manager. Correct me if I'm wrong but the problem I was having is that they don't initialize themselves until the next update pass so when I was trying to use some app states in other calls that were also in the simpleInitApp() I was getting null errors and the only way I found to fix it was to call initialize myself.

Thats the point. They are supposed to initialize on the render thread.
@constantine said:
I did call it myself. In simpleInitApp() I create all the app states and add them to the state manager. Correct me if I'm wrong but the problem I was having is that they don't initialize themselves until the next update pass so when I was trying to use some app states in other calls that were also in the simpleInitApp() I was getting null errors and the only way I found to fix it was to call initialize myself.


Right, they will be initialized on the next update pass before simpleUpdate is called. If you are trying to call them before that then something is weird about your design, I think.

Obviously I don’t know enough about JME. How do you get around a problem like this without calling initialize?



[java]

public void simpleInitApp() {



bulletAppState = new BulletAppState();

stateManager.attach(bulletAppState);



userAppState = new UserAppState();

stateManager.attach(userAppState);



userAppState.example(); //Would get a nullpointerexception here on the bulletappstate call



}



public class UserAppState extends AbstractAppState{



SimpleApplication app;

BulletAppState bullet



public void initialize(AppStateManager manager, Application app){

super.initialize(manager, app);

this.app = (SimpleApplication) app;

this.bullet = app.getStateManager().getState(BulletAppState.class).

}



public void example(){

bullet.getPhysicsSpace.add(Object)

}



}

[/java]

1 Like

call example () inside your initialize function



otherwise you can put the initialize code inside stateAttached and pass your SimpleApplication class to the appstate

1 Like

Yeah, it’s not a good example because we can’t see why you’d even want to call that method at that time. The easy answer is “don’t do that” but we’d have to have a specific use-case to offer real suggestions.

I went the route wezrule suggested changed a few things around and got it working. I am by no means an A class software engineer and I know I have flaws in my design but the everyone’s input helps correct that. I appreciate all suggestions given.

1 Like

Threading is hard, fortunately JME handles most of the problems for you if you just stick to the render thread and enqueue anything you need to.