A code-free wind turbine in Firemonkey 3D
How would you go about building a wind turbine in Firemonkey 3D with revolving blades? You can stop reading right now if you want and go away and think about this; possibly even fire up XE2 and try building something; but do come back!
When I first thought about this “project” my plan was to find a 3D model on a site like 3D Warehouse. There are many wind turbines and even wind farms out there, but no models with the blades separated from the rest of the structure. If the blades were to revolve then I had to be able to animate them without the rest of the structure rotating as well.
So, onto plan B: Build the whole wind turbine from scratch using only the standard 3D components included in XE2. Sure – it won’t look as pretty – but I figured I’d learn a lot more. And, much to my surprise, it turned out to be quite simple as you’ll see.
So here’s a picture of the desired (and completed) result. Of course you cannot see the blades turning in this image, but follow along and build this project and you’ll see that they really do.
or open this link to see a second or two of the turbine running (captured with Wink).
To create this, start a new Firemonkey 3D project. This type of application is a little hidden away. Choose File | New | Other… and then Firemonkey 3D Application.
With any 3D application, the first thing you should do is add a TLight. When added, all objects are initially positioned on the form designer at (0,0,0). Move the light to the top left of the screen and change its LightType property to ltPoint. You may also want to move the light towards you (that is in the negative Z direction). You can experiment with all the settings as much as you want but I’ll only describe the critical ones in this short article.
Next, from the tool palette, drop a TCylinder on the form. Rotate it to the vertical and stretch it into a long thin tower. Make sure the Structure view is open and ensure the cylinder is not a child object of the light. If it is, drag it to the Form so that it is a sibling of the light.
Next add a TCube and a TCone. Position the cube and cone on top of the tower with the cone in front of the cube. You will have to play with the position and rotation of the cone to get them right.
Now, for the blades. We want to add 3 rectangles as a group so that we can animate them as a single entity. You could add them to the nose cone, but to avoid any complications with the fact the cone is alreadt rotated, and to introduce a powerful general technique for grouping objects in a 3D world, we’ll add a TDummy object. TDummy is a funny name – TGroup3D would have been more descriptive – but simply think of it as a non-visible component that you can use as the parent of other objects. Since we’ll attach the blades to this object we will be able to rotate them simply by rotating the TDummy object.
Now, before adding the blades – which are components of type TRectangle3D, make sure the Blades dummy object is pointing directly at you. It will be if you have not rotated it at all. Add 3 rectangles as children of the Blades dummy object and select each one in turn from the structure view and give 2 of them rotations of 120 and 240 degrees. You will have to reposition them a little as well so they look like they are just attached to the nose cone.
We could have made the nose cone a part of the blades group but it is unnecessary as it does not change its appearance when rotated.
While in this view finish the blades by giving each of them a twist by changing the Y rotation to 25 (degrees).
The design time window should look something like this as this stage:
Lastly you should be able to rotate the main assembly (consisting of cube, cone and group of blades) on top of the tower into a slightly side on view to make the image more interesting. To make rotating the turbine easier (hey, we do want the turbine to face the wind, don’t we?) I added the cone, cube and grouped blades dummy objects to yet another dummy object. You will see this if you download and open the source code.
To animate the blades, we can use a TFloatAnimation object. This provides a code-free means of altering a single property in the attached parent object. In our case, we need to attach the TFloatAnimation to the Blades dummy object (it will now have 3 rectangles as well as this animation object as children). Set the PropertyName property to RotationAngle.Z. This is the property of the parent object that will be animated. Set the Duration to 10 (seconds), Loop and Enabled to True and the StartValue to 0 and the StopValue to 360. These settings will cause the TDummy object to rotate through a full 360 degrees about the Z axis once every 10 seconds repeatedly. And since we attached the blades to the TDummy object we see the blades rotating nice and smoothly.
So, there you go, a code-free wind turbine.
Source code is here.