Csharp.Net Tutorial: Basic Ray Collision Detection with Truevision 3D

James Cloud
There probably was never made a video game that did not use some sort of collision detection. Collision detection is used to stop objects from going through walls and for detecting many other conditions in a game. A prime example of collision detection is in a first person shooter game when the player comes in contact with a wall or object. If there was not any collision detection code in place the player would just walk right through these objects.

This tutorial will show you an easy to use method of detecting collision with the camera and a sample object. You can use these basic principles to incorporate a fully functioning ray collision system into your project.

Step One - Defining the Camera and Sample Object

If you have not defined your camera object at this point, then add this definition to your project,

TVCamera Camera = new TVCamera();

The next step is to define the sample object like so,

TVMesh objTest;

Finally you will need to make a collision result holder using the TV_COLLISIONRESULT object,

TV_COLLISIONRESULT Collision_Result;

With the proper declarations in place, proceed to your games loading function and insert in this source code,

objTest = Scene.CreateMeshBuilder(); // Add the test object to the scene
objTest.LoadXFile(Application.StartupPath + "\\Test.x", true); // Load a model into the test object
objTest.SetCollisionEnable(true); //Enable Collision detection on the test object

Make sure to replace the LoadXFile function parameter with the path to a test object.

Step Two - Test for Collisions and Computing Results

In your project's main loop it is necessary to make two 3d vectors in order to store the camera's movement variables.

TV_3DVECTOR Collision_Start, Collision_End;

Before you call the movement code for the camera object, you will need to store the camera's position in the first vector,

Collision_Start = Camera.GetPosition();

Now execute the camera object's movement code and then use this code to store the camera's ending or new position,

Collision_End = Camera.GetPosition();

With the camera's beginning and ending positions stored we can now check for a ray collision. Here is the source code for ray collision detection,

// Checking for collision
if (objTest.AdvancedCollision(Collision_Start, Collision_End, ref Collision_Result))
{
Camera.SetPosition(Collision_Start.x, Collision_Start.y, Collision_Start.z); // Collision detected, place the camera back at the starting vector
}
else
{
Camera.SetPosition(Collision_End.x, Collision_End.y, Collision_End.z); // No Collision detected, place the camera at the ending vector
}

You will now have fully working collision detection with the camera and the test object.

These ray collision techniques can easily be used with entire maps and other object types, just follow the basic guide lines listed above and replace where needed.

Published by James Cloud

I like to program and do basically anything that has to do with technology and computers.  View profile

To comment, please sign in to your Yahoo! account, or sign up for a new account.