August 17, 2017
Hot Topics:

# Creating an Interactive 3D world with C# and DirectX

### 6. Define Object Boundaries for Movement and Collision Detection

In this scene, only the camera or the viewer's eye is allowed to move and no other object in the scene is moving. This also can be done just by adding a little more code. But, for the sake of simplicity, I am moving only the camera. I have made another method, Check_Movement, that takes care of all the movements:

```private void Check_Movement()
{
// Simple check of the mouse.
if(sngAngleX > 1.3f)
{
sngAngleX = 1.3f;
}
if(sngAngleX < -1.3)
{
sngAngleX = -1.3f;
}
// Update the vectors using the angles and positions.
sngPositionX = sngPositionX + (float)(System.Math.Cos((double)sngAngleY)
* sngWalk / 5.0f * TimeElapsed)
+ (float)(System.MathCos((double)sngAngleY + 3.141596/2)
* sngStrafe / 5.0f * TimeElapsed);
sngPositionZ = sngPositionZ + (float)(System.Math.Sin((double)sngAngleY)
* sngWalk / 5.0f * TimeElapsed)
+ (float)(System.Math.Sin((double)sngAngleY + 3.141596/2)
* sngStrafe / 5.0f * TimeElapsed);
// We update the look at position.
snglookatX = sngPositionX + (float)System.Math.Cos((double)sngAngleY);
snglookatY = sngPositionY + (float)System.Math.Tan((double)sngAngleX);
snglookatZ = sngPositionZ + (float)System.Math.Sin((double)sngAngleY);
// With the new values of the camera vectors (position and
// look at), we update the scene's camera.
TVScene.SetCamera(sngPositionX, sngPositionY, sngPositionZ, snglookatX,
snglookatY, snglookatZ);
}
```

Now it's time to enable Collision Detection. Because the demo application is very simple and does not require any collision detection, I have not added it in the code sample.

`TVMesh.SetCollisionEnable(Collision As Boolean, Group As Integer)`

Disables/Enables collision test for the entire mesh or a group of this mesh. Collision test is True by default on all new created/loaded meshes. Don't hesitate to disable collisions when it's not useful because it will free the CPU from some computations.

`TVCollisionResult.GetCollisionMesh()`

Returns the mesh that has been collided/mousepicked if the object type is Mesh. If you're not sure that a mesh has been collided, always a test for the object type.

```TVScene.AdvancedCollision(linestart As D3DVECTOR,
lineend As D3DVECTOR,
ret_CollisionResult As TV_COLLISIONRESULT,
objecttocheck As CONST_TV_OBJECT_TYPE,
TestType As CONST_TV_TESTTYPE,
findclosest As Boolean)
```

Do an advanced collision test on landscape, mesh, and Actor entities. This is basically a ray collision test that returns the closest hit between the line start and line end (or the first hit found if you specify so). The information returned by this function can be used to do more physics computations about the collision and do sliding or bouncing. To get the maximal accuracy for the returned information, you should use the TV_TESTTYPE_ACCURATETESTING checking mode.

```TVMesh.Collision (linestart As D3DVECTOR,
lineend As D3DVECTOR,
CheckType As CONST_TV_TESTTYPE,
Ret_Distance As Single)
```

Does a simple a collision test on this mesh. This is basically a ray collision test that returns the closest hit between the line start and line end (or the first hit found if you specify so).

 linestart Start point of the line test lineend End point of the line test CheckType Defines the type of test that will be done for collision detection. Bounding box or bounding sphere are the fastest ones, but gives less accurate information. Ret_Distance (Optional, value = 0) Returns the distance from t
```//This is for collision detection
if (Room.Collision(ref linestart, ref lineend,
CONST_TV_TESTTYPE.TV_TESTTYPE_BOUNDINGBOX,
ref distance)== true)
{
//Do not move camera
//Add code here for setting the camera's position. It's not done.
}
```

Figure 12: Moving camera through the scene with mouse and keyboard input

### 7. Applying the Laws of Physics

Real-time physics laws—including gravity, collision, and topography—are applied in movements such as that of a plane, ball, car, and so forth. Because in this scene, this is not required, I have skipped this step in this application. I will be applying it in my next tutorials. For example, if you want to check the gravity in the scene, you can do so this way:

```TVScene.CheckGravity(TVCamera() As CameraGravity,
TimeElapsed As Single,
ValueDown As Single, ValueUp As Single,
landscape As Boolean)
```

Check Gravity is a function that checks the camera position movement on Collision and Gravity.

Page 6 of 7

Comment and Contribute

(Maximum characters: 1200). You have characters left.

## Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

## Most Popular Developer Stories

Thanks for your registration, follow us on our social networks to keep up-to-date