Godot Version
4.2
Question
Hi guys, but when I calculate a “steering” as in the graph, is the vector “s” during the physical process calculated per frame in the way I marked the dashes in the graph? Or is my calculation idea wrong?
4.2
Hi guys, but when I calculate a “steering” as in the graph, is the vector “s” during the physical process calculated per frame in the way I marked the dashes in the graph? Or is my calculation idea wrong?
I don’t get the question. What code are we talking about? Is P supposed to be the current position, v the velocity and D a direction? In which way are you applying D to v? (if you’re at all)
it depends on how you code it I guess, description of what going on and what your goal is would a allow for a better answer, if your talking about it moving towards and set interval per frame, there the aptly named move_toward()
Guys I need this because I want to get a good understanding of how to handle vectors. Anyway here is the code, it performs what I want but I want to understand if in a frame “a dashed line” is calculated as I show in the graph and if it is right my idea…
Instead of answering your question, I wrote you a tool to answer the question yourself. Just attach this code to any
Sprite
node, run the scene and then hit the Space key to simulate about roughly 10 frames of movement in one step. The code in the _draw
function will mark the target_position
with a red dot, and draw a line for each of your three vectors direction
(green), velocity
(blue) and steer
(white).
extends Sprite2D
var initial_position := Vector2(100, 100)
var target_position := Vector2(600, 400)
var direction := Vector2()
var dir_speed := 200
var velocity := Vector2()
var vel_speed := 200
var steer := Vector2()
var steer_speed := 10
func _ready():
global_position = initial_position
show_behind_parent = true
func _input(event: InputEvent) -> void:
if event is InputEventKey and event.keycode == KEY_SPACE and event.pressed:
move()
func move() -> void:
var delta := 0.1 # for visualization purposes – the actual delta is a lot smaller!
if global_position.distance_to(target_position) > 1:
direction = (target_position - global_position).normalized() * dir_speed * delta
velocity = transform.y * vel_speed * delta
steer = (velocity + direction) * steer_speed
position = position + steer * delta
queue_redraw()
func _draw() -> void:
draw_line(Vector2.ZERO, direction, Color("green"), 2.0, true)
draw_line(Vector2.ZERO, velocity, Color("blue"), 2.0, true)
draw_line(Vector2.ZERO, steer, Color("white"), 2.0, true)
draw_circle(target_position - global_position, 5, Color("red"))