Saturday, January 17, 2015

18: Vehicle Physics Tests

       In my first vehicle test, I used JigLibX 4.0 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. 

How to Add the Chase Camera
       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. 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.

       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.

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