Search code examples
javaeclipselwjgl

How does this code execute?


I have a basic PlayerEntity which on update updates it's position using velocity. This is the whole update method:

public void update() {

    if (!AABB.checkCollisionCB(world, position[0] += velocity[0], position[1], position[2])) {
        //position[0] += velocity[0];
    }

    if (!AABB.checkCollisionCB(world, position[0], position[1] += velocity[1], position[2])) {
        //position[1] += velocity[1];
        }

    if (!AABB.checkCollisionCB(world, position[0], position[1], position[2] += velocity[2])) {
        //position[2] += velocity[2];
        }


    for (int i = 0; i < 3; i++)
        velocity[i] *= .8;
}

Now as you see, the position[0] += velocity[0]; is commented out, and in theory should not execute, but it still does! What is the deal with this? Also, if it still seems to execute when they are uncommented, and the 'if' isn't satisfied!

The only other mention of the PlayerEntity in code is the following:

    if (player.pushingF == true)
        playerEntity.velocity[0] += .05;
    if (player.pushingB == true)
        playerEntity.velocity[0] -= .05;

    if (player.pushingJ == true)
        playerEntity.velocity[1] += .05;
    if (player.pushingS == true)
        playerEntity.velocity[1] -= .05;

    if (player.pushingL == true)
        playerEntity.velocity[2] += .05;
    if (player.pushingR == true)
        playerEntity.velocity[2] -= .05;

But that only changes velocity, and shouldn't do anything position related. I'm using eclipse, I tried 'Project > Clean,' but that seemed to do nothing.. Strangely enough it wont do any printing in the console, or perhaps any other functions from within those 'if's.


Solution

  • if (!AABB.checkCollisionCB(world, position[0] += velocity[0], ...
    

    Notice that you have position[0] += velocity[0] in the condition of your if-statement, and so it will execute regardless of whether or not the condition evaluates to true (and regardless of what the statement's body is).