Sunday, September 18, 2016

40. Plants & Vegetation


       Hello and welcome to my next engine update. My apologies for the long wait. I recently started a new job a while ago and I am adjusting to my new schedule while trying to make time for working on the game engine. Oh, before I forget, the date located in the right hand corner of the following images below were not updated. So... let's get started! With the help of the billboards sample, I recently got the vegetation processor to work in my game engine. As stated on the billboards link, the vegetation processor renders a very large number of billboard sprites using a vertex shader to perform computations entirely on the GPU. Its a fake way of rendering complex objects without bothering to render a full 3D model. This technique helps performance so that there is no additional CPU load from rendering billboards compared to normal static geometry. I tested the vegetation processor in my sample menu system's 3D scene. Initially I encountered some rendering issues with the positioning and transparency of the grass billboards on the 3D terrain model. 


Fixing the Position


       The first of many issues I encountered after getting the grass billboards working in the scene was fixing their position. In the image above, the grass billboards were rendering in the scene but they were positioned way above the terrain model. Click on the image above to see the grass billboards more clearly. They were so far away, it appeared as if there were starts in the sky. In a nutshell, I needed to multiply the view, projection, and world Matrices together to render the grass billboard images over the terrain. Multiplying these three Matrices together get the WorldProjView matrix which is a calculation that can turn a 3D object into pixels.


What is the View Matrix?
       The View Matrix represents the position and orientation of the camera. It is created by passing in the camera location, where the camera is pointing and by specifying which axis represents “Up” in the universe.  XNA uses a Y-up orientation, which is important to be aware of when creating 3D models.  Blender by default treats Z as the up/down axis, while 3D Studio MAX uses the Y-axis as “Up”.


What is the Projection Matrix?
       The Projection Matrix is the actual camera lens which is used to convert 3D view space to 2D.  It is created by specifying calling CreatePerspectiveFieldOfView() or CreateOrthographicFieldOfView(). As a general rule, for a 2D game you use Orthographic, while in 3D you use Perspective projection.  When creating a Perspective view we specify the field of view ( think of this as the degrees of visibility from the center of your eye view ), the aspect ratio ( the proportions between width and height of the display ), near and far plane ( minimum and maximum depth to render with camera… basically the range of the camera ).  These values all go together to calculate something called the view frustum, which can be thought of as a pyramid in 3D space representing what is currently available.


What is the World Matrix?
       The World matrix is used to position your entity within the scene.  Essentially this is your position in the 3D world.  In addition to positional information, the World matrix can also represent an objects orientation.

In short, think of it like so:

  • View Matrix = Camera Location
  • Projection Matrix = Camera Lens
  • World Matrix = Object Position/Orientation in 3D Scene

In short, by multiplying these three Matrices together in the first pass rendering of the the billboards, positioned them correctly. Further explanation is below in the source code.



       Now that the position was fixed, I encountered another issue which is the transparency. With the help of Shawn Hargreave's blog post on depth sorting alpha blended objects, I was able to solve it. Check out the source code to my terrain's draw method with the source code from the billboard sample integrated below. Its not perfect by any means, but its a decent start.


public void DrawTerrain(Model model, Matrix view, Matrix projection, Matrix world)
        {
            Matrix[] transforms = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(transforms);

            // First we draw the ground geometry using BasicEffect.
            foreach (ModelMesh mm2 in model.Meshes)
            {
                if (mm2.Name != "Billboards")
                {
                    foreach (BasicEffect be2 in mm2.Effects)
                    {
                        ScreenManager.GraphicsDevice.BlendState = BlendState.Opaque;
                        ScreenManager.GraphicsDevice.DepthStencilState = DepthStencilState.Default;
                        ScreenManager.GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;

                        be2.View = view;
                        be2.Projection = projection;
                        be2.World = transforms[mm2.ParentBone.Index] * world;

                        be2.EnableDefaultLighting();
                        be2.AmbientLightColor = new Vector3(0.1f);
                        be2.SpecularPower = 5;
                    }

                    mm2.Draw();
                }
            }

            // Then we use a two-pass technique to render alpha blended billboards with
            // almost-correct depth sorting. The only way to make blending truly proper for
            // alpha objects is to draw everything in sorted order, but manually sorting all
            // our billboards would be very expensive. Instead, we draw in two passes.
            //
            // The first pass has alpha blending turned off, alpha testing set to only accept
            // ~95% or more opaque pixels, and the depth buffer turned on. Because this is only
            // rendering the solid parts of each billboard, the depth buffer works as
            // normal to give correct sorting, but obviously only part of each billboard will
            // be rendered.
            //
            // Then in the second pass we enable alpha blending, set alpha test to only accept
            // pixels with fractional alpha values, and set the depth buffer to test against
            // the existing data but not to write new depth values. This means the translucent
            // areas of each billboard will be sorted correctly against the depth buffer
            // information that was previously written while drawing the opaque parts, although
            // there can still be sorting errors between the translucent areas of different
            // billboards.
            //
            // In practice, sorting errors between translucent pixels tend not to be too
            // noticable as long as the opaque pixels are sorted correctly, so this technique
            // often looks ok, and is much faster than trying to sort everything 100%
            // correctly. It is particularly effective for organic textures like grass and
            // trees.
                foreach (ModelMesh mm2 in model.Meshes)
                {
                    if (mm2.Name == "Billboards")
                    {
                        // First pass renders opaque pixels.
                        foreach (Effect effect in mm2.Effects)
                        {
                            ScreenManager.GraphicsDevice.BlendState = BlendState.Opaque;
                            ScreenManager.GraphicsDevice.DepthStencilState = DepthStencilState.Default;
                            ScreenManager.GraphicsDevice.RasterizerState = RasterizerState.CullNone;
                            ScreenManager.GraphicsDevice.SamplerStates[0] = SamplerState.LinearClamp;

                            // At first, only the camera's view was just stored in the effect 
                            // parameter's value for view. Then I multiplied the terrain model's 
                            // parent bone index ( transforms[mm2.ParentBone.Index] ) which is what 
                            // the world matrix is equivalent to by the camera's view matrix to 
                            // fix the position. If we recall, the world matrix is the object's 
                            // position and orientation in the 3D scene. So in order to position 
                            // it correctly, it needed to be multiplied by the camera's location 
                            // which is the camera's view matrix (the variable view).

                            effect.Parameters["View"].SetValue(transforms[mm2.ParentBone.Index] * view);
                            effect.Parameters["Projection"].SetValue(projection);
                            effect.Parameters["LightDirection"].SetValue(lightDirection);
                            effect.Parameters["WindTime"].SetValue(time);
                            effect.Parameters["AlphaTestDirection"].SetValue(1f);
                        }


                        mm2.Draw();

                        // Second pass renders the alpha blended fringe pixels.
                        foreach (Effect effect in mm2.Effects)
                        {
                            ScreenManager.GraphicsDevice.BlendState = BlendState.NonPremultiplied;
                            ScreenManager.GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead;
                            effect.Parameters["AlphaTestDirection"].SetValue(-1f);

                        }

                        mm2.Draw();
                    }
                }
        }


       Now that the positioning and transparency of the grass billboards are fixed, I plan on adding different grass images later to give it more of a variety. If you have found this blog post helpful with your XNA and or MonoGame projects, please comment below. Thanks for reading..


Friday, June 3, 2016

39. Engine Update 5 Teaser


       This is a brief video teaser sneak peek of my next game engine update. Everything is still a work-in-progress. Since rigged and non-rigged models can be attached to the character now, I am working on character customization. Soon, players will be able to customize their armor. They will be able to swap out head-wear such as hats, helmets and mask. Soon they will also be able to swap between chest pieces and knee guards. This might not be fully shown in the next video update but I have this working for the most part. As players progress through the game, the upgrades to their suit will greatly improve their gameplay experience. Lately, I have been building up content for creating the models and assets that will go into the game world. I am improving artificial intelligence at the moment. This is just a teaser, so it will be a while before my next engine video update. I have been focusing on my 2D game and my independent studies to further educate myself and make far more progress. I am taking some time off and working on the gameplay! So excited. Below are some more screenshots of some fun I had with armor customization.







Saturday, May 14, 2016

38. How to Install MonoGame for Visual Studio 2015




37. Install XNA on Visual Studio 2015!



       This video will guide you to install XNA Framework in your Visual Studio. Having difficulty to follow this lesson, feel free to download the ready to use extension for VS 2015 here. Find the XNA4-VS2015.zip file.




36. XNA 4.0 Parallax Occlusion Mapping

     

       Recently, I came across Electronic Meteor's blog post which updated the Parallax Occlusion Mapping Sample to XNA 4.0. In short, parallax mapping is a way to make textures pop out. It's a very efficient way to add depth to a texture, so there is no tessellation here. Parallax Occlusion Mapping was utilized in the hit first-person shooter game called Crysis. This sample also demonstrates another technique called Normal Mapping. In this sample you can switch between the techniques to see each of their effects. The sample was originally created by Alex Alvarex Urban and updated to XNA 3.1 by Canton Javier Ferrero. Thanks to Chris, the sample was updated to XNA 4.0, however he ran into some problems getting the particle system to work. In time, I was able to fix it. I have made the source code available at the following link below for download. If you have found this sample helpful or run into any problems, please comment below.

Also, on a side note, make sure under the File Properties for the smoke.png image, click the drop-down arrow under Content Processor and set the following values like so if they aren't already:

Color Key Enabled = True
Generate Mipmaps = True
Premultiplay Alpha = False
Resize to Power of Two = False
Texture Format = DxtCompressed


Download



Friday, May 13, 2016

35. Creating A Universally Free Education

           

       In this blog post, rather than showing my next game engine update which would still be too early at the moment, I am going to talk about something else. That is, how can we make education universally free if not more affordable. My outlook on education might seem nontraditional and unconventional to many but this is primarily due to the fact that I am for the most part self-taught. Independent studies became my only option to pursue game development because I couldn't afford to the attend the colleges I was accepted to. To my surprise, independent studies became the best option for me. I didn't pursue game development with the intent to make a lot of money; I just simply wanted to learn it. Like myself, there are many who simply just want to pay rent and get an affordable education in their field of interest, but the predatory student load system is making a profit off of our desire to do so. I have always believed in a virtually free higher education. Higher education cannot be a luxury for a privileged few. It is an economic necessity that every family should be able to afford, and every person with a dream and ambition should be able to access. Why do I believe in making a free higher education? Simply put, education is a human right. With that said, you are probably reading thinking:

  • How will teachers get paid?
  • How is it going to be free?
  • Who will see the value in a free education?

       We need to see the value in higher education, and I am not talking about the price tag. The value of having a great quality education is that it can change your life – it can help improve your quality of life. For now, if you are reading this, I just want you to visualize what America would look like if there were to be a free higher quality education. What would that look like? It wouldn’t matter was “Class” you are. It wouldn’t matter what sort of income you make to have this. It wouldn’t even matter where you live because it will be accessible everywhere- in every library, in every school, from every home because it would be made accessible online. No, I am not necessarily talking about online classes. For now, I want you to imagine all of the endless possibilities if education were made free and feel free to post comments below of what you came up with. Education will then be utilized as a way for people to become free of poverty and the “Rich or Upper Class”, the “Middle Class” and the “Poor Class” will be virtually pretty much gone. My belief, my dream is for this country to have a free higher education.

       If this were to truly be free then there will be one so called “Class” in America. I would simply like to call it “The Human Class”. The real reason why we have classes began to divide further apart traces back years ago ever since books were invented. Ever since the first book printed with movable type, created by Gutenberg, this posed a new opportunity as well as a problem. People could access books because they were so full of ‘Knowledge and facts’ and in this was seen as a problem because those in authority saw the “Value” in books. It was a problem to them because jobs at the time were chosen for you. So by making books more accessible, it would allow people to learn different skills outside of the job they were given. Later books were distributed with soaring prices, prices so high that only the few could afford them. This is what made the gap between classes even wider, thus adding fuel to the segregation between the “rich” and the “poor”. I realize I am omitting and skipping a lot of information but just bare with me.


       We need to move education in America to the 21st century. I explain more below brainstorming solutions as to how we can do that:


1.) Video Tutorials
       What if America created a Universal Virtual School that can be accessed online that provided clear comprehensive tutorials taught be some of the best professors in the country that is fully accredited? This is very realistic especially with the advancements in technology we have today. You will learn from the greatest minds of the best professors in the country as well as around the world. What if America paid the professors up front to help create these great quality video tutorials? To further help these professors, what it these videos they help create were published on this virtual school’s YouTube Channel and monetized thus further adding to their salary? The goal of the video tutorials will be to break down hard subjects and topics in easy-to-understand language. With this feature, the student can follow along as they watch the video and pause, fast-forward, and rewind the video at any time in case they missed something or wanted to hear something repeated again. We have seen this work well with YouTube video tutorials as well as resources like Khan Academy, Thinkwell, and Lynda.com.

       The goal is to allow students to essentially work at their own pace and not feel rushed. This would be great for non-traditional students coming to school. This would also be great for students who are working a job or two and or raising a family by helping to work around their schedule. They can create their own schedule to match their needs so that their job doesn’t feel like its compensating for their course work. With video tutorials like these, students will not have to worry about when to sign up for a class and what days and hours the classes are because they are all videos and can be accessed at any time if they were to sign up for this virtual school. This will eliminate many “excuses” and “problems” students run into in traditional schools and colleges. All schools whether it be public or private high-schools and colleges will be able to utilize these video tutorials to be used in the classroom and engage students in new ways. Today’s education system disengages students in schools.


2.) Notes
       What if this Universal Virtual School provided Lecture Notes in the form of pdfs and Power Points to review the material from the videos, complete with key concepts and definitions that students will need to remember?


3.) Interactive Games & Simulations
       What if this school provided students with interactive games and simulations to put into practice their knowledge so they can actually apply what they have learned?




4.) Innovative Test & Quizzes
       What if this Universal Virtual School created by America gave test and quizzes (that are not timed!)? You can’t fully see what someone knows by setting a time-limit. These quizzes will let you put your knowledge directly into action. When you, the student gets your quiz results back, they don’t simply just list which ones you got wrong. They will show you how to work each problem and explain what sort of mistakes you might be making. That is the goal! To help students learn from their mistakes. Knowledge cannot be strictly determined by how many questions students got right or simply multiple choice. These quizzes will score each of its questions out of a percentage by looking at the steps the student takes to arrive to their answer. The questions in the quizzes can change if a student decides to take it over to avoid cheating but they will be ‘similar’. If a student wants to do better they can take the test & quizzes over as many times as they please. 


The Problem with Standardized Testing & How We Determine Grades
       I will explain briefly why I don’t think standardized testing truly determines someone’s intelligence. For one, many students completely guess! I will give a prime example from a personal experience of mine. I had two friends who took the same multiple choice math test. One got an A and the other barely got a B. The one who got an A wasn’t necessarily more knowledgeable or smarter than my friend who got a B. Why you may ask? I looked at both of their test thoroughly. My friend who got the B clearly knew more because I saw him work out the problems to arrive to his answer and I was upset at how close apart the optional answers were on this test. I mean very close. I saw how he worked out his problems and although he got the wrong answer he actually understood what he was doing. By seeing how he worked out his problem, I was able to see his thought process. My friend who got the A had barely anything written down, didn’t work through the problems, didn’t really prepare for the test. I asked him how did he get an A and he simply said, “Dude, I completely guessed on all of them haha!” This kind of irritated me a little. The problem I see in education is that we measure someone’s intelligence on standardized test strictly by how many they got right or wrong without knowing their thought process and many students are taught to guess the “most probable” answers.


"Genius is one percent inspiration, ninety nine percent perspiration."
-- Thomas Alva Edison


       Knowledge should not be strictly limited and determined by how many questions students got right or simply multiple choice. Today's standard grading methods don't always reflect a student's effort or knowledge because they don't show their thought process as to how or why they arrived and concluded their answer. Grades can sometimes be damaging as teachers, parents and students alike use them to either encourage or discourage others. I can't tell you how many times I've been asked what were my SAT and ACT scores. All those times in school I witnessed students laugh and put down someone because they scored higher on a test or an assignment. The parent patting their son or daughter on the back when they see all A's on their report card but never saw their actual work, oblivious to whether or not they cheated, or even bothered to ask what all did they learn. Grades can also be damaging because it teaches students that getting wrong answers or making mistakes are bad, when in fact, making mistakes is a huge part of the learning process. Once big misconception I had when I was younger adults would tell me was that if you fail in school or end up with bad grades, you won't be successful later in life. Don't let an exam determine your fate!


"Students are forced to accept their grades, which, over time, cause students to eventually internalize a life-long view of themselves as “smart,” “average,” or “dumb” — with profound impact on their lives. However, students do not get to officially grade teachers. If a student receives an F, it is assumed the student failed, not the teacher."
-- John Bell
Director of Leadership Development, YouthBuild USA





5.) eBooks
       What if this Universal Virtual School provided the world’s largest library of books that can be accessed by all. These eBooks can be looked at and viewed freely online through this Universal Virtual School. This school would pay the authors to make thier books viewable. It would also provide the option to students to purchase hard-copies if they wanted to and they can be sent to their home address.


       To conclude, this is not necessarily like online classes. Many online classes work great and many are not so great. Online classes can be missed, whereas these are video tutorials which can be accessed any point in time. Also, my idea of this Universal Virtual School is not necessarily created to somehow replace colleges, but to provide people an alternative option. So whatever your reason may be, whether you couldn't afford to attend a university, lack the confidence, or if it's personal - this can help. Something like this will not necessarily work for every major but it will be highly beneficial. An example of such a major is becoming a doctor which you must be present to understand surgeries hands-on but the doctorate program can still utilize this Universal School as far as videos, tests and quizzes. This Universal Virtual School could potentially work in-conjunction with colleges, allowing students to transfer credits over.

       For example, a student who wants to be a doctor can use this Universal Virtual School to save money and get their General Education course requirements out of the way such as English Composition, Math, Science, etc. thus saving them money for when they go into the core work of their doctorate program. Students can use this Universal Virtual School to transfer credits in to earn certain degrees. What makes this unlike most traditional colleges is that you as the student, can work and learn at your own pace and eventually graduate in your own time. It might take you longer than somebody else and that’s OK. Also, if you are interested in learning game design, art and or programming, check out my Resources page. I highly recommend it!




       Redefine how you view education. Its absurd to limit the term 'education' to a person's formal schooling. There is more than one way in this world to be an educated man or woman. So when I refer to creating a universally free education, I am not limiting it to strictly college because then it would be difficult to answer how will teachers get paid. My goal with this blog post was also to show you that there are so many resources out there, many in which you probably have or might not have been even aware of. Thanks for reading and feel free to share your thoughts below.


"Intelligence plus character; that's the goal of a true education."
-- Martin Luther King Jr.


"An educated person is one who has so developed the faculties of their mind that they may acquire anything they want, or its equivalent, without violating the rights of others." 
-- Napoleon Hill



Friday, April 15, 2016

34. Playing Sound Effects



       The video above I came across is the fourth part of a MonoGame tutorial series. It covers all aspects of audio programming, playing music, sound effects, and even creating audio using XACT from the XNA tools. Below I show how I got sound effects working better recently in XNA. Sound is crucial as it helps to further bring the game to life. In XNA, sound is pretty straightforward. Import the sound into your project, just like you did with textures. WAV is the preferred format in most environment, however MP3 should be fine too. Other than the project knowing the existence of the sound, the program must store it too, so you will need to create a SoundEffect object with a suitable name. I will use the name Gear1SoundEffect as an example based of my vehicle physics program. The command should be:

Gear1SoundEffect = content.Load(“gear1”);

Now, to get this to play, you will use a function that is built into the class, conveniently,

Gear1SoundEffect.Play();

This will play the sound effect once and then stop.


Problem
        Although you can overlap sounds without any problems, if you repeatedly play the same effect at a high rate, the sound will be distorted beyond all recognition. Check out the first time I ran into this problem in an older video of mine here as an example. I needed to limit the rate at which the sound effect was being played using timers; especially since the game program is now running at high frame-rates. So if you have been wondering why most my videos thus far don't have any sound, this is the reason as to why.


Solution
       To fix this I created an object called SoundEffectInstance, assigning it the returned object from Gear1SoundEffect.CreateInstance(). After that, all I did was call it’s Play() function. This class has many interesting functions that I suggest you check out on MSDN. Other than Pause(), Stop() and Resume(), an additional function is Apply3D(), which gives it a position in 3D space, taking as arguments one or more listeners and an emitter. This will probably not be of use to you currently, but if you move into 3D XNA programming, it will be of great value.

       Note that you may have up to 16 different SoundEffectInstance object playing simultaneously, this should be more than enough however. Now you can add some life to your game, essentially anything that does not require rotation of images can now be done with what you know.

For more information, check out this article here.

If you found this blog post helpful, please comment below. Thanks for reading and happy coding!



Wednesday, April 13, 2016

33. Basic Car Drifting Physics


       After some trial and error, I was able to get some drifting mechanics working for my vehicle physics. I am continuing to modify and improve the JigLibX Physics Library that I ported over to XNA 4.0 and MonoGame. Inside my Wheel.cs (wheel class), the key was a variable named "smallVel". This variable applies a force between the drive force and the side force. Initially, it was set to three and I decided to change it to 20 for this vehicle. At the moment, it feels like I am driving on ice but it is controllable which makes it a lot of fun to drift. 

Make the following changes so that the car doesn't slide around too much when stopped: 

            float smallVel;
            if (angVel == 0 & !locked)
            {
                smallVel = 1;
            }
            else smallVel = 20;



To give it more of an authentic feel, you can decrease smallVel to tweleve. I also greatly improved the handling. I will most likely create a starter kit based off of this sample program. So if you are using XNA 4.0 or MonoGame and need some on-road, off-road or arcade style car physics, this will help.




Triangle Mesh Collision Problem
       Many developers using JigLibX in XNA had many problems with the Triangle Mesh Object, especially when using it as a ground model but not with the Height Map Object. I sometimes run into these issues. When a physics object hits a vertex or seam between triangles in the mesh, strange things can occur. 




Solution 
       I separated the garage model into multiple meshes; just two for the most part. Using Autodesk Maya, I then selected them all, grouped them and finally exported the selection. You might have to separate models that have a lot of triangles and faces into separate models. JigLibX uses models that support 16-bit indices in size and not 32-bit. Below is a sample of  my JigLibX TriangeMeshObject.cs code that lets you extract the vertices and indices from a loaded model.


using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using JigLibX.Geometry;
using JigLibX.Physics;
using JigLibX.Collision;


namespace JigLibXGame
{
    class TriangleMeshObject : PhysicObject
    {
        TriangleMesh triangleMesh;
        Matrix xform;
        public TriangleMeshObject(Game game, Model model, Matrix orientation, Vector3 position)
            : base(game, model)
        {
            body = new Body();
            collision = new CollisionSkin(null);
            triangleMesh = new TriangleMesh();
            List<Vector3> vertexList = new List<Vector3>();
            List<TriangleVertexIndices> indexList = new List<TriangleVertexIndices>();
            ExtractData(vertexList, indexList, model);
            triangleMesh.CreateMesh(vertexList, indexList, 4, 1.0f);
            collision.AddPrimitive(triangleMesh, new MaterialProperties(0.8f, 0.7f, 0.6f));
            PhysicsSystem.CurrentPhysicsSystem.CollisionSystem.AddCollisionSkin(collision);
            // Transform
            collision.ApplyLocalTransform(new JigLibX.Math.Transform(position, orientation));
            // we also need to move this dummy, so the object is *rendered* at the correct 
               position
            // body.MoveTo(position, orientation);
            this.body.CollisionSkin = this.collision;
          
        }

    
        // Helper Method to get the vertex and index List from the model.
        public void ExtractData(List<Vector3> vertices, List<TriangleVertexIndices> indices, Model model)
        {
            Matrix[] bones_ = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(bones_);
            foreach (ModelMesh mm in model.Meshes)
            {
                Game.GraphicsDevice.RasterizerState = RasterizerState.CullNone;
              
                xform = bones_[mm.ParentBone.Index];
                foreach (ModelMeshPart mmp in mm.MeshParts)
                {
                    int offset = vertices.Count;
                    Vector3[] a = new Vector3[mmp.NumVertices];
                    int vertexStride = mmp.VertexBuffer.VertexDeclaration.VertexStride;
                    mmp.VertexBuffer.GetData<Vector3>(mmp.VertexOffset * vertexStride, a, 0, mmp.NumVertices, vertexStride);
                   
                    for (int i = 0; i != a.Length; ++i)
                        Vector3.Transform(ref a[i], ref xform, out a[i]);
                    vertices.AddRange(a);

                    // new
                    for (int i = 0; i < a.Length; i++) vertices.Add(new Vector3(a[i].X, a[i].Y, a[i].Z));

                    if (mmp.IndexBuffer.IndexElementSize != IndexElementSize.SixteenBits)
                        throw new Exception(
                            String.Format("Model uses 32-bit indices, which are not supported."));
                    short[] s = new short[mmp.PrimitiveCount * 3];
                    
                    mmp.IndexBuffer.GetData<short>(mmp.StartIndex * 2, s, 0, mmp.PrimitiveCount * 3);
                    JigLibX.Geometry.TriangleVertexIndices[] tvi = new JigLibX.Geometry.TriangleVertexIndices[mmp.PrimitiveCount];
                    for (int i = 0; i != tvi.Length; ++i)
                    {
                        tvi[i].I0 = s[i * 3 + 2] + offset;
                        tvi[i].I1 = s[i * 3 + 1] + offset;
                        tvi[i].I2 = s[i * 3 + 0] + offset;
                    }
                    indices.AddRange(tvi);
                }
            }
        }
  
        public override void ApplyEffects(BasicEffect effect)
        {
            //effect.DiffuseColor = Vector3.One * 0.8f;
            
        }
    }
}


       Next I will be making a way where the player can switch to cockpit-view and drive in first-person perspective while in-game. Thanks for reading and I look forward to posting more updates. If you have found this blog post helpful or would like to share any improvements, please comment below.




Tuesday, April 5, 2016

32. MonoGame Video Showcases





       MonoGame is an open-source implementation of the Microsoft XNA 4.0 Framework for creating powerful cross-platform games. Learn more by visiting monogame.net and also fork and star their repository on Github


Tuesday, March 15, 2016

31. MonoGame is coming to Xbox One

     

       XNA is not Dead! It lives on through MonoGame, which implements the XNA 4.0 Framework. This allows developers using the XNA dev kit to port their games to other platforms besides the default platforms supported already by the XNA 4.0 Framework. Recently at GDC (the Game Developer's Conference) 2016, in addition to the cross-network play support, Microsoft announced MonoGame is coming to Xbox One. In an older blog post I wrote, I was asking Microsoft to allow their Xbox One dev kit to support MonoGame as well as other tools and middleware.



       The Cyclone Game Engine is built upon the XNA and MonoGame framework, so this is great news for myself as well as Steel Cyclone Studios. Back in 2014 of March, Sony announced that their PS4 dev kits would support MonoGame to registered PS4 developers. Microsoft officially ended support for XNA some time ago, but MonoGame launched an open source implementation of XNA 4.0 in 2009. Since then MonoGame has been used to help create a number of games for many various platforms, including Windows 8, Windows Phone and others. The first Xbox One game made with MonoGame is called Axiom Verge, which is a retro-themed side-scroller. The game was created by just one person, Tom Happ, who also worked at Petroglyph Games. Axiom Verge launched last year on Playstation 4 and Windows PC, and will be released for Xbox One sometime later this year. Congratulations to the MonoGame team! Below is a video by Blit Works released in 2015 showcasing MonoGame samples running on PSVita and Xbox One.


“With nearly every game on the market today being released simultaneously on all platforms, the need for a good cross-platform development strategy is crucially important. Every hour spent reinventing the wheel is an hour wasted. Or to put it another way, every hour spent writing cross-platform code constitutes three saved hours.” 
       --(Steven Goodwin (2005), Cross-Platform Game Programming)






Friday, March 4, 2016

30. XNA Vehicle Physics Test 4


       In this update, I am continuing to modify and expand the JigLibX (Jiggle Library X) Physics Engine. Recently I added an exhaust particle effect to the vehicle. This particle effect streams behind the vehicle when the player presses the gas so often. I will change the color of the smoke to grey. The standard vehicle class in my game engine has a hand brake that locks the rear wheels, thereby creating some drag and slowing the car down. That part is realistic. What isn't realistic is the rear wheels still have full traction. If you've ever pulled a hand brake in a car while moving you've notice that the rear end loses traction and wants to swing around causing a spin. It can be useful for making tight 180 degree turns, orienting your car coming into a turn and for good 'ol fashioned fun. This is why I kept this feature. 


Adding the smoke particle

Vector3 OffsetAmount = new Vector3(0, 0, -3);

Vector3 OffsetPosition = Car.carObject.Car.Chassis.Body.Position +
    (Car.carObject.Car.Chassis.Body.Orientation.Right * OffsetAmount.Z) +
    (Car.carObject.Car.Chassis.Body.Orientation.Up * OffsetAmount.Y) -
    (Car.carObject.Car.Chassis.Body.Orientation.Forward * OffsetAmount.X);


ParticleComponent.smokeParticles.AddParticle(OffsetPosition, Vector3.Zero);


       OffsetAmount is a Vector3 that is the relative offset you want, in this case 3 units behind the center of the car. OffsetPosition is the final, global, relative position, that you can then pass into your particle system or something. It took some trial and error to get all of these working correctly, but I've tested it in several positions and it seems to work OK. And the last line tells my ParticleComponent to add a particle at the specified position and initial velocity - replace it with your own particle code to get a nice stream of particles behind your car. I plan on adding a similar particle effect to the actual engine when the vehicle takes damage. For example, if the engine is emitting grey smoke, the damage to the vehicle is rather light. But if the smoke is black, the vehicle is heavily damaged and requires repair. The next particle effect I will be working on is adding a dust trail streaming behind the tires.


Change the Gravity 
       One thing I forgot to mention in my previous vehicle physics post was how I changed the gravity. Inside the Physics System Class (PhysicsSystem.cs), scroll down and change the gravity variable from -10 to -30. 

        public PhysicsSystem()  
        {  
            CurrentPhysicsSystem = this;  
            Gravity = -30.0f * Vector3.Up;  
        } 

This will make the car and the physics in general less like the "moon's gravity" and more like the Earth's gravity.


Add Rotational Friction to the Wheels
       Before, if you were on level ground, you could coast forever after letting off the throttle.

     if (driveTorque == 0)
    {
        angVel *= .97f;
    }  

Put this in Update() in the Wheels.cs right above angVel += driveTorque * dt / inertia:

                 if (driveTorque == 0)
                {
                    float axleFriction = 5; // higher number = more rotational friction
                    if (angVel > axleFriction)
                    {
                        if (angVel > 0) angVel -= axleFriction;
                        else angVel += axleFriction;
                    }
                    else angVel = 0;
                }

If you want to add more friction, multiply by a smaller number. For less friction, use a bigger number that's less than 1. Also, to help counteract any bumps on the track, I decreased the wheel's damping fraction.


Improve JigLibX's Performance 
       I have highly modified the Jiggle Physics Library to run on XNA 4.0 and MonoGame. I am extending and improving it in many ways. Many indie game developers chose BEPU rather than JigLibX when developing their games for Xbox 360. One of the major reasons why was the fact that JigLibX's performance on Xbox 360 was extremely slow which decreased the game's frame rate. To help with JigLibX's physics performance on Xbox 360 as well as other less advanced hardware, I am changing its foreach statements to for and I will be adding separate loading threads to help improve the low frame-rate issues. JigLibX also makes extensive use of the "float" object. I mention this because the Xbox 360 has some issues processing floating points. So I am changing out all the floats to avoid major physics performance issues.


How to Get Rid of of the Infamous Wobble at High Speed
       One of the major issues people encountered using JigLibX was once the vehicle got to a certain speed, it started to rock side to side, with no obvious way to stop it. Nikescar came up with a solution to fix this issue. If you would like to see this problem for yourself, adjust the wheelFriction to 5.0f and wheelDampingFrac to 0.3f. The car should start wobbling at max speed.

       Each Update( ) is called at a different time for each wheel. One wheel gets slowed down and the other wheels stay at the current speed. Next, the update from one of the three wheels from the last frame gets slowed down and the remaining wheels go at the same speed. If you do this sixty times a second, you will get quick declaration corners of the car causing the wobbling. After a lot of trial and error, here is how you can fix this.

This problem occurs when the andVel reaches maxAngVel in the Update( ) method of the Wheel.cs:

angVel = MathHelper.Clamp(angVel, -maxAngVel, maxAngVel);

Either delete or comment out this line and it will take care of the problem. However, this creates another problem because now you won't be able to set a limit for the top speed.

In the Wheel.cs, add this with the rest of them:

public float AngVel
        {
            get { return angVel; }

        }


Then just after this under PostPhysics( ) in the Car.cs:

for (int i = 0; i < wheels.Count; i++)
                wheels[i].Update(dt);


add this:

for (int i = 0; i < wheels.Count; i++)
            {
                if (wheels[i].AngVel >= topSpeed)
                    destAccelerate = 0;
            }


This should fix it. Now you can freely setup your car without having to worry about the limitations of the infamous wobble. Lastly, to see how I implemented the skybox, check out my blog post here. Thanks for reading and if anyone has found this blog post helpful, please comment below.


Sunday, January 17, 2016

29. Cross Fade Animation Blending Fixed


     I have fixed the cross fade function in the skeletal XNAnimation Library for XNA 4.0 & MonoGame. Cross Fade interpolates between two animation clips, fading out the current animation clip and fading in a new one. This allows the character's animations to transition more smoothly when their actions change. Animations can be played forward and backward. The animation speed can also be controlled and looped. The XNAnimation library supports models up to 80 bones and a .xml file can be used to split animation into a new set of animations based on time or keyframes. I am currently working on another technique called Additve Blending. The sample source code for cross fade animation blending is below. 


using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using XNAnimation;
using XNAnimation.Controllers;
using XNAnimation.Effects;

namespace CrossFadeBlendSample
{
    ///
    /// This sample illustrates how to use an animation controller
    ///
    public class XNAnimationSample : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;

        ///
        /// Graphics 2D stuff
        ///
        SpriteBatch spriteBatch;
        SpriteFont spriteFont;

        ///
        /// View and Projection camera matrices
        ///
        Matrix[] absoluteBoneTransforms;
        Matrix cameraView;
        Matrix cameraProjection;

        ///
        /// Stores the last keyboard state
        ///
        KeyboardState lastKeyboradState;

        ///
        /// The SkinnedModel class handles skeletal animated models
        ///
        SkinnedModel skinnedModel;

        ///
        /// Index of the active skinned model being drawed
        ///
        int activeSkinnedModelIndex = 0;

        ///
        /// An array with all the skinned model files available
        ///
        readonly String[] skinnedModelFiles = { "Models/PlayerMarine", "Models/EnemyBeast"};

        ///
        /// Custom world transformation for each model
        ///
        readonly Matrix[] worldTransformations = {Matrix.CreateTranslation(0, -12, 0),
            Matrix.Identity, Matrix.Identity};

        ///
        /// The AnimationController class handles the animation playback
        ///
        AnimationController animationController;

        ///
        /// Index of the active animation being played
        ///
        int activeAnimationClipIndex = 0;

        ///
        /// If true, enable the keyframe interpolation
        ///
        bool enableInterpolation = false;

        ///
        /// Copyright messages that appears when each model is draw
        ///
        readonly String[] copyrightMessages = {
            "Model courtesy of FloatBox Studios",
            "Model courtesy of Psionic",
            "DirectX SDK Sample Model"};

        public XNAnimationSample()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferMultiSampling = true;

            Content.RootDirectory = "Content";
        }

        private void LoadSkinnedModel()
        {
            // Loads an animated model
            skinnedModel = Content.Load<SkinnedModel>(skinnedModelFiles[activeSkinnedModelIndex]);

            // Copy the absolute transformation of each node
            absoluteBoneTransforms = new Matrix[skinnedModel.Model.Bones.Count];
            skinnedModel.Model.CopyBoneTransformsTo(absoluteBoneTransforms);

            // Creates an animation controller
            animationController = new AnimationController(skinnedModel.SkeletonBones);

            // Start the first animation stored in the AnimationClips dictionary
            animationController.StartClip(
                skinnedModel.AnimationClips.Values[activeAnimationClipIndex]);
        }

        protected override void LoadContent()
        {
            // Create a SpriteBatch and a SpritFont for 2D drawing
            spriteBatch = new SpriteBatch(GraphicsDevice);
            spriteFont = Content.Load<SpriteFont>("Fonts/Arial");

            // Setup camera
            cameraView = Matrix.CreateLookAt(new Vector3(10, 15, 30), new Vector3(0, 12, 0), Vector3.Up);
            cameraProjection = Matrix.CreatePerspectiveFieldOfView(1, 1, 1, 1000);

            // Load a skinned model
            activeSkinnedModelIndex = 0;
            activeAnimationClipIndex = 0;

            LoadSkinnedModel();
        }


        protected override void Update(GameTime gameTime)
        {
            #region Keyboard Handling
            KeyboardState keyboradState = Keyboard.GetState();

            // Exit
            if (keyboradState.IsKeyDown(Keys.Escape))
                Exit();

            // 'Up' and 'Down' changes the animation speed
            if (keyboradState.IsKeyDown(Keys.Up))
            {
                animationController.Speed += 0.005f;
            }
            else if (keyboradState.IsKeyDown(Keys.Down))
            {
                animationController.Speed = (animationController.Speed < 0.1f) ?
                    0.1f : animationController.Speed - 0.005f;
            }

            // 'Left' and 'Right loop through the animations
            if (keyboradState.IsKeyDown(Keys.Left) && lastKeyboradState.IsKeyUp(Keys.Left))
            {
                activeAnimationClipIndex = ((activeAnimationClipIndex - 1) < 0) ?
                    skinnedModel.AnimationClips.Count - 1 : activeAnimationClipIndex - 1;
                animationController.CrossFade(skinnedModel.AnimationClips.Values[activeAnimationClipIndex],
                    TimeSpan.FromMilliseconds(300));
            }
            else if (keyboradState.IsKeyDown(Keys.Right) && lastKeyboradState.IsKeyUp(Keys.Right))
            {
                activeAnimationClipIndex = (activeAnimationClipIndex + 1) % skinnedModel.AnimationClips.Count;
                animationController.CrossFade(skinnedModel.AnimationClips.Values[activeAnimationClipIndex],
                    TimeSpan.FromMilliseconds(300));
            }

            // Use CAPSLOCK to cross fade through the animations
            if (keyboradState.IsKeyDown(Keys.CapsLock) && lastKeyboradState.IsKeyUp(Keys.CapsLock))
            {
                activeAnimationClipIndex = (activeAnimationClipIndex + 1) % skinnedModel.AnimationClips.Count;
                animationController.CrossFade(skinnedModel.AnimationClips.Values[activeAnimationClipIndex],
                    System.TimeSpan.FromMilliseconds(300));
            }

            // 'Enter' enable/disable animation interpolation
            if (keyboradState.IsKeyDown(Keys.Enter) && lastKeyboradState.IsKeyUp(Keys.Enter))
            {
                enableInterpolation = !enableInterpolation;

                if (enableInterpolation)
                {
                    animationController.TranslationInterpolation = InterpolationMode.Linear;
                    animationController.OrientationInterpolation = InterpolationMode.Spherical;
                    animationController.ScaleInterpolation = InterpolationMode.Linear;
                }
                else
                {
                    animationController.TranslationInterpolation = InterpolationMode.None;
                    animationController.OrientationInterpolation = InterpolationMode.None;
                    animationController.ScaleInterpolation = InterpolationMode.None;
                }
            }

            // 'Tab' changes the current animated model
            if (keyboradState.IsKeyDown(Keys.Tab) && lastKeyboradState.IsKeyUp(Keys.Tab))
            {
                activeSkinnedModelIndex = (activeSkinnedModelIndex + 1) % skinnedModelFiles.Length;
                activeAnimationClipIndex = 0;
                LoadSkinnedModel();
            }

            lastKeyboradState = keyboradState;

            #endregion

            // Update the animation according to the elapsed time
            animationController.Update(gameTime.ElapsedGameTime, Matrix.Identity);

            base.Update(gameTime);
        }


        protected override void Draw(GameTime gameTime)
        {
         
            graphics.GraphicsDevice.Clear(Color.Gray);
            graphics.GraphicsDevice.DepthStencilState = DepthStencilState.Default;
            graphics.GraphicsDevice.RasterizerState = RasterizerState.CullNone;
            graphics.GraphicsDevice.BlendState = BlendState.Opaque;

            // In this first version of the library the animated model is draw
            // through an internal Model object
            foreach (ModelMesh modelMesh in skinnedModel.Model.Meshes)
            {
                foreach (SkinnedEffect effect in modelMesh.Effects)
                {
                    // Set the animated bones to the model
                    effect.SetBoneTransforms(animationController.SkinnedBoneTransforms);

                    // OPTIONAL - Configure Material
                    effect.DiffuseColor = new Vector3(0.8f);
                    effect.SpecularColor = new Vector3(0.3f);
                    effect.SpecularPower = 8;

                    // OPTIONAL - Configure lights
                    effect.EnableDefaultLighting();
                    effect.AmbientLightColor = new Vector3(0.1f);
                    effect.PreferPerPixelLighting = true;

                    // Setup camera properties
                    effect.View = cameraView;
                    effect.Projection = cameraProjection;
                }

                // Draw a model mesh
                modelMesh.Draw();
            }
         
            base.Draw(gameTime);

            Draw2D();
        }

        #region Draw 2D Stuff
        private void Draw2D()
        {
            spriteBatch.Begin();

            spriteBatch.DrawString(spriteFont, "Use [LEFT/RIGHT] to Cross Fade Blend with a new animation clip.",
                new Vector2(10, 5), Color.White);
            spriteBatch.DrawString(spriteFont, "Use [UP/DOWN] to change the animation speed.",
                new Vector2(10, 22), Color.White);
            spriteBatch.DrawString(spriteFont, "Use [ENTER] to enable/disable animation interpolation.",
                new Vector2(10, 39), Color.White);
            spriteBatch.DrawString(spriteFont, "Use [TAB] to change the animated model.",
                new Vector2(10, 56), Color.White);

            spriteBatch.DrawString(spriteFont, copyrightMessages[activeSkinnedModelIndex],
                new Vector2(560, 580), Color.White);
            spriteBatch.End();

        }
        #endregion

        #region Entry Point
        static void Main(string[] args)
        {
            using (XNAnimationSample game = new XNAnimationSample())
            {
                game.Run();
            }
        }
        #endregion
    }
}