Trackball samples

hi all

anyone can suggest me a trackball sample with jme3?

A trackball is the same thing as a mouse as far as i know. You will have to be more specific if you mean anything else than this:

i need a sphere in the center of the scene ; ) that react as a mouse trackball with drag mouse .my purpose till my begin with jme3 :wink:

with java 3d it’s quite easy manage rotation but with jme3 i have axis rotation problems as you well know eheh

Like this?

[java]

/*

  • Copyright © 2009-2011 jMonkeyEngine
  • All rights reserved.

    *
  • Redistribution and use in source and binary forms, with or without
  • modification, are permitted provided that the following conditions are
  • met:

    *
    • Redistributions of source code must retain the above copyright
  • notice, this list of conditions and the following disclaimer.

    *
    • Redistributions in binary form must reproduce the above copyright
  • notice, this list of conditions and the following disclaimer in the
  • documentation and/or other materials provided with the distribution.

    *
    • Neither the name of ‘jMonkeyEngine’ nor the names of its contributors
  • may be used to endorse or promote products derived from this software
  • without specific prior written permission.

    *
  • THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  • "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  • TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  • PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  • CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  • EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  • PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  • PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  • LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  • NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  • SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

    */

    package testcases;



    import com.jme3.app.SimpleApplication;

    import com.jme3.asset.TextureKey;

    import com.jme3.input.MouseInput;

    import com.jme3.input.controls.ActionListener;

    import com.jme3.input.controls.AnalogListener;

    import com.jme3.input.controls.MouseAxisTrigger;

    import com.jme3.input.controls.MouseButtonTrigger;

    import com.jme3.light.DirectionalLight;

    import com.jme3.material.Material;

    import com.jme3.math.FastMath;

    import com.jme3.math.Quaternion;

    import com.jme3.math.Vector3f;

    import com.jme3.scene.Geometry;

    import com.jme3.scene.shape.Sphere;

    import com.jme3.texture.Texture;



    /**

    *
  • @author normenhansen

    */

    public class TestRollBall extends SimpleApplication implements ActionListener, AnalogListener {



    Material matRock;

    Geometry sphere;

    Vector3f lookAt = new Vector3f(0,0,1);

    Vector3f up = new Vector3f(0,1,0);

    boolean pressed = false;



    public static void main(String[] args) {

    TestRollBall app = new TestRollBall();

    app.start();

    }



    @Override

    public void simpleInitApp() {

    setupInput();

    createMaterial();

    Sphere mesh = new Sphere(16, 16, 1);

    sphere = new Geometry("sphere", mesh);

    sphere.setMaterial(matRock);

    rootNode.attachChild(sphere);

    rootNode.addLight(new DirectionalLight());

    flyCam.setEnabled(false);

    }



    private void setupInput() {

    inputManager.addMapping("MouseLeft", new MouseAxisTrigger(MouseInput.AXIS_X, true));

    inputManager.addMapping("MouseRight", new MouseAxisTrigger(MouseInput.AXIS_X, false));

    inputManager.addMapping("MouseUp", new MouseAxisTrigger(MouseInput.AXIS_Y, true));

    inputManager.addMapping("MouseDown", new MouseAxisTrigger(MouseInput.AXIS_Y, false));

    inputManager.addMapping("Button", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));

    inputManager.addListener(this, "MouseLeft", "MouseRight", "MouseUp", "MouseDown", "Button");

    }



    private void createMaterial() {

    matRock = new Material(assetManager, "Common/MatDefs/Misc/SimpleTextured.j3md");

    TextureKey key2 = new TextureKey("Textures/Terrain/Rock/Rock.PNG");

    key2.setGenerateMips(true);

    Texture tex2 = assetManager.loadTexture(key2);

    matRock.setTexture("ColorMap", tex2);

    }



    @Override

    public void simpleUpdate(float tpf) {

    sphere.lookAt(lookAt, up);

    }



    public void onAction(String binding, boolean value, float tpf) {

    if (binding.equals("Button")) {

    pressed = value;

    System.out.println("button: " + value);

    }

    }



    public void onAnalog(String binding, float value, float tpf) {

    if (binding.equals("MouseLeft")) {

    if (pressed) {

    Quaternion add = new Quaternion().fromAngleAxis(-FastMath.QUARTER_PI * value * 10, Vector3f.UNIT_Y);

    add.multLocal(lookAt);

    add.multLocal(up);

    } else {

    }

    } else if (binding.equals("MouseRight")) {

    if (pressed) {

    Quaternion add = new Quaternion().fromAngleAxis(FastMath.QUARTER_PI * value * 10, Vector3f.UNIT_Y);

    add.multLocal(lookAt);

    add.multLocal(up);

    } else {

    }

    } else if (binding.equals("MouseUp")) {

    if (pressed) {

    Quaternion add = new Quaternion().fromAngleAxis(FastMath.QUARTER_PI * value * 10, Vector3f.UNIT_X);

    add.multLocal(lookAt);

    add.multLocal(up);

    } else {

    }

    } else if (binding.equals("MouseDown")) {

    if (pressed) {

    Quaternion add = new Quaternion().fromAngleAxis(-FastMath.QUARTER_PI * value * 10, Vector3f.UNIT_X);

    add.multLocal(lookAt);

    add.multLocal(up);

    } else {

    }

    }

    }

    }

    [/java]
2 Likes

perfect normen great job

This is only using stuff from math for dummies btw.



http://i.imgur.com/8yyUL.png



P.S. If you stumble together your whole application from forum code posts I have to demand you donate the money for this job to the jMonkeyEngine project.

the compensation you have done is not for dummies and it is not mentioned in tutorial.

If i’ll become like steeve jobs (hopefully more healthy) i’ll offer you my super chief administrator position.

Or you prefer an icy beer?

I just rotate two vectors using quaternions and use the lookAt method, both is explained in the math for dummies document.

hi normen

suppose my sphere is traslated on the right side.How can i change your code for rotating the sphere ?



i created a node pivot and i added my spatial to it.After that i traslated the node;

[java]sphere = new Geometry("sphere", mesh);

sphere.setMaterial(matRock);

pivot1.setLocalTranslation(new Vector3f(1,0,0));

pivot1.attachChild(sphere);

rootNode.attachChild(pivot1);[/java]



but now rotation on y axis is more slow.

Should i traslate the lookAt and up vector? how to do that?