Tutorial: Creating a New Character Class in C# (Legacy)

NOTE: This is a very old tutorial which has been updated in a few places, but can still serve as a reference in getting started.  However, there are newer ways of doing most things described here which are either completed, or will be available in the next Visual3D release. 
This tutorial is designed only for Scripters / Programmers who have (or can install and use) Visual Studio 2008 / 2010 or Visual C# Express 2008 / 2010 (which is free).



Part 1: Adding character asset files

If needed, exporter your model from your favored modeling package (eg. 3ds max, Maya, Softimage XSI) to Ogre .mesh, including .skeleton (for animations) and .material, along with textures, if you have them setup for it.

You can follow tutorials and exporters linked to from the Visual3D Asset Pipeline documentation index page.

Or, you can use a model that comes with the Visual3D SDK installer releases.


Open the windows explorer and browse to: "C:\RealmWare\Visual3D.NET Beta 2.2\Projects" open the folder called "Demo Assets" and then the folder called Models. Create a new folder there and call it "orc" put your model, skeleton, and textures in there.

http://i330.photobucket.com/albums/l431/bloody_grunt/weOrc.jpg


Part 2: Creating The Character in C#

Open up windows explorer (press windows key and 'e' at the same time or open "My Computer" And browse to visual3d.net projects. By default it's located at: "C:\RealmWare\Visual3D.NET Beta 2.2\Projects" Inside that, open the folder called "Demo Assets" and then open the file called "Demo Assets" which is a C# project file. Double click that and it should open up Visual Studio. Inside Visual Studio should see somewhere on the screen a window that has "Solution Explorer" at the top. If you don't see it, click view at the top and click "Solution Explorer".

In the Solution Explorer, scroll down to a folder called "Actors" and expand it. Then right click the folder called "Custom" and go to Add > New Folder. Call it "Orc". Now right click on the orc folder you just created and go to add > new item. A box should pop up that says "Add New Item - Demo Assets" On the left hand side of the box is a list that says "Visual C# items" In the list, Highlight "code" Now in the center of the box select the item called "Class". Name it "orc.cs". A document with some text will pop up in Visual Studio, now delete all the text in it.

Type the following into your C# compiler:

Note: C# is case sensitive, it must be typed with the correct case. 

Put the following at the top of the document you have open in visual studio.

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Visual3D.Commands;
using Visual3D.Demo.Behaviors;
using Visual3D.Demo.Behaviors.Sensors;
using Animation = Visual3D.Graphics3D.Animations.Animation;
using Accel = Visual3D.Physics.AccelLinear;
using Visual3D.Graphics3D;

You don't really need to know what all this does, this is the gritty part of what makes the program run. All you have to do is make sure you add it to the top.

Note: anything following two forward slashes //  Is comments that are not part of the code.

namespace Visual3D.Demo.Actors

{
 

[ComponentType(Name="Jimmy (Ogre)", Group="Entities", Category="Avatars"] 

//NOTE: The above line will cause an instance of this class to automatically be created and added to the Asset Database, so that it is shown in Asset Explorer with a folder path like "Entities/Avatars/Jimmy (Ogre)"

public class Orc : HumanoidAvatar // It Inherits from a class called HumanoidAvatar.
    {
        public Orc(string name, float sizeScale, float speedScalar, int maxHitPoints)  // Inside Humanoid Avatar is variables that the orc is going to use.
            : base(name, "orc.mesh", sizeScale, speedScalar, 100) // here we assign the some of the variables. We've told it to use the Orc model, and at this point, have given it 100 hitPoints


        {
             _missileLauncherPosition = new Vector3(-20, 80, 20);  // If your model fires a gun this tells it where to fire from. You can edit this inside of the architect though, so don't worry about it matching up, I'll show you how to do that later.
            CameraFocusOffset = new Vector3(0, 12, 0); // When you take control of the character, where the camera will go. This will put the camera somewhere behind the Orc.

        }
     protected override void OnInitialized()
        {
            base.OnInitialized();                            // This calls a function in HumanoidAvatar.
        }

  protected override void OnConstructPrototype()
        {
            base.OnConstructPrototype(); // Calls a function in HumanoidAvatar.

            float animSpeed = SpeedScalar * 0.7f; // this is how fast the animations on your character move.
            float runSpeed = SpeedScalar * 3000; // this is how fast your character actually moves across the terrain.

Vector3 strafeLeft = Vec3.UnitX; //This and strafeRight will be explained later.
 

Vector3 strafeRight = Vec3.MinusUnitX; // this is kind of the inverse of the other one.


            Animation attackAnim = new Animation(AvatarCommands.Action1, "strike", animSpeed, null, false); //Note: If your character has a gun, replace action1 with FireWeapon.
 

// everything is keymapped already, AvatarCommands.Action1 uses the left mouse button. this makes an animation called attackAnim, tells it to use left mouse button, tells it what animation to use (note: you can find out the names of the animations of your characters in the multiverse model viewer, the orc's attack animation is called  "Strike") than it tells it how fast to move, next it tells it where to go (but because when you attack your character doesn't move anywhere, this is set to null.), then it tells it whether or not to loop the animation, because the orc hits once than waits before hitting again, we want to only have him do it once until the player tells him to do it again, so we set it to false.
 

Animation runForward = new Animation(AvatarCommands.MoveForward, "run", animSpeed, new Accel(Vector3.UnitZ, 1.0f * runSpeed, 0, 0.05f, false));

// This one is different because instead of staying in one place like the attackAnim, this one actually moves across the terrain. Vector3 refers to world Position. It helps here if  you have a knowledge of 3d modeling. UnitX refers to left and right and UnitY refers to up and down, like on a grid. But UnitZ refers to moving forwards and backwards. 

 So avatarcommands.MoveForward is keymapped to the 'W' key, the animation called "run", it goes as fast as animSpeed, and Moves forwards at the speed of what ever run speed is equal to. The next  0 is called float damping which is kind of like inertia, but don't wory about that now, the 0.05f is how much it moves across the terrain, and false is to check to see if the character can only do the animation while on the ground. If you want  the character to defy physics and be able to move in the air put true, if not false.
 

Animation runBackward = new Animation(AvatarCommands.MoveBackward, "run", 1.3f * animSpeed,  new Accel(Vector3.UnitZ, -08f * runSpeed, 0, 0.05f, false),

// So this is keymapped to 'S', uses run animation, animation moves as fast as animSpeed, and than the same thing as runForward but runSpeed is multiplied by a negative number, which makes the player move backwards.

         new Animation runLeft = new Animation(AvatarCommands.MoveLeft, "run", animSpeed, 0,
                    new Accel(strafeLeft, 0.8f * runSpeed, 0, 0.0005f, false), true);

// keymapped to 'A', the difference here is the strafeLeft we defined above so that it moves across the 'X' axis. It's better if you model has a strafe anim, because run doesn't look right moving side to side, it will work for this though.

  new Animation runRight = (AvatarCommands.MoveRight, "run", animSpeed, 0,
                    new Accel(strafeRight, 0.8f * runSpeed, 0, 0.0005f, false), true),

//keymapped to 'D' same as runLeft, but moves right because it's the inverse of runLeft.
 

             _animationSet.ConfigureAnimations(  //This is easy, just put all the animations you made in here, with a comma between each, except the last one.

attackAnim,

runForward,

runBackward,

runLeft,

runRight

                ); //now your character can move!

  }

Now press Ctrl + F5 to build. If it has no errors, a box will pop up that says something like: "A project of output type of class library can not be started directly."

Good job.

 Part 3: Setting up the Material using Visual3D Material Editor

  Now start up Visual3D.NET and load up a Scene. I'll be loading "The Cove" Alright there is a window called "Asset Library" somewhere on the screen. Inside that expand the item called entities and then expand the folder called "Avatars" inside is an item called "Jimmy" Drag and drop the item anywhere in the scene, POOF! an Orc just appeared where you dropped it!

But he's entirely gray, let's fix that.

Note: For a more in-depth tutorial on how to do the following go here: http://wiki.visual3d.net/Tutorials_Home/Your_first_import_in_Visual3D.NET 

All right in the main view window of Visual3D.NET at the top are some tabs. By default the Scene View tab is selected. Now, select the Model Editor tab. In the Asset Library expand the item called Models and then expand the folder called "Models" then expand the folder called "Orc" inside is orc.mesh drag and drop it into the main view window.

http://i330.photobucket.com/albums/l431/bloody_grunt/modelEditor.jpg

Now press the button "Parts and Materials" to expand it.

http://i330.photobucket.com/albums/l431/bloody_grunt/materials.jpg

Select the first item on the list and the Orc's body will start flashing. Now press the button with "..." in it, next to the material: textbox. A box will pop up that says "Material selection" will pop up.

  http://i330.photobucket.com/albums/l431/bloody_grunt/material2.jpg

Click the button that says: "Create New"

The orc's body will stop flashing and look like a checkerboard, good. Now in the asset library, expand the item called Textures then expand the folder called Models and then Expand the folder called Orc. Inside is a file called "body_UV_DI_RED_1024. Drag and drop that into the main view window. The orc's body will no longer be gray! However is face and eyes will be.

Select the second item on the "Parts and Materials" list and do the same thing, but this time drag and drop the "faceUV_DI_RED_1024". And again with the third item on the list, using "faceUV_DI_RED_1024" again. Good, the orc entirely no longer gray. Now click on the save button near the top of the Material Editor.

Good job you're done, click on the Scene View tab and look at your Orc again, he should have all of his textures!
 

Part 4: Character Weapons and User Controls

Alright, click on the Scene View tab at the top and load up a scene. Such as "The Cove", now in the Asset Library window, expand the item called "Entities" and then expand the folder called "Avatars" Inside is an item called "Orc" drag and drop it anywhere on the scene. Poof! You're orc is where you dragged it, good job.

Now if your character has a gun (the orc does not) click on the properties tab in the object editor window. Go down to "character stats" and expand it. There should be a part that says Missile Launcher position with a text box next to it. With some trial and error you can get it to look like it's the missile's are firing out of your gun. Remember the numbers before the first comma are where it is left to right, the second is where it is up and down, and the third is forwards and backwards. Also, If your characters gun slanting to the right or the left, you can change missile launcher orientation to make it fire directly out of the weapon.

Now you can play around as your orc, he can move forwards, backwards, left, and right and attack. (you have to hold down the left mouse button for him to finish the attack animation)

Note: Even though he has an attack animation, it will do absolutely no damage. It can be programmed in using RealmWare's coldSteel custom weapon, which is what the lizardman uses. 


Thanks

Hope you found this useful!

If you have any questions you can e-mail me at daspartan117@gmail.com