-6

I'm making a GUI for the game (Open Gl). That is, I have a Gui Screen on which other components are added. So I wanted to make all other GUIs move to the left relative to the original position of GuiScreen. It works but not as I wanted simply contrary to logic.

Here i have this situation (this code runs in onUpdate(), just do every frame):

private void moveRelativeContent(Gui guiContent)
{
    guiContent.position.x = guiContent.startPosition.x + this.position.x();
    guiContent.position.y = guiContent.startPosition.y + this.position.y();
}

As you can see I'm just using assignment. That is, I assign the content position from the List<Gui> to the sum of the original content position plus the current GuiScreen position. That is guiContent.position needs each frame always be guiContent.startPosition + this.position;. It's logical. But for me it's work very strange. I have every frame guiContent.position increases. It's like he's using the += command instead of the = command.

Values before method:

Before: ScreenPosX: 0.02
Before: Current Gui PosX: 0.0039999485
Before: Current Gui Start PosX: 0.0039999485

Values after method:

After: ScreenPos: 0.02
After: Current Gui PosX: 0.023999948
After: Current Gui Start PosX: 0.023999948

First look in my initialized GameObject code (include Guis):

public GameObject(float posX, float posY, float posZ, 
                    float rotX, float rotY, float rotZ, 
                        float scaleX, float scaleY, float scaleZ) 
{
    this();
    ...

    this.startPosition = new Vector3f(posX, posY, posZ);
    this.startRotation = new Vector3f(rotX, rotY, rotZ);
    this.startScale    = new Vector3f(scaleX, scaleY, scaleZ);
    this.position = this.startPosition;
    this.rotation = this.startRotation;
    this.scale    = this.startScale;

    ...
}

Here another version's of this constructor:

public GameObject(Vector3f position, Vector3f rotation, Vector3f scale) 
{
    this(position.x(), position.y(), position.z(), 
         rotation.x(), rotation.y(), rotation.z(), 
           scale.x() ,   scale.y() ,  scale.z() );
}

public GameObject(Vector2f position, Vector2f rotation, Vector2f scale) 
{
    this(position.x(), position.y(), 
         rotation.x(), rotation.y(), 
           scale.x() ,  scale.y() );
}

Next where started updated GuiScreen. This code, from my Scene and WorldScene.class in main onUpdate():

@Override
public abstract void onUpdate(); // in Scene

@Override
public void onUpdate() // in WorldScene
{
    if(!this.isInGamePause)
    {
        ... this some in-game code
        this.client.selectedGuiScreen.close(); // close once

    }
    else if(this.isInGamePause)
    {
        ...
        this.client.selectedGuiScreen.open();  // open once
        this.client.selectedGuiScreen.onUpdate();
    }

    ...

    this.updateFrameTime();
    this.client.getGameWindow().updateViewport();
    this.client.getGameWindow().updateWindow();

    ...
}

Next step its my GuiScreen.class ``onUpdate()``` method:

public GameObject onUpdate() 
{
    for(Gui guiContent : this.allContent)
    {
        guiContent.onUpdate();

        if(guiContent.getId() == 0)
        {
            SimplePrint.message("Before: ScreenPosX: " + this.position.x);
            SimplePrint.message("Before: Current Gui PosX: " + guiContent.position.x);
            SimplePrint.message("Before: Current Gui Start PosX: " + guiContent.startPosition.x);
        }

        this.moveRelaviteContent(guiContent);

        if(guiContent.getId() == 0)
        {
            SimplePrint.message("After: ScreenPos: " + this.position.x);
            SimplePrint.message("After: Current Gui PosX: " + guiContent.position.x);
            SimplePrint.message("After: Current Gui Start PosX: " + guiContent.startPosition.x);
        }

        System.exit(0);
    }

    return this;
}

And finally if who interested my guiContent.onUpdate() :

@Override
public GameObject onUpdate() 
{
    if(this.scale.x() <= 0) this.scale.x = 0.2f;
    if(this.scale.y() <= 0) this.scale.y = 0.2f;

    if(this.scale.x() >= 2) this.scale.x = 2f;
    if(this.scale.y() >= 2) this.scale.y = 2f;

    //this.rotateGui();
    return super.onUpdate();
}

Ps: Immediately say that mistakes in other methods I have no. I just double-checked 100 times.

And most interesting the that I in test project tested and guiContent.position = guiContent.startPosition + this.position; worked as I intended. Please help and the I already 3 day not can understand in than the problem.

Taslim Oseni
  • 6,086
  • 10
  • 44
  • 69
  • This is way too less information to find the source of your issue. It is maybe enough to get a rough idea of what you are observing, but you completelty left out all the relevant details that play a role here. Your question is likely to get closed for being too broad if it stays like this. Please see [ask], thanks. – Zabuzard Sep 22 '19 at 10:22
  • I know this is very less info but, in your opinion what could trigger this bug? – Kenny Tutorials Sep 22 '19 at 10:24
  • Please try to provide a [MCVE] that reproduces the issue. Please tell us the values of `guiContent.position`, `guiContent.startPosition` and `this.position` before and after your method. – Max Vollmer Sep 22 '19 at 10:26
  • 1
    This is not about opinions, because there could be hundreds of different reasons. You should add the relevant and necessary code so that we can pinpoint the problem. Or you need to learn [how to use a debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – QBrute Sep 22 '19 at 10:28
  • 1
    *"I know this is very less info but, in your opinion what could trigger this bug?"* This is not how SO works. SO is not a forum to opinionate about bugs, it's a programming Q&A. Your question is supposed to be useful for other people. Our answers are supposed to be useful for other people. That on the way we might help you solve an issue you have is a nice side effect, but not the purpose. – Max Vollmer Sep 22 '19 at 10:28
  • Ok @MaxVollmer wait 2 min – Kenny Tutorials Sep 22 '19 at 10:29
  • 3
    *"My project is too big to write all information here."* The **minimal** in [MCVE] is there for a reason. *Please* **read** the links we provide you here. In no way have you read [How to Ask](https://stackoverflow.com/questions/how-to-ask) and [MCVE] and all the referenced links in those articles in the time that it took you to respond to Zabuza and me. – Max Vollmer Sep 22 '19 at 10:30
  • 1
    *"wait 2 min"* <- Please don't rush. We don't run away, take your time to read the help pages on SO, read other questions with high upvote count, and then take the rest of the day to rewrite your question properly. Good luck! – Max Vollmer Sep 22 '19 at 10:32
  • ```[12:34:02]: [Client thread/INFO]: [Craftix]: Before: ScreenPosX: 0.02 [12:34:02]: [Client thread/INFO]: [Craftix]: Before: Current Gui PosX: 0.0039999485 [12:34:02]: [Client thread/INFO]: [Craftix]: Before: Current Gui Start PosX: 0.0039999485 [12:34:02]: [Client thread/INFO]: [Craftix]: After: ScreenPos: 0.02 [12:34:02]: [Client thread/INFO]: [Craftix]: After: Current Gui PosX: 0.023999948 [12:34:02]: [Client thread/INFO]: [Craftix]: After: Current Gui Start PosX: 0.023999948``` – Kenny Tutorials Sep 22 '19 at 10:35
  • A don't how can increase ```startPosition``` if it not change – Kenny Tutorials Sep 22 '19 at 10:37
  • 1
    How do you initialize `guiContent.position` and `guiContent.startPosition`? Both are objects, so maybe you initialize them with the same object. – Thomas Kläger Sep 22 '19 at 10:58
  • Yes position, startPosition, is Vector3f object where contains x, y, z values – Kenny Tutorials Sep 22 '19 at 11:14
  • @ThomasKläger i add it to post – Kenny Tutorials Sep 22 '19 at 11:21
  • You are assigning the same object to `startPosition` and `position`. Once you do that, if you change the values in that object, it is reflected both in `startPosition` and `position` because they are the same. You need to assign two different objects. – RealSkeptic Sep 22 '19 at 11:25
  • `this.position = this.startPosition;` In a language like C++ this would create a copy, in Java you simply have two references to the same object. Any change to `position` will change `startPosition` and vice versa, because they are the same instance of `Vector3f`. @ThomasKläger nailed it. – Max Vollmer Sep 22 '19 at 11:25
  • And tell me how then is better to do? – Kenny Tutorials Sep 22 '19 at 11:30
  • Just create a different object: `this.position = new Vector3f(posX, posY, posZ);` – Max Vollmer Sep 22 '19 at 11:33
  • But on top of all constructors i create this.position = new Vector3f(0,0,0). – Kenny Tutorials Sep 22 '19 at 11:35

1 Answers1

2

I've reduced your constructor to a minimum to show the problem with this.position and this.startPosition. The same problem exists with this.rotation and this.scale - you have to apply the solution for this.position also to these two fields...

public GameObject(float posX, float posY, float posZ, 
                float rotX, float rotY, float rotZ, 
                float scaleX, float scaleY, float scaleZ) 
{
    this();
    this.position = new Vector3f(0, 0, 0); // 1
    this.startPosition = new Vector3f(posX, posY, posZ); // 2
    this.position = this.startPosition;  // 3
}

On line // 1 you create a Vector3f (A) and assign a reference to it to this.position.

On line // 2 you create another Vector3f (B) and assign a reference to it to this.startPosition.

On line // 3 you overwrite the reference to Vector3f (A) that has been stored in this.position with the reference to Vector3f (B) that you stored in this.startPosition.

From this point on this.position references the same Vector3f as this.startPosition, so this.position.x is just another name for this.startPosition.x (both refer to the same memory location).


To fix your problem you must assign a clone of this.startPosition to this.position

public GameObject(float posX, float posY, float posZ, 
                float rotX, float rotY, float rotZ, 
                float scaleX, float scaleY, float scaleZ) 
{
    this();
    this.position = new Vector3f(0, 0, 0); // 1
    this.startPosition = new Vector3f(posX, posY, posZ); // 2
    this.position = new Vector3f(this.startPosition);  // 3
}

Now, line // 3 creates a new, independent Vector3f that happens to have the same starting values as the one referenced by this.startPosition.

Thomas Kläger
  • 17,754
  • 3
  • 23
  • 34