0

JMonkey Engine 3 : Example of vertex docking ( snap to grid ) mouse picking and more

Here is a test app to help explain how to find a vertex in a geometry and attach another geometry to the vertex…..

All that code below will give you something like this:

what it shows you...

what it shows you…

Check out the comments in the code for details on whats going on, but use the left mouse button to drag the yellow ball. The red arrow shows you where the pick is currently on the surface of the sphere. Release the mouse and the yellow ball sticks to the sphere. Enjoy. Press T to toggle snapping and watch as the yellow ball jumps from vertex to vertex..wow!.

UPDATE : I found an issue in my first version that caused the yellow ball to be placed in the wrong location if the picked geometry was moved from 0,0,0. This version shows how snapping can be done between separate geometries. I intend to write a better demo to show how to move nodes around and dock them to other nodes using this method. Watch out for that next week.

The Main file. You will need the mouse sensor control below also.

MouseSensor.java

0

JME3 Eight point rotation control for spatial on the guiNode

This control class adds eight points of rotation to a spatial. this version was written for use with a spatial attached to the GUINode. It can be applied to Nodes on the rootNode as well with just a bit of tweaking.

this is the GUINODE version

And this is the RootNode version.

0

JME 3 orbiting nodes

Hi, well I have started learning to use the JME3 engine and I must say for what I have seen so far its vary good. There is, as with anything like this a bit of a steep learning curve, but its not too hard and there are plenty of docs and tutorials on the web.

So the point of this post is that I solved my first problem in JME3 that I thought was noteworthy. I’m just messing and learning at the moment and one of the things I wanted to try was to build a simple solar system complete with a star, planets and moons. The first two parts where easy, but getting the moons to orbit the planets as desired caused me no end of head scratching. I posted up and the JME forums to ask for advice and got it. The advice was that I was doing to all wrong. Ha! so I started over and took the pointers I was given and came up with the code below. If you have the JME3 SDK you can run this and it will show a very simple system with a sun and a planet and moon orbiting as they should. I’m posting this because I think its something others could use and will save them days of frustrations.

Enjoy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
 package mygame;
 
import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Box;
import com.jme3.system.AppSettings;
 
/**
 * test
 *
 * @author normenhansen
 */
public class Main extends SimpleApplication {
 
    public static void main(String[] args) {
        Main app = new Main();
        AppSettings gameSettings = new AppSettings(false);
        gameSettings.setFullscreen(false);
        gameSettings.setResolution(1680, 1050);
 
        gameSettings.setVSync(false);
        gameSettings.setTitle("Dyson Wars");
        gameSettings.setUseInput(true);
        gameSettings.setFrameRate(500);
        gameSettings.setSamples(0);
        gameSettings.setRenderer("LWJGL-OpenGL2");
        app.settings = gameSettings;
        app.setShowSettings(false);
 
        app.start();
    }
    private Node sun; // the sun
    private Node planetOrbitNode; //the planets orbit node
    private Node planet; // the planet
    private Node moonOrbitNode; // te moons orbit node
    private Node moon; // the moon
 
    @Override
    public void simpleInitApp() {
        cam.setLocation(new Vector3f(0, 0, 20));
        cam.lookAt(new Vector3f(0, 0, 0), Vector3f.UNIT_Y);
        flyCam.setMoveSpeed(100);
        flyCam.setZoomSpeed(1);
 
 
        Node system = new Node("System");
 
        sun = new Node("Sun");
        Geometry g_sun = new Geometry("Sun", new Box(0.5f, 0.5f, 0.5f));
        Material m_sun = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        m_sun.setColor("Color", ColorRGBA.Orange);
        g_sun.setMaterial(m_sun);
        sun.attachChild(g_sun);
 
 
        planet = new Node("Planet");
        Geometry g_planet = new Geometry("planet", new Box(0.5f, 0.5f, 0.5f));
        Material m_planet = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        m_planet.setColor("Color", ColorRGBA.Green);
        g_planet.setMaterial(m_planet);
        planet.attachChild(g_planet);
        planetOrbitNode = new Node("PON");
        planetOrbitNode.attachChild(planet);
        planet.setLocalTranslation(0, 3, 0);
 
        // 
        moon = new Node("moon");
        Geometry g_moon = new Geometry("moon", new Box(0.25f, 0.25f, 0.25f));
        Material m_moon = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        m_moon.setColor("Color", ColorRGBA.DarkGray);
        g_moon.setMaterial(m_moon);
        moon.attachChild(g_moon);
 
        // attach the moon to its orbit node
        moonOrbitNode = new Node("MON");
        moonOrbitNode.attachChild(moon);
        moon.setLocalTranslation(0, 1.5f, 0);
        // attatch the moons orbit node to the planet
        planet.attachChild(moonOrbitNode);
 
 
        system.attachChild(planetOrbitNode);
        system.attachChild(sun);
        rootNode.attachChild(system);
 
    }
 
    @Override
    public void simpleUpdate(float tpf) {
        this.moonOrbitNode.rotate(0, 0, -0.002f);
        this.moon.rotate(0, 0, 0.001f);
        this.planetOrbitNode.rotate(0, 0, 0.001f);
        this.planet.rotate(0, 0, 0.001f);
        this.sun.rotate(0, 0, -0.002f);
    }
 
    @Override
    public void simpleRender(RenderManager rm) {
        //TODO: add render code
    }
}