Attach detach

Hi guys I have a small question

How can I attach an object on the guiNode during the first loop of the simple update method and in the second loop I want to attach a new object with a new position on the guiNode and detach the old object from the first loop.

In the end it should looks like that the object in the HUD is moving but in my tries it always resulted in a path :S

I’m sorry my english really sucks :smiley:

I guess that I should explain my problem in more detail.
I’ve created a mini-map on the HUD with a picture and then I positioned a star at the position of the player.
Every time the player moves it attaches a new star on the calculated position.
This results in a path :frowning:
I tried to remove the old stars, but I haven’t made it.

[java]public class Main extends SimpleApplication {

private PlayerMarker playerPositionOnMap;

public static void main(String[] args) {
}

@Override
public void simpleInitApp() {


    miniMap = new MiniMap(assetManager, settings, guiNode);

    miniMap.setPosition(10, settings.getHeight() - 170 - 10, 1);

    miniMap.showMiniMap();
    


    playerPositionOnMap = new PlayerMarker(guiNode, assetManager, settings, guiFont, miniMap);
    playerPositionOnMap.showPlayerPositionOnMap();

}

@Override
public void simpleUpdate(float tpf) {

    Vector3f positionOfPlayer = playerNode.getLocalTranslation();

    playerPositionOnMap.update(positionOfPlayer);
}

}[/java]

[java]public class MiniMap {
private final AssetManager assetManager;
private Node guiNode;
private AppSettings settings;

private int xCoord = 0;
private int yCoord = 0;
private int zCoord = 0;

public MiniMap(AssetManager inAssetManager, AppSettings inAppSettings, Node inGuiNode){
    assetManager = inAssetManager;
    settings = inAppSettings;
    guiNode = inGuiNode;
}

private void initMiniMap() {

    Picture minimapPicture = new Picture("MinimapPicture");
    
    Material minimapMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    
    minimapMat.setTexture("ColorMap", assetManager.loadTexture("Materials/Pictures/minimap.png"));
    
    minimapMat.getAdditionalRenderState().setBlendMode(RenderState.BlendMode.Alpha);
    
    minimapPicture.setMaterial(minimapMat);
    
    minimapPicture.setHeight(170);
    
    minimapPicture.setWidth(170);
    
    minimapPicture.setLocalTranslation(xCoord, yCoord, zCoord);
    
    guiNode.attachChild(minimapPicture);
}

public void showMiniMap() {
    initMiniMap();
}

public void setPosition(int inXCoord, int inYCoord, int inZCoord) {
    xCoord = inXCoord;
    yCoord = inYCoord;
    zCoord = inZCoord;
}

public int getXCoord() {
    return xCoord;
}

public int getYCoord() {
    return yCoord;
}

public int getZCoord() {
    return zCoord;
}

}[/java]

[java]public class PlayerMarker {

private Node guiNode;
private final AssetManager assetManager;
private AppSettings settings;
private BitmapFont guiFont;
private Vector3f middlePoint;
private MiniMap miniMap;

public PlayerMarker(Node inGuiNode, AssetManager inAssetManager, AppSettings inAppSettings, BitmapFont inGuiFont, MiniMap inMiniMap) {
    guiNode = inGuiNode;
    assetManager = inAssetManager;
    settings = inAppSettings;
    guiFont = inGuiFont;
    miniMap = inMiniMap;
    middlePoint = new Vector3f(miniMap.getXCoord() + 80, miniMap.getYCoord() + 95, 2);
    
}

private void showMarkerPositionOnMiniMap(Vector3f inPositionOfStar) {

    guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
    BitmapText playerPositionOnMap = new BitmapText(guiFont, false);

    playerPositionOnMap.setSize(guiFont.getCharSet().getRenderedSize() * 1);

    playerPositionOnMap.setText("*");

    playerPositionOnMap.setLocalTranslation(inPositionOfStar);

    guiNode.attachChild(playerPositionOnMap);
}

public void showPlayerPositionOnMap() {
    showMarkerPositionOnMiniMap(middlePoint);
}

public void update(Vector3f inPosition) {

    Vector3f positionOfMap = calculatePosition(inPosition);

    showMarkerPositionOnMiniMap(positionOfMap);
}

private Vector3f calculatePosition(Vector3f inPosition) {

    float newXPositionOnMiniMap = (float) (inPosition.x * 0.5 + middlePoint.x);

    float newZPositionOnMiniMap = (float) (-inPosition.z * 0.5 + middlePoint.y);

    return new Vector3f(newXPositionOnMiniMap, newZPositionOnMiniMap, middlePoint.z);
}

}[/java]

I haven’t worked with GUI node so I may be totally off here but why do you create a new star every update, why not just create the star once and then just move it using setLocalTranslation when it should move?

1 Like

I spend so much time and it was so obvious -.-
I just needed a break and than it was crystal clear XD

In the update method I give every time the new Vectors to showMarkerPositionOnMiniMap to set the new position, but I only have to change the position of playerPositionOnMap.

I made a field private BitmapText playerPositionOnMap; to get playerPositionOnMap into a new method where I can set the new position.

[java]public void update(Vector3f inPosition) {
Vector3f positionOfMap = calculatePosition(inPosition);
newShowMarkerPositionOnMiniMap(positionOfMap);

}

private void newShowMarkerPositionOnMiniMap(Vector3f inNewPositionOfStar) {
playerPositionOnMap.setLocalTranslation(inNewPositionOfStar);
}[/java]

But anyway thanks for the hint Johan Maasing =D

1 Like

Just for awareness, the reason your original attempt didn’t work and resulted in a “path” (which I guess means the object smeared across the screen) was because you never removed the old object. Your subject says “Attach detach” so you had the words right but your example code never actually calls detachChild or removeFromParent.

Still, just moving the object is better but I thought you might want to know why the original less-efficient way didn’t work.