DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Data
  4. Drawing 3D Helper Axes in XNA

Drawing 3D Helper Axes in XNA

Denzel D. user avatar by
Denzel D.
·
Nov. 16, 12 · Interview
Like (0)
Save
Tweet
Share
5.88K Views

Join the DZone community and get the full member experience.

Join For Free

I started with XNA a while ago, and while working on my 3D game development skills, I realized that sometimes it is sometimes useful to see where your objects are located. For this purpose, there are the XYZ axes. There is no built-in way to display helper axes, so I decided to design my own.

To get started, I created two matrices, that are the view matrix and the projection matrix. The view matrix defines the position of the camera, that will look at the world, while the projection matrix will determine the way it looks at it.

Matrix viewMatrix;
Matrix projectionMatrix; 

Here is a function that will set the proper location values for the matrices:

private void CameraInit()
{
viewMatrix = Matrix.CreateLookAt(new Vector3(positionX, positionY, positionZ), new Vector3(0, 0, 0), new Vector3(0, 1, 0));

projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.Viewport.AspectRatio, 0.1f, 100.0f);
} 

Notice that the first Vector3, that defines the camera position, gets the x, y and z values from three different variables. I use this to later on modify these values whenever the camera moves. Here is the declaration (also should be position in the class header):

int positionX = 30;
int positionY = 0;
int positionZ = 30; 

The values can be easily adjusted depending on the object that is being rendered, so that it fits in the camera “viewfinder”. Now, I needed to draw the lines that will represent the axes. First of all, I defined an array of VertexPositionColor:

VertexPositionColor[] lines;

To set the values for the reference points, I created a separate function that determines the size of the vertices:

protected void SetLineVerticles()
{

lines = new VertexPositionColor[6];

lines[0] = new VertexPositionColor(new Vector3(-10, 0, 0), Color.Red);

lines[1] = new VertexPositionColor(new Vector3(10,0,0), Color.Red);

lines[2] = new VertexPositionColor(new Vector3(0, -10, 0), Color.Green);

lines[3] = new VertexPositionColor(new Vector3(0,10,0), Color.Green);

lines[4] = new VertexPositionColor(new Vector3(0, 0, -10), Color.Blue);

lines[5] = new VertexPositionColor(new Vector3(0,0,10), Color.Blue);
} 

Every two VertexPositionColor instances define two points that will be used to draw  a line as well as the line color. It is important to make these lines intersect in a single point and make sure that those are perpendicular to each other. Opposite values on the same axis will make this possible (see the correspondence between values – for the X axis it is –10 and 10, same applies to the Y and Z axes.

To draw the axes, I used the this snippet:

protected override void Draw(GameTime gameTime)
{

device.Clear(Color.White);

BasicEffect effect = new BasicEffect(device,null);

effect.World = Matrix.Identity;

effect.View = viewMatrix;

effect.Projection = projectionMatrix;

effect.VertexColorEnabled = true;

effect.Begin();

foreach (EffectPass pass in effect.CurrentTechnique.Passes)

{  

pass.Begin();

device.VertexDeclaration = vertexDeclaration;

device.DrawUserPrimitives(PrimitiveType.LineList, lines, 0, 3);

pass.End();

}

effect.End();

base.Draw(gameTime);

} 

It is the regular Draw function with primitive drawing code inside. Notice that I have a undeclared element here: vertexDeclaration. It is what it is named – a VertexDeclaration instance:

VertexDeclaration vertexDeclaration;

Another interesting part of the above code is effect.VertexColorEnabled = true; This part enables the colors for the drawn primitives. If I will not enable this, all my lines will be black, although I explicitly set the colors for them.

If you run the game now, you will see a set of lines drawn as seen from the front. What if I wanted to see those from an angled perspective? I added the code below to the Update function, so that when the up key is pressed, the Y position of the camera is shifted, therefore I can clearly see the axes.

if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Up))

{

positionY++;

CameraInit();

} 
Now, you can easily place other objects in this space and you can visually track where those are located.If you enjoy watching video tutorials, you might want to look at this on YouTube.

 

Matrix (protocol) Object (computer science) Design Snippet (programming) Data structure Space (architecture) Element

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The 12 Biggest Android App Development Trends in 2023
  • Utilize OpenAI API to Extract Information From PDF Files
  • 2023 Software Testing Trends: A Look Ahead at the Industry's Future
  • Better Performance and Security by Monitoring Logs, Metrics, and More

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: