Skip to content
Joshua Slack edited this page Sep 20, 2017 · 10 revisions

Running some examples

This article assumes you have read and completed the steps in [[ That's it! Now head over to Your First Steps and run some examples.]]
(If you use NetBeans you should still be able to follow. )

Open up the project called ardor3d-examples:

There are several ways in Eclipse to view your project files.

I strongly suggest to use the “Package Explorer”.
If you dont already have it you can open it using “Window → Show View → Other…” and filter for “package explorer”.

You should see the following list of projects:

  • ardor3d-animation
  • ardor3d-awt
  • ardor3d-core
  • ardor3d-collada
  • ardor3d-effects
  • ardor3d-examples
  • ardor3d-extras
  • ardor3d-math
  • ardor3d-savable
  • ardor3d-jogl
  • ardor3d-lwjgl
  • ardor3d-swt
  • ardor3d-ui

Open up the one called ardor3d-examples
You should see the following nodes in the project:

  • src/main/java
  • src/main/resources
  • JRE System Library [JavaSE-1.6 ( or similar )]
  • src
  • ardor3d-examples.iml
  • assembly.xml
  • nbactions.xml
  • pom.xml

src/main/java contains all the Java code for the examples.
src/main/resources contains all the textures (images) and models (collada and obj format) for the examples.

  1. Open up the node src/main/java
  2. Open up the package com.ardor3d.example
  3. You should see a Java file called ExampleRunner.java
  4. Right click on it and select “Run as → Java Application”

This is should open up a helpful Window listing all the available examples. This is the best way to get a glimpse of what Ardor3D is capable of and a great source of inspiration for your own projects.

Note: that you can move around in the 3D view using First Person Shooter style controls:

  • W and S moves forward and backward.
  • A and D moves side ways.
  • click and drag the mouse to turn the camera.

Let's look at some code

Let's take a look at the source code of BoxExample. Its source code is located in the package: com.ardor3d.example.basic The file is called BoxExample.java

public class BoxExample extends ExampleBase {
 
    /** Keep a reference to the box to be able to rotate it each frame. */
    private Mesh box;
 
    /** Rotation matrix for the spinning box. */
    private final Matrix3 rotate = new Matrix3();
 
    /** Angle of rotation for the box. */
    private double angle = 0;
 
    /** Axis to rotate the box around. */
    private final Vector3 axis = new Vector3(1, 1, 0.5f).normalizeLocal();
 
    public static void main(final String[] args) {
        start(BoxExample.class);
    }
 
    @Override
    protected void updateExample(final ReadOnlyTimer timer) {
        // Update the angle using the current tpf to rotate at a constant speed.
        angle += timer.getTimePerFrame() * 50;
        // Wrap the angle to keep it inside 0-360 range
        angle %= 360;
 
        // Update the rotation matrix using the angle and rotation axis.
        rotate.fromAngleNormalAxis(angle * MathUtils.DEG_TO_RAD, axis);
        // Update the box rotation using the rotation matrix.
        box.setRotation(rotate);
    }
 
    @Override
    protected void initExample() {
        _canvas.setTitle("Box Example");
 
        // Create a new box centered at (0,0,0) with width/height/depth of size 10.
        box = new Box("Box", new Vector3(0, 0, 0), 5, 5, 5);
        // Set a bounding box for frustum culling.
        box.setModelBound(new BoundingBox());
        // Move the box out from the camera 15 units.
        box.setTranslation(new Vector3(0, 0, -15));
        // Give the box some nice colors.
        box.setRandomColors();
        // Attach the box to the scenegraph root.
        _root.attachChild(box);
 
        // Add a texture to the box.
        final TextureState ts = new TextureState();
        ts.setTexture(TextureManager.load("images/ardor3d_white_256.jpg", Texture.MinificationFilter.Trilinear, true));
        box.setRenderState(ts);
 
        // Add a material to the box, to show both vertex color and lighting/shading.
        final MaterialState ms = new MaterialState();
        ms.setColorMaterial(ColorMaterial.Diffuse);
        box.setRenderState(ms);
    }
}

For detailed information about each instruction see the upcoming articles in Code Tutorials.

Your First Ardor3D Project

But for now let's just create our own project and change a few instructions:

  1. Create a new project in Eclipse: “File → New → Project…”
    
  2. Select **Java Project**
    
  3. You should see the Eclipse **New Java Project** wizard
    
  4. Enter **MyFirstArdor3DProject** in the **Project name** field
    
  5. Select **Use an execution environment** and there select **JavaSE-1.6**
    
  6. Click **Next** to go to the page **Java Settings**
    
  7. Click on the tab **Projects** and add **all** 13 Ardor3D projects as listed prior.
    
  8. Click **Finish**
    

You should now have a new project called MyFirstArdor3DProject. Now we will add a new class and copy the BoxExample source code into it and change it.

  1. Right click on your new project, select “New → Class”
    
  2. Enter **TorusExample** in the Name field
    
  3. Enter **com.ardor3d.example.ExampleBase** in the Superclass field
    
  4. **Deselect “Inherited abstract methods”** for this tutorial, to make things a bit easier.
    
  5. Click **Finish**
    
  6. Go back to or open the code of **BoxExample.java**
    
  7. Copy all lines **inside** of public class BoxExample extends ExampleBase { …HERE… }
    
  8. Paste into public class TorusExample extends ExampleBase { …HERE… }
    
  9. Change the line: `start(BoxExample.class);” → “start(TorusExample.class);`
    
  10. Change the line: `_canvas.setTitle(“Box Example”);” → “_canvas.setTitle(“Torus Example”);`
    
  11. Change the line: `box = new Box(“Box”, new Vector3(0, 0, 0), 5, 5, 5);” → “box = new Torus("Torus", 16, 8, 1.0, 2.5);`
    
  12. Add the import **com.ardor3d.scenegraph.shape.Torus**
    

Now right click TorusExample in the Package Explorer and select "Configure -> Convert to Maven Project", click “Finish" and then run the TorusExample by right clicking again and selecting “Run as → Java Application”

You should see a donut shaped object now instead of the former box.

For some additional animation add this line

box.setScale(Math.sin(angle * MathUtils.DEG_TO_RAD));

just above

_box.setRotation(rotate);

Congratulations, you have written your first Ardor3D program!