-1

Very new at Box2D, would appreciate help.

I've got a simple function that takes in a b2World and b2Body*, where the body is supposed to fall until it collides with a ground box (already created in main())

The code is essentially as follows:

void snake::update(b2World world, b2Body* snake)
{
    bool running = true;
    Clock deltaClock;
    Time deltaTime;

    while (running)
    {
        deltaTime = deltaClock.getElapsedTime();
        deltaClock.restart();
        world.Step(deltaTime.asSeconds(), 6, 2);

        position = snake->GetPosition();

        char ch;
        if (_kbhit())
        {
            ch = _getch();
            if (ch == 'x')
            {
                running = false;
            }
        }
    }
}

This code was originally implemented in main(), and it worked fine. However, when I tried to move it to its own function in a separate class, the collision no longer works... I've got no idea as to why, as it was literally copy/pasted, just changing around variable names to fit the parameters...

I've got no idea what's going wrong, and if you need any additional information, I'd be more than happy to provide.

Thanks in advance.

  • what does `world.Step` do? does it modify `world`? and if so, was `world` a global variable before? – DJSchaffner Oct 06 '20 at 13:44
  • You are passing `world` by value meaning if this function ever ends (does not appear possible to get out of the loop) the caller will not see a change in `world` – drescherjm Oct 06 '20 at 13:53
  • I think we need a [mcve] a lot of the code in the loop seems pointless or will not work. Like `position = snake->GetPosition();` where is `position` defined? How is it used? Then there is `while (running) {}` does not seem like there is a chance that `running` changes to false. – drescherjm Oct 06 '20 at 13:57
  • @DJSchaffner `world.Step` performs collision detection. AFAIK it does not modify `world`. `world` was not a global variable before, it was created in main and then passed through as a parameter. – Franklin Pugh Oct 06 '20 at 14:15
  • @drescherjm Apologies, I'm clearly very new at this... `position` is defined as a `b2Vec2` within the class. As far as the infinite loop, there are outside conditionals that end the loop, and when I use the same loop in `main()`, it works fine... Additionally, `position = snake->GetPosition();` is used simply to store the position of `snake` and write it to the console. – Franklin Pugh Oct 06 '20 at 14:22
  • @drescherjm Sorry for the confusion, I believe that I worded that poorly. I didn't include it in the original post as I thought it wasn't particularly relevant to my problem. I've edited my post now. – Franklin Pugh Oct 06 '20 at 14:39
  • @FranklinPugh i just checked the reference for `b2World` and `Step` should modify the object, otherwise i don't really understand what it's point would be. Try changing your method head to `void snake::update(b2World& world, b2Body* snake)` and see if that works. – DJSchaffner Oct 06 '20 at 14:55
  • @DJSchaffner It works! I have no idea why, but it does. If you wouldn't mind explaining why? Like I said, I am very new at all of this. Thanks for the help! – Franklin Pugh Oct 06 '20 at 14:58
  • ***It works! I have no idea why, but it does*** I explained that in the second comment to this question. – drescherjm Oct 06 '20 at 16:35
  • I will add an answer for you explaining whats wrong a little and tell you where you can look for a more detailed explanation – DJSchaffner Oct 06 '20 at 16:46

1 Answers1

1

Basically the way you had your parameter set before you were passing the b2World object by value in your original function head.

void snake::update(b2World world, b2Body* snake)

This means you are creating a copy and if this copy is being modified, the original object that was passed to the function remains the same. With the & behind the type you are now passing the object by reference.

void snake::update(b2World& world, b2Body* snake)

This means there is no copy being created but instead the function works with the original object and thus, changes do affect it aswell.

So when you were using this loop before it was probably nested inside of your main function or whatever and could modify the original variable world which was declared in that scope. Now that you have moved it to a seperate function you have to tell the compiler you want to work with the original object.

Read more about passing by value and passing by reference here and here for example

DJSchaffner
  • 562
  • 7
  • 22