Now every frame we can run through each piece in the list, using this progress value as an interpolation weight between its old position and the new position. (You could also store your history as a circular buffer, and just shift the starting offset instead of copying all the data over one notch) ![]() TODO: Handle eating a pellet here and beginning a new growth phase. when finishing the previous growth phase. TODO: Handle activating a new tail segment here We can update the state like so: progress += deltaTime / secondsToCrossOneTile In our positionHistory array, 0 will be the point the head is moving toward now, 1 will be the point the head just left, and snakeSize will be the point the last tail segment just vacated. We'll number our snake parts from 0 for the head up to snakeSize - 1 for the last tail segment. ![]() Then we'll store a progress variable between 0 and 1, that represents how far the snake has progressed from its old position to its new position. I'd solve this by storing an array of the grid points the snake head has run through, and the next point it's on its way toward. (I can copy this over to your original question if you'd like to undelete that one and delete/close this one instead) However, I do not think that is appropriate for snake. Then you can update the motion as soon as input comes, and still have the turn end at the same instant. That would require to have an end_time and check against that, instead of turn_duration. The code above can be modded to interrupt the motion. You can treat no input as going in the same direction if you like. When, the turn ends, we use the input and the last position of the head to compute the next position of the head. We use the elapsed time since the start of the turn to compute the current position via interpolation. Thus, for every part, and for the head, we are storing where it was at the start of the "turn", and where it will be at the end of the turn. Part.current_position = interpolate(part.start_position, part.end_position, elapsed_time) Head.current_position = interpolate(head.start_position, head.end_position, elapsed_time) įor (var part_index = 0 part_index < tail.length part_index++) Head.end_position = compute_next_position(head.start_position, input) ![]() Part.end_position = next_part.start_position įinal_part.start_position = final_part.end_position įinal_part.end_position = head.start_position Var elapsed_time = current_time - start_time įor (var part_index = 0 part_index < tail.length - 1 part_index++) Pseudo-code: var current_time = get_current_time() At the end of the turn the avatar will be aligned to the grid, and a turn always takes the same time. A turn will happen regardless of whatever or not there is input or not.
0 Comments
Leave a Reply. |