Saturday, January 17, 2015

18: Vehicle Physics Tests

Problem: JigLibX (Jiggle Library X) hasn't been updated
       The major issue with JigLibX is that it hasn't had an update since XNA 4.0 came out, so the version downloaded from the codeplex website wouldn't work with the project I have. The project I downloaded still didn't work. Visual Studio was having problems with converting the project, so I had to edit the file JigLibX Xbox 360.csproj using a text editor (open with -> notepad, for example.) Below is how I solved this problem.
Solution: How to Port JigLibX to XNA 4.0 
1.) Get the latest from JigLibX's SVN

2.) The first line to look for, found near the top, is this:


Simply change the v2.0 to v4.0.

3.) Next, 
find these two lines, found close to the bottom:

<Import Project="$(MSBuildExtensionsPath)\Microsoft\XNA Game Studio\v2.0\
   Microsoft.Xna.GameStudio.Common.targets" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\XNA Game Studio\v2.0\
   Microsoft.Xna.GameStudio.NestedContent.targets" />

You can either remove them, or comment them out (I chose to just comment them out). I was unable to find anything similar from my XNA 4.0 install, so I commented them out and everything seems to work just fine. [*Note, to comment them out, simply surround the lines with the opening tag "<!--" and the closing tag "-->" excluding the quotes, of course]

4.) If you run into any problems, feel free to download my XNA 4.0 port of JigLibX on my IndieDB page. Also, I was able to get JigLibX running on Xbox 360 but there were some performance issues. 

How to Add the Chase Camera in JigLibX
       In my first vehicle test, I used JigLibX 4.0 (XNA 4.0 port of JigLibX) for the physics. I added a Chase Camera to follow the vehicle while driving. You can download source code of the chase camera sample here. Chase cameras are great for creating a feeling of realistic motion. As the car accelerates and brakes, it changes distance from the camera. The problem I ran into is when the vehicle accelerates at very high speeds, the chase camera tends to get too far behind the vehicle object. I found that simply reducing the springiness of the camera won't work. To fix this problem, my solution is to add an extra Z-Stiffness property. This will control how much the camera tries to keep up with the moving vehicle object. You probably first noticed that the rotation of the wheels are off greatly. 

       In the Update method, add the following:
                    camera.ChasePosition = Car.carObject.Car.Chassis.Body.Position; 

                    camera.ChaseDirection = Car.carObject.Car.Chassis.Body.Orientation.Right;  

                    camera.Up = Vector3.Up;

       For my second test with vehicle physics, I used the BEPU Physics Engine. In the following video above, I was able to add one of the custom monster trucks I modeled to the engine. To do this, I declared a model variable for the vehicle's body at the top like so: 

Model car; // This will be the vehicle's body

Under the following line:

vehicle = new Vehicle(body);


vehicle.Body.Tag = car;

Then Load the car model: 

car = Content.Load<Model>("car");

Then in your Draw Method, draw the car model there. 

       In this next vehicle test, I continued with JigLibX 4.0 for the physics. Notice the rotation of the wheels are fixed. At first, I thought this was due to the coordinates in which the wheel model was placed and exported in 3D space from Autodesk Maya . The wheel model in Maya has to be centered at the origin. The program passes the wheel model through an array to create copies for the other wheels of the vehicle. The problem was the model itself due to the amount if detail it has. So I placed a low-poly wheel inside of it to be used as a guide to get the wheel rotating correctly. I forgot to render it transparent so its not visible to the player.

       The car object class inside the vehicle folder does not actually use spheres for the wheels. Instead it uses some number of rays and casts out the wheel direction to detect the ground. I have greatly improved the controls along with the overall handling of the vehicle after a few adjustments. I found great help from a tutorial in the JigLibX forums by nikescar. Check out nikescar's Vehicle Class Modifications for more information. If you run into any troubles with the link, I have posted nikescar's modifications here as well. I also added a nifty power sliding effect and you can take tight turns by letting off the gas some.

Improving Performance in JigLibX
       As of now, it runs a little slow on Xbox 360 and I am optimizing it. To help with JigLibX's physics performance on older hardware such as Xbox 360, I am changing my foreach statements to for and I will be adding separate loading threads to help improve the frame-rate issues. I will continue to research and find other ways to optimize and improve the performance in JigLibX.

       In this next vehicle physics test above, I was testing how well the model handles on a static mesh composed of many triangles. The 3D terrain model is composed of many hills; making this test pretty fun when driving over them at high speeds. The vehicle also did a complete 360 barrel roll in mid air while falling as I drove off a hill near the end. This randomly occurred, so it was a good thing I was able to capture the moment on video. If you have found this post helpful, please post a comment in the comment section. After looking through other 3D open-source XNA physics engines such as Jitter and BEPU Physics, I felt that JigLibX was the best fit for what I needed. It was a personal preference since I particularly liked the component style of framework. It meshes well with XNA. There are several other implementation styles and core mechanics about it that I liked, so it was selected to be the physics for the Cyclone Game Engine.

Monster Truck 3D Model Work-In-Progress

Below are screenshots of vehicles I plan on creating physics for.

Laser Tank Car

Right now, I have the custom laser tank car I modeled working. I just have to animate the tracks which will take some time. The model didn't fully export correctly but I found out the problem. Overall I was glad to see it in-game. 

Here is a side view of the custom laser tank car. 

I am also working on driving vehicles in first-person perspective. 

I am figuring out out how the camera will be positioned when driving in first-person and animating the certain individual parts of the vehicle. 


I am also working on water physics so that soon, the player can drive water-based vehicles like the one I modeled in this image above. 


I have flight working for the most part but I am working on how flight for a helicopter or gyro-copter will function.  

No comments:

Post a Comment