5,693,062 members and growing! (19,386 online)
Email Password   helpLost your password?
Multimedia » General Graphics » General     Intermediate

Managed DirectX Tutorials: Part 8 - Exploring the Terrain with Basic Input

By James Gupta

Great, we have terrain, but now, in a similar fashion to Dr. Livingstone we must explore this vast terrain
C#, Windows, .NET 2.0, .NET, DirectX, Visual Studio, VS2005, Dev

Posted: 26 May 2006
Updated: 26 May 2006
Views: 12,791
Bookmarked: 20 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
3 votes for this Article.
Popularity: 2.18 Rating: 4.57 out of 5
0 votes, 0.0%
1
1 vote, 33.3%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
2 votes, 66.7%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Sample Image - MDXTut_8.jpg

Rotating Terrain with Basic Matrices Input

Now, we will learn to perform basic input commands on our sample so that we can traverse the island from a "god" camera.
This is only basic matrices input. As I personally get better at this I will add more / update this tutorial.

Short & Sweet

As we learnt a lot about matrices in the earlier tutorials, we can now add input relatively painlessly.
Add the following variables to your form class:

public const float rotateSpeed = 2;
public float Zoom = 0.0f;
public float Alpha = 0;
public float Beta = 0;
public float Charlie = 10;

The first one of these is a modifier which will define how fast we rotate / zoom our terrain. As this is a sample as opposed to a game, we do not need to time it as nothing needs to be balanced. However, it is nice to have a simple member to change to modify the speed.

The next is used, as the name suggests to zoom around the landscape.

The other three are used to rotate the world along its X, Y and Z axes.

Now, at the top of the Paint event handler, add the following lines:

device.Transform.View = Matrix.LookAtLH ( new Vector3 ( 0, Zoom, 150 ), 
               new Vector3 ( 0, -5, 0 ), new Vector3 ( 0, 1, 0 ) );
device.Transform.World = Matrix.Translation ( -256 / 2, -256/ 2, 0 )* 
      Matrix.RotationX ( Alpha ) * Matrix.RotationY ( Beta ) *
      Matrix.RotationZ ( Charlie ); 

As you can see, we change the View (camera position)'s Y axis (for different results, try moving the Zoom member to affect different axis).
And then we multiply our world transformation by the rotation values.

Try fiddling around with these to learn more about matrices, putting what we learnt in previous tutorials into practice.

The final part of this short tutorial is to make a system to actually change the values. We will wire up the KeyDown event, for reasons explained in the Base Tutorial (3).

The event simply checks the keys we want, and performs the event needed:

private void Form1_KeyDown ( object sender, KeyEventArgs e )
{
switch (e.KeyData)
{
case Keys.A:
{
Charlie += 0.01f * rotateSpeed;
} break;
case Keys.D:
{
Charlie -= 0.01f * rotateSpeed;
} break;
case Keys.W:
{
Alpha += 0.01f * rotateSpeed;
} break;
case Keys.S:
{
Alpha -= 0.01f * rotateSpeed;
} break;
case Keys.Left:
{
Beta += 0.01f * rotateSpeed;
} break;
case Keys.Right:
{
Beta -= 0.01f * rotateSpeed;
} break;
case Keys.Up:
{
Zoom += 1f * rotateSpeed;
} break;
case Keys.Down:
{
Zoom -= 1f * rotateSpeed;
} break;
}
} 


DONT FORGET TO WIRE THE EVENT HANDLER IN THE FORMS CONSTRUCTOR:


this.KeyDown += new KeyEventHandler(Form1_KeyDown);

As you can see, we can easily change the speed simply by changing the value assigned to the constant member rotateSpeed, making our sample easily adjustable.

And thats it :) You now have a rotatable, zoomable landscape. This tutorial was just to give you a little "nudge" into matrices manipulation. I reccomend you play with the variables yourself.
In a future tutorial we may look into a more robust input system.

Feedback

I am always open for answering questions, whether through MSN (jamespraveen@aol.com), email (james@magclan.cwhnetworks.com) or through the message board attatched to this article.
If you have a problem with any of my samples / my topics in general please feel free to ask me.
Or you can post on my forums at http//www.just-code-it.net

History

26/05/06: Posted on CodeProject


Previous Articles

Part 1: Setting Up DirectX
Part 2: Initialising Direct3D
Part 3: Rendering Primitives
Part 4: The Transformation Pipeline
Part 5: Basic Texturing
Part 6: Batch Processing Primitives
Part 7: Using Heightmaps
Part 8: Exploring Terrain with Input

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

James Gupta


I live in England, UK in a small town which only just got broadband...

In my spare time, I run a small website design, hosting and maintenance business at www.jamesgupta.com
Occupation: Web Developer
Location: United States United States

Other popular General Graphics articles:

  • A flexible charting library for .NET
    Looking for a way to draw 2D line graphs with C#? Here's yet another charting class library with a high degree of configurability, that is also easy to use.
  • CxImage
    CxImage is a C++ class to load, save, display, transform BMP, JPEG, GIF, PNG, TIFF, MNG, ICO, PCX, TGA, WMF, WBMP, JBG, J2K images.
  • 3D Pie Chart
    A class library for drawing 3D pie charts.
  • Really cool visual FX
    A set of classes for doing stunning visual effects, including water, plasma and fire.
  • ImageStone
    An article on a library for image manipulation.
Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 1 of 1 (Total in Forum: 1) (Refresh)FirstPrevNext
GeneralThe BestmemberGeorgi Petrov3:58 31 Jul '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 26 May 2006
Editor:
Copyright 2006 by James Gupta
Everything else Copyright © CodeProject, 1999-2008
Web08 | Advertise on the Code Project