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
Kodak EasyShare 7.0MP Digital Camera ReviewIf you are looking for an amazing digital camera, without having to spend a ton of money, than this is the camera for you.- How a Camera Works and What it Really IsEverybody takes pictures -- but what's really going on when you pick up your camera and snap off a shot?
- How to Buy a Digital Camera when You Know Nothing About ThemIt's definitely the digital age! Considering a digital camera but don't feel like you know enough about them to make an educated decision? This article will help you with all you need to know!
- Ten Reasons Why You Should Not Buy a Film CameraEasy way to learn many different things about a film loading camera, and reasons why it's a better choice to not purchase one.
Top Camera Bags Under $20This is a simple article highlighting five popular camera bags for small digital camera. Each camera bag mentioned costs less than $20 and can be found at one of the shops ment...
- Galaxy Cluster Collision Proves Existence of Dark Matter
- The Big Three for 2006
- Canon Powershot A510 Digital Camera
- Review of the Pentax Optio WPi Digital Camera
- Review of the Nikon D2X Digital Camera
- How to Take Better Camera Phone Pictures
- Airport Security: Port Authority Upset with Raytheon's Detection System
