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
entity->x
andgridSize
andbestway.front()->y
? \$\endgroup\$ – akaltar Mar 12 '15 at 14:52deltatime
? What unit and what type? Also, what is your update frequency? \$\endgroup\$ – akaltar Mar 12 '15 at 15:04