Godot: Drawing a sprite with rotation

Created on 23 Oct 2016  路  6Comments  路  Source: godotengine/godot

While I was browsing Reddit, I came across a post describing a "fake 3D" method in the game NIUM (thread here).

This seems like a really cool effect, and I was excited to experiment with it, but as I went into the Godot documentation I realized that there's absolutely no way to draw a sprite on the screen with a given rotation, barring manually manipulating the pixels yourself. While technically, you could achieve the same effect by stacking Sprite nodes as a child to the root node (which some people have suggested), this seems like it should be a remarkably inefficient process, as each node is receiving its own callbacks from the engine.

I'd like to request a simple, additional, optional parameter to the draw_texture function family which specifies an angle to rotate the sprite by when drawing it, much like Game Maker has in its draw_sprite_ext function.

For the record, when I was looking for a solution to this, I checked for, in order:

  • A way to rotate the sprite when drawing it
  • A way to make translations independent of rotation when drawing
  • A way to rotate the ImageTexture/Texture before drawing it

None of these came out fruitfully, unless I completely missed something.

archived

Most helpful comment

You can rotate (and translate and scale) if you call draw_set_transform or draw_set_transform_matrix before calling draw_texture().

All 6 comments

You can rotate (and translate and scale) if you call draw_set_transform or draw_set_transform_matrix before calling draw_texture().

BTW, using Sprite nodes is probably easier (since you can just set or animate the rotation) and I don't think it's inefficient. You can really add a lot of nodes before it starts taking a meaningful hit on performance. Sounds like premature optimization. Calling a lot of draw_ functions from GDScript is not the epitome of performance anyway.

Ah. I never noticed that function, I guess. Thanks, though, but incidentally - is there any way to tell when the object rotates without having to check its properties every process? I'm namely talking in-editor - I overrode just about every single rotation-related method on Node2D and superclasses with a print call and a superclass call, but nothing ever printed when I tried rotating the object in the editor.

No, I believe it's not possible to observe a property for changes. But a node doesn't rotate on its own, so you should be able to emit a custom signal when you change the rotation from code.

Ah, I was worried about that. I guess there's nothing to be done about the editor rotation, but actually now that I have the draw_set_transform detail I think this could go well. Thanks!

@MutantOctopus just found out it's actually possible to detect changes in the transform of a node (including rotation):

func _notification(what):
    if what == NOTIFICATION_TRANSFORM_CHANGED:
        # Do stuff when transform changed

Based on my test, this works even if you enable the Sync Scene Changes option and move or rotate the node in the editor.

Was this page helpful?
0 / 5 - 0 ratings