Monday, October 10, 2011

Working with AI: Movement

To Start spawn an AI grunt unity by going to rollup bar > Entity > AI > Grunt. 






To choose a character scroll down to the model parameter




It is possible that you chose a character with no hands, if so, this is easy to fix. 




 Go to topbar > view > open view pane > character editor.




Go to file > open and open the character model that you have in your scene.  In the bottom left under attachments click add, and call this attachment hands.





In the section to the right of the attachments click on the … after the object, in the file window back out to /Human and then enter the folder Hands.  Find a hand or glove model that looks like it would match the arm position of the character.  





Then click apply.




You now have a character with hands




Save the character (file>save) and close the window.  Select your grunt again and change the model to the character you created.  (click root in the top left corner then follow the path Game > Levels > Name of your Project) 




Now that we have a character in game we want them to move.  One way that we can do this is by adding an AI path (rollup bar > AI > AIPath).
NOTE: if your crysis crashes at this point it is likely due to tablet services.  Open services.msc in windows and stop any tablet tasks (for instance I have to stop Tablet PC Input Service and TabletServicePen).




You can now draw a path by clicking to add points and double clicking to finish the path.  If you are unhappy with the path you can click edit shape to change the points as you would like (remember to select direction at the top in which you would like to move the point.




Once you are happy with your path select your grunt and scroll down to the flow graph section.  Click create to open the flow graph window.  Flow graphs are a powerful, yet daunting, tool that we can use to control objects and AI in our game.  Flow graphs are node based, which you can add by right clicking and selecting what type of node you would like to add.



When you do: Right click > Add node > misc > start it adds a node to the graph, unfortunately you are so far zoomed out that you will not be able to see this node, to fix this Right Click > Fit Graph To View.




Now we should add two more nodes.  First: Right click > add selected entity and second: Right click > add node > AI > AIFollowPathSpeedStance. 
If you look at your nodes you will notice that each node contains several values on the each side with arrows pointing both in and out of the node.  These values correspond to inputs and outputs, if you click/drag on an output arrow of a node you will notice that a blue arrow appears that you can move around, this allows you to direct the output from one node into the input of another node.
We want to have the following I/O in our graph Start>output to Grunt>Enable and Grunt>Enabled to AIFollowPathSpeedStance>sync.  If done correctly it should look like:





Now if you look closer at AIFollowPathSpeedStance you will notice that the top value is highlighted in read and reads ‘choose entity’ we need to assign it the entity that we would like to follow the path.  To do so make sure that the grunt is selected in your scene and rightclick on the value and click ‘assign selected entity’.
With the AIFollowPathSpeedStance selected you will notice the input window in the top right, this allows us to change the values that are used, we want to change ‘StartNearest’ to true and change ‘path_name’ to whatever the name of your AIPath is.  Once you have done so save your flowgraph and make sure it looks like this (with your path_name set correctly):




Now if we jump in game we would expect the grunt to walk along the path, but for some reason he does not.  We need to change one last value for the path to work.  Select your AIPath and in the entity parameters make sure the first value ‘Road’ is set to false.  Now if we jump in game the AI will walk along the path.
NOTE: if you are standing to close to the grunt he will react to you instead of walking along his path.




We can change how the grunt will move along the path by opening the flowgraph and changing the ‘run’ and ‘Stance’ input values on the AIFollowPathSpeedStance.  You can make the grunt stealth walk by selecting Stealth in the Stance input, or make the grunt run by setting run to 3 and stance to combat.





The second way to make grunts move is by use of a tagpoint and a goto, using this method you drag a point onto your map and the grunt will path find his way to the point.

To add a TagPoint go to rollup bar > AI > TagPoint and drag it onto your view window.




And place it in your map wherever you would like




When you are happy with the placement of your tagpoint select your grunt and create a flowgraph.  Add the following nodes : Misc > Start, AI > AIGoto, Entity > GetPos.  Drag arrows from Start>output to GetPos>Get and AIGoTo>sync and from GetPos>Pos to AIGoTo>pos.  Finally assign the grunt as the entity for the AIGoTo and the Tagpoint as the entity for the GetPos.  If done correctly your flowgraph should look like this:




After saving you should be able to jump into the game and see your AI unit walk to the point.  But what if we don’t want the grunt to walk through a certain area?  Well that is where ForbiddenAreas come into play.  To add a forbidden area go to rollupbar > AI > ForbiddenArea and draw an area between your grunt and your TagPoint much like you did with the AI Path (double click to end).  




ForbiddenAreas are regions that you designate that AI units are not allowed to go through, so if you run the game you will notice that your AI unit walks straight through your ForbiddenArea.  Every time you add a ForbiddenArea you have to recalculate the navigation so that the AI knows not to pass through the area.  To do so go to topbar > AI > Generate All Navigation, now you can drop in and your AI unit will walk around the ForbiddenArea.


Friday, September 2, 2011

CryEngine 3 Tutorial:
Beginning Solids.
At the end of this tutorial you will have created a simple 4 wall building with a ceiling, a floor, and a door.




To place a solid first pick "solid" from the right rollupBar.  There are several different solid shapes that can be created, but for now make sure that Box and 4 sides are selected in the solid parameters.




After choosing the correct parameters a solid could be created by click dragging to form the x-y base.  Once released, move your mouse up to choose a z height for your solid and click to finish the solid.




 At the top of the window the grid size can be adjusted so that solids can be made with more precision.  For right now we will choose .5 as our grid size.  The default measurement for Cryengine is 1 unit = 1 meter.


 To start we are going to create a solid that is .5x20x3



Next we will create a solid that is 20x.5x3




We want these too walls to align with each other and form a corner, so we must move them closer to each other.  To do this first select the move tool in the Upper Left.




When the move tool is selected an object can be selected and moved along the x,y, or z planes by clicking on the desired axis and moving the mouse in that direction.  i.e. if the z axis is selected the object can be moved up or down.




We will move the one of the walls so that the two match up but so that they do not come to a corner on the outside.




To resize an object you must first select the object and then click editing mode in the bottom right.




After selecting editing mode the object should turn blue (if it does not, pressing shift+space should turn helpers on.)  Because we want to resize the object so that the face lines up with the other wall we will pick ‘Face’ under selection type.




We will select the desired face, which will then be highlighted in red. And use the x/y/z axises to pull it into the correct place to be flush with the wall.
NOTE: IT IS IMPORTANT TO CLICK ‘RESET XFORM’ EVERY TIME AFTER YOU SCALE OR CHANGE THE SHAPE OF A SOLID.
The ‘reset xform’ button can be found directely underneath the editing mode button on the solid editing tool.




Now we will copy the 2 walls to make 4 walls.  An important note when copying solids is that when you copy you will only be able to move the object in the direction that you have selected at the top of the window (X, Y, Z, or XY.)  Once you have selected the direction you would like to move the copied solid you can copy by hitting CTRL+C.  Once you have moved the new copy to the desired position click to place it.




Once you have copied both walls and lined them up, you should have a large four walled area.




Now to create roof we have two options.  The first option is to copy one one of the walls and rotate it so that it lies horizontally over the building and stretch it to the corners.



 To rotate select the rotate tool in the toolbar.  Once chosen the grid will switch several lines that start at the solid and stretch out at various angles.  To rotate along an axis select the axis from the toolbar and drag the solid until the desired angle is reached.  Alternatively an angle can be typed into x/y/z fields at the bottom of the window to get a more precise rotation.




At this point we run into an issue with the cryengine in that when rotations are performed they sometimes cause the solid to no longer match up on the grid.  At this point you can either force it back onto the grid by manually moving it, or you can delete the solid and create a new one for the roof.



 After we stretch the roof out to all of the corners copy it down to the bottom of the building to make a floor.




Now we plan to insert a door into world so that we can enter the building.  To do so make sure you deselect your current object (esc), then choose entity on the right to bring up the entity browser.  Entities can be found either by looking through folders or typing a name in the filter at the bottom.  At this point we are looking for a door (choose ‘door’ not animated door or advanced door).  Click on the entity door and drag it into the main window, click again to place it.




Now that we have our door entity in the world we need to select a model to represent it.  Click on the model field in the bottom right to bring up the model browser.




Once again, items can be browsed for, or by pressing the smart button in the bottom right you can filter for items.




In the filter field input door and it should return 20 or so items.  Models are of the file type .cgf. At this point we run into the second issue with the cryengine, lack of assets.  Now if you have a working option you can skip ahead in the tutorial, but for me personally I had one option for a door, and it was a broken model.  So I had to create the model myself.
This however is one of the strengths of working with solids the cryengine. You are able to create complex geometry using solids and then save them to a cgf file for use as a model (which also helps increase performance).




If we are going to go through the trouble of making our own door, we might as well make it look better than just a square with a wood texture right?  To start our door (metal with a glass pane) we will make a solid that is 1.5 meters wide, .25 meters thick, and 2 meters tall.




The second step is to make a door that is 1.25 meters wide, 1.75 meters tall, and thicker than our first solid.




Move the fatter solid over so that it sits inside the thin solid, but leaves an edge all the way around the thin solid.




Choose the thin solid, and then CTRL+CLICK on the fat solid so that both are selected.  Then click difference in the bottom right (ignore the fact that I accidentally circled union).  You may have to scroll down to see the difference button.  If done correctly your solid should now look like this:




If it does not that means that you selected your objects in the incorrect order. Undo and try again. 




At this point I want to decrease the size of the grid beyond what is an option in the drop down menu.  Instead click setup grid, and in the ‘grid lines every’ field type .0625.  Then click OK.




Create another solid and resize it so that it sits on the inside of your carved solid to make the window.  However, you want this solid to leave a lip between it and the solid in the thickness direction, the solid I created is .06 units thick.




Now select both your outer and inside solid and click merge on the bottom right.  Combining your two solids into one (this is necessary for saving as a .cgf)




Because we want the door to have two separate materials and textures (glass, metal) we need to set up the correct faces on different matIDs.  To get started first click UV Mapping on the right side.




In the bottom right you can see the matID property I was talking about earlier, if you click on a face you can see what material ID it is currently being assigned.  We need to separate the glass and metal onto two separate IDs for this to work properly.  Go through and click each face that makes up the metal frame, change its id to 2 and click assign, repeat until you feel you are done. 




To make sure that you have selected all the faces, switch to matID to 2 and press select.  This will highlight all faces assigned matID 2 on the solid.




After you have changed all the metal faces to matID 2 (you shouldn’t have to change the glass faces as they default to 1 and should be 1) click the soccer ball looking icon (material editor dialog) in the top right.  When the window opens right click on the left and select ‘Add new multi Material’





Choose a filename for your new material.  I chose slidingdoor.




Now right click on your material and choose ‘Set number of sub materials’ change it to 2 and press ok.




Select SubMtl1 to change the material that will displayed on matID 1 surfaces.  Because this is our glass layer I used the following settings:
Diffuse Texture map: Texture/defaults/glasstint.dds
Surface Type: glass_unbreakable (if you choose glass, it will break when hit causing your door to disappear)
Also make sure to change the color to something that looks better than white and since we are using glass change the opacity to 80.

For SubMtl2 I used the following settings:
Diffuse Texture map: Texture/decals/rust_leaking_1.dds
Surface Type: metal




Once you are happy with your final textures and colors. Click the save button at the top to save your texture.  Then click apply to selected object (the top left icon).  If you shoot at it in game you will notice that the materials of the window and door frame respond differently to being shot at.




Now that we have created our object and our texture it is time to save it as a model.  Select the object and select ‘save to CGF’ in the bottom right corner.  Choose a filename I chose slidingdoor and save.
Now that we have saved it, go ahead and delete your current door.  Head back over to your door entity (or create a new one if you deleted it) and click to choose the model for it.










































Now browse to the correct location for your door model that you just saved. Click root in the top left, then browse down the structure Game/Levels/YourProjectName and select the .cgf file that you just saved.




Unfortunately it the texture is not saved to the .cgf when saved so you will have to reapply it. On the right there should be an MTL field, click it and assign the texture we just made.




Congratulations you now have a custom door.  Unfortunately if you go into game mode and use the door you will notice that it does not turn properly.  The way to change this is to change the origin point in a modeling software and re-export the model.  So instead of going through all that work we are just going to make our door a sliding door instead of a rotating door.
On the right under the model you need to change the following values to make a proper rotating door:

Under Rotation change Acceleration to 0 and Range to 0.

Under Slid change Axis to whichever axis you would like it to slid on (I changed my axis to the y because that is what mapped to moving sideways on my model.)  Also change your range to the model size (my model is 1.5 units wide so I would change the range to 1.5 if I want it to slide right, or -1.5 if I want it to slide left.




Now we need to create a hole in the wall for the door, place your door up against the wall to serve as a reference point and select the clipping tool in the bottom right.
NOTE: YOU COULD ALSO MOVE THE WALLS SO THAT THEY SURROUND THE DOOR AND LEAVE A GAP, OR YOU CAN FIGHT WITH THE CLIPPING TOOL.




If you selected the clipping tool go to Display>Cycle 2D Viewport and cycle until you get the 2d perspective where the door is facing towards you on the screen (it will be a yellow square).  You can zoom in using the scroll wheel (every time you switch perspective you have to click in the window to gain focus)




Click so that the two blue dots match up with one side of the door (you may have to drag one of them) and click split on the left.  Now line up the dots with the other side of the door and click Clip Front or Clip Back (I believe the yellow lines points back. Only click the button once, if you did it correctly it should look like this:




If not try using the opposite clip.  Have you been pressing reset xform after you change the size/shape of a solid (remember it is the bottom right below ‘editing mode’?)



Once you have convinced the clip tool to work properly create another solid and place it in the hole above the door.



Congratulations you now can enter your building.  But now you want to add some textures you say?

Select the solid that you would like to apply the texture to and open the material editor (soccer ball top left)






 Select a material (I chose concrete_wall_01), make sure you select a surface type and hit apply to selected object.  If your textures are appearing as all black that means that your diffuse color is too low, change it to something lighter.




If your texture is not tiling well you can change this by clicking uv mapping in the bottom right.  Select the face you would like to change and then the offset (left column) and scale (right column) values and hitting apply.  Note: this only changes the selected face.

That is all for this tutorial.  Play around and continue to mess with solids to make a more amazing building. 

More coming soon hopefully.