-
Notifications
You must be signed in to change notification settings - Fork 1
/
interpolated_rigid_body_2d.gd
50 lines (34 loc) · 1.36 KB
/
interpolated_rigid_body_2d.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Copyright © 2019 Hugo Locurcio and contributors - MIT License
# See `LICENSE.md` included in the source distribution for details.
extends RigidBody2D
const SPEED := 50
onready var sprite := $Sprite as Sprite
export var use_interpolation := true
# The accumulated motion
var motion := Vector2()
# Interpolation timer (set to 0.0 on every physics step)
var interp_timer := 0.0
# Recent transforms
var transforms := [transform, transform]
func _ready() -> void:
sprite.set_as_toplevel(true)
func _process(delta: float) -> void:
interp_timer += delta
var old_transform: Transform2D = transforms[transforms.size() - 2]
var new_transform: Transform2D = transforms[transforms.size() - 1]
if use_interpolation:
sprite.transform = old_transform.interpolate_with(new_transform, interp_timer * Engine.iterations_per_second)
else:
sprite.transform = transform
func _integrate_forces(_state: Physics2DDirectBodyState) -> void:
interp_timer = 0.0
transforms.push_back(transform)
transforms.pop_front()
# Acceleration
motion.x += Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
motion.y += Input.get_action_strength("move_down") - Input.get_action_strength("move_up")
# Friction
motion *= 0.85
linear_velocity = motion * SPEED
func _on_interpolation_button_toggled(button_pressed: bool) -> void:
use_interpolation = button_pressed