I am trying to figure out how to animate the rotation of a wind turbine based on the changing direction of wind. Also I am hoping to animate the blades so it spins faster as the wind speed increases and slows down as the wind speed decreases. I have been working on a game, but the graphics are 2D pixel art. I don’t know how to even begin figuring out how to animate a voxel object on a 2D tilemap. Any suggestions on where I can learn how to do this? Below is a screenshot of what I have so far:
That white column thing on the right is supposed to be a wind turbine. The idea is when the player places a wind turbine, the column, the turbine base, and the turbine blades would then show. The turbine base and blades would re-orient based on the direction of the wind, and the blades would spin faster or slower, depending on the wind speed. I have no idea how to do this however, so if any of you know of any tutorials or resources I can learn from, I would appreciate suggestions.
If the turbine is a 2D tile/sprite, you can use animated tiles. Create an animated tile for every possible direction and speed, swap out the tile using set_cell() depending on the weather. Animated tileset tutorial.
If you want more flexibility, you can create the turbine as a scene using Sprite2D’s and animate it with an AnimationPlayer or AnimatedSprite2D like you would normally, add a script and setup signals with your weather/wind system so it updates its animation(and animation speed). Then you can add that object to your tileset as a Scene Collection tile which places it like a tile.
You can layer 2D and 3D on top of each other, so if you want to do your voxels in 3D space you can do that. The main thing to bear in mind is that the 3D layer (assuming it’s in front) will be composited on top of the 2D layer, so:
you’ll need to set the 3D layer to have a transparent background
you’ll probably want to use an ortho camera so distance scaling doesn’t screw things up
the 2D layer won’t interact with the z buffer in the 3D layer
You should be able to use the same set_cell() method. Animated tiles are normal tiles with added animation properties(same with scene collection tiles). Only terrain sets(auto-tiles) require a different method as far as I’m aware.
I decided to go with the scenes collection method, but I have never done this before. I made the AnimatedSprite2D as a child of the TileMapLayer:
I made an empty scenes collection in the ‘preview’ TileMapLater already, but I do not know how to add the AnimatedSprite2D named ‘Wind_turbine’ as a scene. Are there any tutorials you know of I can refer to?
You’re adding the scene as a child of the TileMapLayer in the scene tree rather than to the actual tileset. In the tileset dock tab, you need to add a new scene collection tileset.
Click on the add button to create a new scene collection tile source.
Now you have an empty scene collection tile source. Now you can add scenes to the scene collection tile source by pressing the right-most add button.
Select the Wind_turbine.tscn file.
All set. You can now go into the TileMap dock tab, select and paint the wind turbine as tiles.
This is how you use set_cell() to place a scene collection tile by code.
set_cell(Vector2i(x, y), 1, Vector2i.ZERO, 1)
set_cell(
Vector2i(x, y), # position in TileMap to place tile
1, # Tile Source source_id of the scene collection
Vector2i.ZERO, # atlas_coord is always (0, 0) when painting scene collection tiles
1 # alternative_tile is property is used to id the specific scene tile
)
Because scene collection tiles are different from normal tiles, which are normally identified by their Vector2i atlas_coords in a tileset. Scene collection tiles are identified by their integer ID in the alternative_tileproperty instead. Meaning the atlas_coords property is unused and should always be Vector2i.ZERO.