Tutorial: Create a flint from beginning to end

Introduction

In this tutorial you will learn how to create a very simple object, called a flint, from beginning to end. A flint has the simple purpose to explode upon hiting the landscape. In this tutorial, the Eclipse C4DT plugin will be used to edit all the files, but you can use any environment you like.

Creating the 3D model

You can skip this part if you like, and directly download the needed .mesh and .material files if you want (see link at the end of this chapter). I'm going to "model" this simple shape in Cinema 4D, then use a free plugin to export it to a .xml.mesh file. To start off, we create a simple sphere (sorry for using the german version here).



The scaling is far too huge. Inside Cinema 4D, 1 cm maps to about 1 pixel at maximum zooming level. We'll set the radius to 5 cm. Next, we'll give it a simple color. Create a new material by double-clicking in the empty space at the lower-left corner. A new, grey material will be created. Double click on it to open the material editor.



Disable the specular, only leave the color channel enabled. For the color, I've chosen a darker red. Finally, drag and drop the red material onto the sphere. In the last step, delete the "Phong" tag (the 2 orange balls in the scene hierarchy next to the sphere), because the OpenClonk engine doesn't support Phong shading. Removing the tag gives you a better preview inside C4D of how your object will look like ingame. At last, convert the sphere into an editable object by selecting it, then pressing 'c' on the keyboard. It should finally look like this:



For converting the mesh, I'll use a free exporter plugin for Cinema 4D. You can grab it from here. (install it by choosing your closest C4D version, click on "Datei speichern" (= "save file"), then extract the contents of the file in the Cinema 4D "plugins" folder. It will show up automatically in C4D.)
When exporting, it is important that all vertices contain a position, normal and texcoords (= UV coordinates) data. If one of these are missing, the OpenClonk engine will throw an error. I'm using these export settings:



Notice that even though we deleted our phong tags, this plugin generates a .material file with phong enabled. Also wrote a "emissive" tag in there, which will cause our object to be simply bright white. We also don't need the specular. After fixing the .material file, it should look something like this: material Sphere {

   technique
   {
       pass
       {
           ambient 1 1 1 1
           diffuse 0.8 0 0 1
           emissive 0 0 0 1
       }
   }

} And the Graphics.mesh.xml looks something like this (vertex data abbrieviated) <mesh>

   <submeshes>
       <submesh material="Sphere" usesharedvertices="false" use32bitindexes="false" operationtype="triangle_list">
           <faces count="1848">

..

           </faces>
           <geometry vertexcount="5544">
               <vertexbuffer positions="true" normals="true" texture_coords="1" texture_coords_dimensions_0="2">
               ..

</vertexbuffer>

           </geometry>
       </submesh>
   </submeshes>
   <submeshnames>
       <submeshname name="Sphere" index="0" />
   </submeshnames>

</mesh> For now, we won't worry about this being in the .xml format. If you want to convert it to the much smaller binary ".mesh" file, then use the OgreXMLConverter, available here. You can download the Graphics.mesh.xml and Spehere.material files here.

A moment of success - Fiddling the mesh into an object

If you haven't set up your development environment yet, please do by following the instructions at C4DT_Installation_Guide.
First, create a new testscenario for the flint. To do so, right click right-click on your OC folder in Eclipse, goto "New->Scenario".



Give it any name and description you like in the following dialouge. Next, right click on your new scenario, and goto "New->Objectdefinition". Name it "Flint", give it the description "Explodes", click on finish. Your file structure should look something like this:


Delete the Graphics.png, we won't be using that. Drag and drop the Graphics.mesh.xml and the Flint.material file into the Flint.ocd folder. The Graphics file must be called Graphics(.xml).mesh, the names for the .material file can be arbitrary. We'll edit the DefCore later, for now, run the scenario, and then drag and drop the Flint.ocd from the project explorer inside the viewport. What do we see?


HA! WE DID IT! Congratulations on this first step into OpenClonk development.
But we've still got some work to do. As you can see in the screenshot above, I switched to 'Object mode' by clicking the white array on the engine window on the right, then clicked on the flint. These little white dots you see mark the width and height of the object, and those aren't correct yet. Also, our object is clipping through the earth, because we didn't give the proper collision points (called "Vertices" in the DefCore.txt). Let's handle this now. By the way, your file structure should now look like this:

Correcting the DefCore.txt

So, we have to determine the correct height and width for our object. Since we've got our scenario running and the white cursor in the engine-window selected, we can just do the following: You've surely noticed that when you move your mouse cursor within the viewport, your current mouse coordinates on the map are displayed on the lower left corner of the engine window. Now, we can just write down the coordinate of the top left corner of our object, and the bottom right corner, and derive the width and height from that. My coordinates are:
57 / 318 (top left)
62 / 323 (bottom right)
Subtracting the second X-coordinate from the first X-coordinate yields the width:
62 - 57 = 5
Same for the Y-coordinate.
323 - 318 = 5.
That way we have easily determined our object's dimensions.
Open up the DefCore.txt of the Flint.ocd. The default one looks like this: [DefCore] id=Flint Version=5,2,0,1 Category=C4D_Object Width=8 Height=8 Offset=-4,-4 Vertices=3 VertexX=0,2,-2 VertexY=1,-1,-1 VertexFriction=20 Value=1 Mass=10 Components=Flint=1 Projectile=1 Rotate=1We already figured out that our Width and Height should be 5. But what's all this other stuff? With the Offset you can set where the center of the object should be located. If you set to be 0,0 then the "center" will be at the top left corner of the mesh. Usually, you use the formulas Height=-Width/2,-Height/2But 5 / 2 = 2.5, and we cannot put floating-point valus in there. We have to either always make our object's dimensions divisible by 2, or round. We'll just round 2.5 down to 2. Height=-2,-2 Moving on, What are these "Vertices"? Simply put, these are a set of points on which the object will detect collisions with the landscape. The coordinates here are relative to the offset, don't forget that. Thinking of our flint, where do we want the collision to happen? On the bottom of that sphere ofcourse. This is how we can define that vertex (we'll only setup one here): Vertices=1 VertexX=0 VertexY=3 VertexFriction=20 Playing around the Y position, I've chosen 2. It's actually somewhere between 2 and 3, but being closer to 2. Just play put in some values you think are okay, and see how the engine reacts on it.
Next, there are some values we can play with. Value controls how much the object costs, Mass determines its acceleration when thrown from a height, and also how much it hurts an object if thrown at. Components can be specified so that it may be constructed at a workshop, where a Script can read the Component section of the DefCore. You can delete all of these if wanted, I'll just keep them. However, I'll remove the Rotate=1 from the DefCore, because that would rotate around the vertices when thrown. Since we've only set up one vertex on the bottom, collision would fail again.
Notice that some DefCore values have been moved into the Script.c section, sothat they may be accessible and editable at runtime. "Collectible" is one of these. Now, we'll fix the remaining DefCore in the Script.c, then write the Flint functionality. Our DefCore.txt looks like this: [DefCore] id=Flint Version=5,2,0,1 Category=C4D_Object Width=5 Height=5 Offset=-2,-2 Vertices=1 VertexX=0 VertexY=2 VertexFriction=20 Value=1 Mass=10 Components=Flint=1 Projectile=1

Writing the Script.c

When you open up your default, C4DT generated Script.c, it looks like this. /** Flint Explodes.

@author

  • /

local Name = "$Name$"; local Description = "$Description$";

func Initialize() { } You'll recognize your comment which you could type in the dialogue box before. The @ marks are special for Eclipse, you can use the tags @param @return and @author and special comments when writing functions. Maybe I'll do a short tutorial on that either.
The lines local Name = "$Name$"; local Description = "$Description$"; with these fancy $ signs are also special, they can be used for Internationalization. The $Name$ will be replaced by the corresponding entry in the StringTblDE/US.txt.
But let's just write the functionality of our Flint here. To make the Clonks able to collect the Flint, add a local variable "Collectible" to the object, and set it to 1 (=true).local Collectible = 1;When an object hits the ground, the engine calls the Hit() function inside the object. Refer to the documentation for a complete list of these callbacks. We will use this callback to make our Flint explode, with a radius of 40 pixels. /** Flint Explodes.

@author Gamer

  • /

local Name = "$Name$"; local Description = "$Description$"; local Collectible = 1;

func Initialize() { }

func Hit() {

  Explode(40); 

} Start the scenario again, drag and drop the Flint.ocd in the viewport (not too high!). You'll see, that the vertices are now alligned correctly:


Explosive testing

Throw the flint, and it goes: BOOOM!


The complete scenario can be downloaded from here.