Fotograma contra delta
La misma constante de velocidad aplicada con y sin delta, con la distancia recorrida en cada fila. La segunda solo es rápida en la medida de la tasa de fotogramas.
El editor de arriba está ejecutando este proyecto. Cambia una línea y se recarga.
Clases de Godot usadas
Polygon2DLabelEngine
El código
scripts/main.gd
extends Node
const SPEED: float = 300.0
const MARGIN: float = 70.0
const SIZE: Vector2 = Vector2(46, 46)
var timed: Polygon2D
var untimed: Polygon2D
var timed_label: Label
var untimed_label: Label
var fps_label: Label
var timed_distance: float = 0.0
var untimed_distance: float = 0.0
var track_start: float = 0.0
var track_end: float = 0.0
func _ready() -> void:
_add_background()
var view: Vector2 = get_viewport().get_visible_rect().size
track_start = MARGIN
track_end = view.x - MARGIN
timed = _add_track(view.y * 0.36, Color('#8eef97'))
untimed = _add_track(view.y * 0.68, Color('#fc7f7f'))
timed_label = _add_label('position.x += speed * delta', Vector2(MARGIN, view.y * 0.36 - 70), Color('#8eef97'))
untimed_label = _add_label('position.x += speed', Vector2(MARGIN, view.y * 0.68 - 70), Color('#fc7f7f'))
fps_label = _add_label('', Vector2(MARGIN, 40), Color('#999999'))
func _process(delta: float) -> void:
var timed_step: float = SPEED * delta
var untimed_step: float = SPEED
timed.position.x = _advance(timed.position.x, timed_step)
untimed.position.x = _advance(untimed.position.x, untimed_step)
timed_distance += timed_step
untimed_distance += untimed_step
timed_label.text = 'position.x += speed * delta travelled %d px' % timed_distance
untimed_label.text = 'position.x += speed travelled %d px' % untimed_distance
fps_label.text = 'same speed constant, one frame rate: %d fps' % Engine.get_frames_per_second()
func _advance(from: float, step: float) -> float:
return wrapf(from + step, track_start, track_end)
func _add_track(row: float, color: Color) -> Polygon2D:
var rail: Line2D = Line2D.new()
rail.width = 2.0
rail.default_color = Color('#2a2f3d')
rail.points = PackedVector2Array([Vector2(track_start, row), Vector2(track_end, row)])
add_child(rail)
var marker: Polygon2D = _make_rect(SIZE, color)
marker.position = Vector2(track_start, row)
add_child(marker)
return marker
func _add_label(text: String, at: Vector2, color: Color) -> Label:
var label: Label = Label.new()
label.text = text
label.position = at
label.add_theme_color_override('font_color', color)
label.add_theme_font_size_override('font_size', 20)
add_child(label)
return label
func _make_rect(size: Vector2, color: Color) -> Polygon2D:
var rect: Polygon2D = Polygon2D.new()
rect.polygon = PackedVector2Array([-size * 0.5, Vector2(size.x, -size.y) * 0.5, size * 0.5, Vector2(-size.x, size.y) * 0.5])
rect.color = color
return rect
func _add_background() -> void:
var background: ColorRect = ColorRect.new()
background.color = Color('#12141a')
add_child(background)
background.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)