![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | Sylvain22 |
Hello,
I was moving my 2D code motion to grid movement, in order to ensure a fixed range position. I took some Example I got from Godot 3.2: Godot-Grid-Movement
Based, the code changed brings me some inconsistent 2D motion effect.
The code use _physics_process(delta): and a default speed of 200.
at 60fps default the delta = 0,016666666666667 * 200 = 3,33333333333333
Here is the screen recording:
screencast inconsistent 2D motion effect
The code
func _physics_process(delta):
if not initialized:
return
# MOVEMENT
# increase, the player travels from grid step
var vel = speed * delta
if not float_velocity:
vel = ceil(vel)
var p = position
velocity = want_dir * vel
p += velocity
# if we've moved further than one space
if p.distance_to(last_position) >= grid_size - vel:
p = target_position # snap the player to the intended position
if p != position:
position = p
#print("p: %10s vel %10s targer %10s" % [p, vel, target_position])
if position == target_position:
# IDLE on grid position
get_input()
# get new target (also shift to moving state)
var new_target = target_position + want_dir * grid_size
if new_target != position and is_inside_grid(new_target):
target_position = new_target
# record the player's current idle position
last_position = position
speed = our_max_speed
This seems to be due to the specific value computed here: 3,33333333333333
As the increment velocity. If I switch to integer velocity, I don’t get any motion inconsistency, nor for more float value not 3.3333. Speed 201 or 199 don’t generate any motion inconsistency in float.
I was first seeking about the overlapping element on the screen as the “slow” part of the motion seemed be over the silhouette, but no. This is definitely related to float computation.
Did you ever noticed that?
How did you fixed it?
Regards,
Sylvain.