0
\$\begingroup\$

I want my player entity to follow a path, which works, but it looks very roughly. Like it would stuck everytime.

    bool arrivedX = false;
    bool arrivedY = false;

    float speed = 0.06f;

    if(bestway.empty()) {
        //arrived
    } else {

        if(entity->x < bestway.front()->x * gridSize) {
            entity->x += speed * machine->getDeltaTime();
        } else if(entity->x > bestway.front()->x * gridSize) {
            entity->x -= speed * machine->getDeltaTime();
        } else {
            arrivedX = true;
        }

        if(entity->y < bestway.front()->y * gridSize) {
            entity->y += speed * machine->getDeltaTime();
        } else if(entity->y > bestway.front()->y * gridSize) {
            entity->y -= speed * machine->getDeltaTime();
        } else {
            arrivedY = true;
        }

        if(arrivedX && arrivedY) {
            bestway.pop_front();
        }
    }

the vector contains grid coords from 0 to WIDTH or HEIGHT / gridSize. And the entity has pixels coords. I have tried already:

entity->x / gridSize < bestway.front()->x

which runs smoothly, but when it goes up or right it will omit one grid tile. I think it is because of when the player is at pixel 19 and the gridSize is 20 it would calculate 0 . 19/20 = 0... I tried to ceil and floor it, but it didn't work out. Hope you guys can help me.

edit:

        int ix = int( x + 0.5f );
        int iy = int( y + 0.5f );

        if(ix < bestway.front()->x * gridSize) {
            x += speed * machine->getDeltaTime();
        } else if(ix > bestway.front()->x * gridSize) {
            x -= speed * machine->getDeltaTime();
        } else {
            arrivedX = true;
        }

Video

http://youtu.be/-DugHbMdODg

\$\endgroup\$
  • \$\begingroup\$ What are the types of entity->x and gridSize and bestway.front()->y? \$\endgroup\$ – akaltar Mar 12 '15 at 14:52
  • \$\begingroup\$ hi. they're all integers. \$\endgroup\$ – Kronos Mar 12 '15 at 15:00
  • \$\begingroup\$ And what is deltatime? What unit and what type? Also, what is your update frequency? \$\endgroup\$ – akaltar Mar 12 '15 at 15:04
  • \$\begingroup\$ Deltatime is float most time between 30 and 60. But now I saw that when it stucks deltatime ist above 100. I use the SDL_iPhoneSetAnimationCallback() method for updating. So I don't know how often it updates. I have to figure this out first. \$\endgroup\$ – Kronos Mar 12 '15 at 15:33
1
\$\begingroup\$

I believe your error arises from rounding errors.

I believe the problem could be solved by using float for pixel-coordinate storage. This way you will always get the exact increment needed for the given delta time, which is usually very small and thus the resulting per update increment is less than 1, and truncated to 0.

Don't forget to use proper rounding when converting back for rendering or comparison. For rendering I would use: int ix = (int)( entity->x + 0.5 ) Which will yield 'proper' rounding: up from 0.5 and down with anything less.


Edit:

From the video it seems you are overshooting the position you are aiming for. There are three ways to solve this:

  • After moving, check if you overshot and correct for it:

    entity->x += speed * dt;
    if( entity->x > movelocation->x * gridSize )
        entity->x = movelocation->x * gridSize;
    
  • Lower the speed you are moving with.

  • Use a higher update frequency.

The most solid option seems to be checking for overshoot and correcting. Note: The simple code I provided doesn't account for the movement speed lost when arriving at each tile.

\$\endgroup\$
  • \$\begingroup\$ Thanks for the answer. I cannot easily use float since I'm using the SDL_Rect x, y coords which are integers. And I really will not use a Wrapper or tricky stuff. Any idea? \$\endgroup\$ – Kronos Mar 12 '15 at 16:21
  • \$\begingroup\$ Still is shaky... that is the better description for it. looks like it would increment, then decrement and so on... edit: used floats for x,y coords and updating the SDL_Rect so I can use it as well \$\endgroup\$ – Kronos Mar 12 '15 at 16:43
  • \$\begingroup\$ @Kronos could you post the new code too? I showed how to convert the float to an integer, it should be smooth. I see no real reason for the jumping forward and backward.. A gif or video of the issue would be helpful as well. Also: Are you warning-free? You should usually treat compiler warning as errors. \$\endgroup\$ – akaltar Mar 12 '15 at 17:56
  • \$\begingroup\$ Hi. there are no Warnings. I edited the original post and added a video. Hope this will help. \$\endgroup\$ – Kronos Mar 12 '15 at 18:36
  • \$\begingroup\$ @Kronos Edited, I guess this will solve your problem. \$\endgroup\$ – akaltar Mar 12 '15 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.