Model being duplicated

Making a game with friends, and when we added our first weapon (Glock 17) we hit a major problem, when the character moved, the node didn’t update, and the Glock just kept getting redrawn, any help is welcome and never mind the gun rotation yet:P

Note: The drawGlock method is called every tick.

	public void drawGlock(float glockX, float glockY, float glockZ,
			float glockRotateX, float glockRotateY, float glockRotateZ) {
                Node glockNode = new Node();
                glockNode.attachChild(glock);
		glock = assetManager.loadModel("Models/Glock/Glock.obj");
		glockMat = new Material(assetManager,
				"Common/MatDefs/Misc/Unshaded.j3md");
		TextureKey glockKey = new TextureKey("Models/Glock/Glock_Tex_Unlit.tga");
		glockKey.setGenerateMips(true);
		Texture glockTex = assetManager.loadTexture(glockKey);
		glockMat.setTexture("ColorMap", glockTex);
		glock.setMaterial(glockMat);
		glock.scale(2.0f, 2.0f, 2.0f);
		glock.rotate((40.0f - glockRotateX), (90.0f - glockRotateY),
				(-50.0f - glockRotateZ));
		glock.setLocalTranslation(glockX, glockY, glockZ);
		
		glockNode.detachChild(glock);
		
	}

The scene graph doesn’t work like you think it does. (Not only that, you are attaching the glock before you load it.)

First the logic problem in your code:
-attach the old glock
-load a brand new instance of glock
-detach brand new instance of glock

so if draw is called 50 times you will have 50 glocks in your scene.

But really, why not just load it once and move it every frame instead of creating a brand new object every frame? Not to mention that it won’t even be rendered until after this method… way after. So you can’t just swap it in and out like that.

Edit: and now I see that glockNode is never attached to the scene anyway. So this does a lot of work to end up doing nothing, really.

This may help: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:scenegraph_for_dummies

Really all the tutorials are good though, and they cover all this stuff.

Ahh thanks every one, I was writing this late last night, I’ll correct and post how it’s gone:)

1 Like

Ok, I’ve corrected my mistakes (at least I think so), so now drawGlock is only getting called once, and the glock is being moved, but the glock is no longer visible


	public void drawGlock(float glockX, float glockY, float glockZ,
			float glockRotateX, float glockRotateY, float glockRotateZ) {
		glock = assetManager.loadModel("Models/Glock/Glock.obj");
		Node glockNode = new Node();
		glockNode.attachChild(glock);
		glockMat = new Material(assetManager,
				"Common/MatDefs/Misc/Unshaded.j3md");
		TextureKey glockKey = new TextureKey("Models/Glock/Glock_Tex_Unlit.tga");
		glockKey.setGenerateMips(true);
		Texture glockTex = assetManager.loadTexture(glockKey);
		glockMat.setTexture("ColorMap", glockTex);
		glock.setMaterial(glockMat);
		glock.scale(2.0f, 2.0f, 2.0f);
		glock.rotate((40.0f - glockRotateX), (90.0f - glockRotateY),
				(-50.0f - glockRotateZ));
		glock.setLocalTranslation(glockX, glockY, glockZ);
		
		rootNode.attachChild(glockNode);
		
	}

float glockX = player.getPhysicsLocation().get(0);
float glockY = player.getPhysicsLocation().get(1);
float glockZ = player.getPhysicsLocation().get(2);

	glock.move((glockX + 0.75f), (glockY - 1.0f), (glockZ - 1.8f));

@dvd604 said: Ok, I've corrected my mistakes (at least I think so), so now drawGlock is only getting called once, and the glock is being moved, but the glock is no longer visible

If it’s still getting duplicated then you are loading it more than once and adding it to the scene more than once.

A trip through the debugger or some System.out.println()s would tell you for sure. But I can say with 100% certainty that models don’t duplicate themselves.

Read again, It isn’t getting duplicated anymore, it just isn’t visible

EDIT: for a quick test, I attached the glock directly to the rootNode, and it still isn’t visible.

As a test just drop a plain blue cube (as per the first tutorial) in the glock node and see where that appears. If that doesn’t work you know you are setting up position/attaching/whatever wrong. If it does work you know your glock itself is the problem.

If you haven’t already then put something in the skybox too (or set a background colour on the viewport) so if its appearing but completely black you will still see it.

Every time you call move() it is moving it from its last position. I think you want setLocalTranslation()… otherwise it is shooting off into outerspace somewhere.

I swear the post I responded to said “same thing is happening” but it must have been edited by the time I quoted.