Hi,
I'm currently working on a 2D game where you control spaceships from a top-down perspective. One should be able to click on a spaceship and right click to make it move to that waypoint. I implemented a very basic steering behavior that applies a constant movement and rotation to the Node2D that represents the spaceship to move it in a curve towards the waypoint:
func _process(delta: float) -> void:
transform = transform.translated(Vector2.UP * speed * delta)
rotate(rot_speed * delta)
The actual code is a little more complex, this is just an example. With this example, the spaceship will just move in a circle if speed
and rot_speed
stay constant.
Now my actual problem is the following: I want to give the user a preview of how the spaceship will move when they click to set a waypoint. For this, I want to draw a line between the spaceship and the mouse position. My idea was to pre-calculate the path using the same logic the spaceship uses to move, and save the points in an array to draw them with draw_polyline()
. Late I could make it look better using a different rendering method. I used an empty Transform2D
to try and move it exactly like the spaceship does and save the points along the way (think of it like a pen), but I could not get it to do what I wanted.
var ship_path = PoolVector2Array()
var pen = Transform2D(0, get_viewport().size / 2) # set pen to center of screen
var speed = 25
var rot_speed = deg2rad(4) # 4 degrees per second
var line_segments = 20
for i in range(line_segments * 15): # simulate 15 seconds of movement
ship_path.append(pen.origin)
pen = pen.translated(Vector2.UP * speed / line_segments)
pen = pen.rotated(rot_speed / line_segments)
draw_polyline(ship_path, Color.red)
The problem is, that pen.rotated()
seems to rotate the Transform2D
around its' origin vs. Node2D.rotate()
seems to rotate it around itself, regardless of its' origin. Is there a simple way to rotate a Transform2D
around itself, not the origin, or do I have to code that myself? Additionally, do you have any suggestions on how to do this in a more simple way?
Thanks in advance!