Advertisement
Guest User

Main program

a guest
Jul 20th, 2013
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.14 KB | None | 0 0
  1. package co.pixelapp.accelerometer;
  2.  
  3. import com.jme3.app.SimpleApplication;
  4. import com.jme3.input.Joystick;
  5. import com.jme3.input.JoystickAxis;
  6. import com.jme3.input.MouseInput;
  7. import com.jme3.input.SensorJoystickAxis;
  8. import com.jme3.input.controls.ActionListener;
  9. import com.jme3.input.controls.AnalogListener;
  10. import com.jme3.input.controls.MouseButtonTrigger;
  11. import com.jme3.material.Material;
  12. import com.jme3.math.ColorRGBA;
  13. import com.jme3.math.FastMath;
  14. import com.jme3.math.Quaternion;
  15. import com.jme3.math.Vector3f;
  16. import com.jme3.scene.Geometry;
  17. import com.jme3.scene.Mesh;
  18. import com.jme3.scene.shape.Box;
  19. import com.jme3.scene.shape.Line;
  20. import com.jme3.system.AppSettings;
  21. import com.jme3.texture.Texture;
  22. import com.jme3.util.IntMap;
  23. import com.jme3.util.IntMap.Entry;
  24. import java.util.List;
  25. import java.util.logging.Level;
  26. import java.util.logging.Logger;
  27.  
  28. public class Main extends SimpleApplication implements ActionListener, AnalogListener
  29. {
  30.     private static final Logger logger = Logger.getLogger(Main.class.getName());
  31.     private Geometry geomZero = null;
  32.  
  33.     // Map of joysticks saved with the joyId as the key
  34.     private IntMap<Joystick> joystickMap = new IntMap<Joystick>();
  35.  
  36.     // flag to allow for the joystick axis to be calibrated on startup
  37.     private boolean initialCalibrationComplete = false;
  38.  
  39.     // mappings used for onAnalog
  40.     private final String ORIENTATION_X_PLUS = "Orientation_X_Plus";
  41.     private final String ORIENTATION_X_MINUS = "Orientation_X_Minus";
  42.     private final String ORIENTATION_Y_PLUS = "Orientation_Y_Plus";
  43.     private final String ORIENTATION_Y_MINUS = "Orientation_Y_Minus";
  44.     private final String ORIENTATION_Z_PLUS = "Orientation_Z_Plus";
  45.     private final String ORIENTATION_Z_MINUS = "Orientation_Z_Minus";
  46.  
  47.     // variables to save the current rotation
  48.     // Used when controlling the geometry with device orientation
  49.     private float[] anglesCurrent = new float[] {0f, 0f, 0f};
  50.     private Quaternion rotationQuat = new Quaternion();
  51.  
  52.     // switch to apply an absolute rotation (geometry.setLocalRotation) or
  53.     // an incremental constant rotation (geometry.rotate)
  54.     // Used when controlling the geometry with device orientation
  55.     private boolean useAbsolute = true;
  56.  
  57.     // rotation speed to use when apply constant incremental rotation
  58.     // Used when controlling the geometry with device orientation
  59.     private float rotationSpeedX = 1f;
  60.     private float rotationSpeedY = 1f;
  61.  
  62.     // current intensity of the rumble
  63.     float rumbleAmount = 0f;
  64.  
  65.     // toggle to enable rumble
  66.     boolean enableRumble = false;
  67.  
  68.     // toggle to enable device orientation in FlyByCamera
  69.     boolean enableFlyByCameraRotation = true;
  70.  
  71.     // toggle to enable controlling geometry rotation
  72.     boolean enableGeometryRotation = true;
  73.  
  74.     // Make sure to set joystickEventsEnabled = true in MainActivity for Android
  75.  
  76.     private boolean initialized = false;
  77.    
  78.     public static void main(String[] args)
  79.     {
  80.         Main app = new Main();
  81.         AppSettings settings = new AppSettings(true);
  82.         settings.setUseJoysticks(true);
  83.         app.setSettings(settings);
  84.         app.start();
  85.     }
  86.  
  87.     private float toDegrees(float rad)
  88.     {
  89.         return rad*FastMath.RAD_TO_DEG;
  90.     }
  91.  
  92.     @Override
  93.     public void simpleInitApp()
  94.     {
  95.         // setup configuration for this test program
  96.         //        enableFlyByCameraRotation = true;
  97.         enableGeometryRotation = true;
  98.         //        useAbsolute = true;
  99.         //        enableRumble = true;
  100.  
  101.  
  102.         if (enableFlyByCameraRotation)
  103.         {
  104.             // if setDragToRotate is set, then camera rotations are only applied
  105.             // if the mouse is being clicked (ie. finger pressing on the screen)
  106.             //            flyCam.setDragToRotate(true);
  107.             flyCam.setEnabled(true);
  108.         }
  109.         else
  110.         {
  111.             flyCam.setEnabled(false);
  112.         }
  113.  
  114.         Mesh lineX = new Line(Vector3f.ZERO, Vector3f.ZERO.add(Vector3f.UNIT_X.mult(3)));
  115.         Mesh lineY = new Line(Vector3f.ZERO, Vector3f.ZERO.add(Vector3f.UNIT_Y.mult(3)));
  116.         Mesh lineZ = new Line(Vector3f.ZERO, Vector3f.ZERO.add(Vector3f.UNIT_Z.mult(3)));
  117.  
  118.         lineX.setLineWidth(2);
  119.         lineX.setLineWidth(2);
  120.         lineX.setLineWidth(2);
  121.  
  122.         Geometry geoX = new Geometry("X", lineX);
  123.         Material matX = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  124.         matX.setColor("Color", ColorRGBA.Red);
  125.         geoX.setMaterial(matX);
  126.         rootNode.attachChild(geoX);
  127.  
  128.         Geometry geoY = new Geometry("Y", lineY);
  129.         Material matY = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  130.         matY.setColor("Color", ColorRGBA.Green);
  131.         geoY.setMaterial(matY);
  132.         rootNode.attachChild(geoY);
  133.  
  134.         Geometry geoZ = new Geometry("Z", lineZ);
  135.         Material matZ = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  136.         matZ.setColor("Color", ColorRGBA.Blue);
  137.         geoZ.setMaterial(matZ);
  138.         rootNode.attachChild(geoZ);
  139.  
  140.         Box b = new Box(Vector3f.ZERO, 1, 1, 1);
  141.         geomZero = new Geometry("Box", b);
  142.  
  143.         Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  144.         mat.setColor("Color", ColorRGBA.Yellow);
  145.         Texture tex_ml = assetManager.loadTexture("Interface/Logo/Monkey.jpg");
  146.         mat.setTexture("ColorMap", tex_ml);
  147.  
  148.         geomZero.setMaterial(mat);
  149.         geomZero.setLocalTranslation(Vector3f.ZERO);
  150.         geomZero.setLocalRotation(Quaternion.IDENTITY);
  151.         rootNode.attachChild(geomZero);
  152.  
  153.         // Touch (aka MouseInput.BUTTON_LEFT) is used to record the starting
  154.         // orientation when using absolute rotations
  155.         inputManager.addMapping("MouseClick", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
  156.         inputManager.addListener(this, "MouseClick");
  157.     }
  158.  
  159.     @Override
  160.     public void simpleUpdate(float tpf)
  161.     {
  162.         if(!initialized)
  163.         {
  164.             Joystick[] joysticks = inputManager.getJoysticks();
  165.             if (joysticks == null || joysticks.length < 1)
  166.             {
  167.                 logger.log(Level.INFO, "Cannot find any joysticks!");
  168.  
  169.             }
  170.             else
  171.             {
  172.                 // Joysticks return a value of 0 to 1 based on how far the stick is
  173.                 // push on the axis.  This value is then scaled based on how long
  174.                 // during the frame the joystick axis has been in that position.
  175.                 // If the joystick is push all the way for the whole frame,
  176.                 // then the value in onAnalog is equal to tpf.
  177.                 // If the joystick is push 1/2 way for the entire frame, then the
  178.                 // onAnalog value is 1/2 tpf.
  179.                 // Similarly, if the joystick is pushed to the maximum during a frame
  180.                 // the value in onAnalog will also be scaled.
  181.                 // For Android sensors, rotating the device 90deg is the same as
  182.                 // pushing an actual joystick axis to the maximum.
  183.  
  184.                 logger.log(Level.INFO, "Number of joysticks: {0}", joysticks.length);
  185.                 JoystickAxis axis;
  186.                 for (Joystick joystick: joysticks)
  187.                 {
  188.  
  189.                     // Get and display all axes in joystick.
  190.                     List<JoystickAxis> axes = joystick.getAxes();
  191.                     for (JoystickAxis joystickAxis: axes)
  192.                     {
  193.                       logger.log(Level.INFO, "{0} axis scan Name: {1}, LogicalId: {2}, AxisId: {3}",
  194.                       new Object[]{joystick.getName(), joystickAxis.getName(), joystickAxis.getLogicalId(), joystickAxis.getAxisId()});
  195.                     }
  196.  
  197.                     // Get specific axis based on LogicalId of the JoystickAxis
  198.                     // If found, map axis
  199.                     axis = joystick.getAxis(SensorJoystickAxis.ORIENTATION_X);
  200.                     if (axis != null)
  201.                     {
  202.                         axis.assignAxis(ORIENTATION_X_PLUS, ORIENTATION_X_MINUS);
  203.                         inputManager.addListener(this, ORIENTATION_X_PLUS, ORIENTATION_X_MINUS);
  204.                         logger.log(Level.INFO, "Found {0} Joystick, assigning mapping for X axis: {1}, with max value: {2}",
  205.                         new Object[]{joystick.toString(), axis.toString(), ((SensorJoystickAxis)axis).getMaxRawValue()});
  206.                     }
  207.                     axis = joystick.getAxis(SensorJoystickAxis.ORIENTATION_Y);
  208.                     if (axis != null)
  209.                     {
  210.                         axis.assignAxis(ORIENTATION_Y_PLUS, ORIENTATION_Y_MINUS);
  211.                         inputManager.addListener(this, ORIENTATION_Y_PLUS, ORIENTATION_Y_MINUS);
  212.                         logger.log(Level.INFO, "Found {0} Joystick, assigning mapping for Y axis: {1}, with max value: {2}",
  213.                         new Object[]{joystick.toString(), axis.toString(), ((SensorJoystickAxis)axis).getMaxRawValue()});
  214.                     }
  215.                     axis = joystick.getAxis(SensorJoystickAxis.ORIENTATION_Z);
  216.                     if (axis != null)
  217.                     {
  218.                         axis.assignAxis(ORIENTATION_Z_PLUS, ORIENTATION_Z_MINUS);
  219.                         inputManager.addListener(this, ORIENTATION_Z_PLUS, ORIENTATION_Z_MINUS);
  220.                         logger.log(Level.INFO, "Found {0} Joystick, assigning mapping for Z axis: {1}, with max value: {2}",
  221.                         new Object[]{joystick.toString(), axis.toString(), ((SensorJoystickAxis)axis).getMaxRawValue()});
  222.                     }
  223.  
  224.                     joystickMap.put(joystick.getJoyId(), joystick);
  225.  
  226.                 }
  227.             }
  228.            
  229.             initialized = true;
  230.         }
  231.        
  232.         if (!initialCalibrationComplete)
  233.         {
  234.             // Calibrate the axis (set new zero position) if the axis
  235.             // is a sensor joystick axis
  236.             for (Entry<Joystick> entry: joystickMap)
  237.             {
  238.                 for (JoystickAxis axis: entry.getValue().getAxes())
  239.                 {
  240.                     if (axis instanceof SensorJoystickAxis)
  241.                     {
  242.                         logger.log(Level.INFO, "Calibrating Axis: {0}", axis.toString());
  243.                         ((SensorJoystickAxis)axis).calibrateCenter();
  244.                     }
  245.                 }
  246.             }
  247.             initialCalibrationComplete = true;
  248.         }
  249.  
  250.  
  251.         if (enableGeometryRotation)
  252.         {
  253.             rotationQuat.fromAngles(anglesCurrent);
  254.             rotationQuat.normalizeLocal();
  255.  
  256.             if (useAbsolute)
  257.             {
  258.                 geomZero.setLocalRotation(rotationQuat);
  259.             }
  260.             else
  261.             {
  262.                 geomZero.rotate(rotationQuat);
  263.             }
  264.             anglesCurrent[0] = anglesCurrent[1] = anglesCurrent[2] = 0f;
  265.         }
  266.  
  267.     }
  268.  
  269.     public void onAction(String string, boolean pressed, float tpf)
  270.     {
  271.  
  272.         if (string.equalsIgnoreCase("MouseClick") && pressed)
  273.         {
  274.  
  275.             // Calibrate the axis (set new zero position) if the axis
  276.             // is a sensor joystick axis
  277.             for (Entry<Joystick> entry: joystickMap)
  278.             {
  279.                 for (JoystickAxis axis: entry.getValue().getAxes())
  280.                 {
  281.                     if (axis instanceof SensorJoystickAxis)
  282.                     {
  283.                         logger.log(Level.INFO, "Calibrating Axis: {0}", axis.toString());
  284.                         ((SensorJoystickAxis)axis).calibrateCenter();
  285.                     }
  286.                 }
  287.             }
  288.  
  289.             if (enableRumble)
  290.             {
  291.                 // manipulate joystick rumble
  292.                 for (Entry<Joystick> entry: joystickMap)
  293.                 {
  294.                     rumbleAmount += 0.1f;
  295.                     if (rumbleAmount > 1f + FastMath.ZERO_TOLERANCE)
  296.                     {
  297.                         rumbleAmount = 0f;
  298.                     }
  299.                     logger.log(Level.INFO, "rumbling with amount: {0}", rumbleAmount);
  300.                     entry.getValue().rumble(rumbleAmount);
  301.                 }
  302.             }
  303.         }
  304.     }
  305.  
  306.     public void onAnalog(String string, float value, float tpf)
  307.     {
  308.         //logger.log(Level.INFO, "onAnalog for {0}, value: {1}, tpf: {2}",
  309.         //new Object[]{string, value, tpf});
  310.  
  311.         float scaledValue = value;
  312.  
  313.         if (string.equalsIgnoreCase(ORIENTATION_X_PLUS))
  314.         {
  315.             if (useAbsolute)
  316.             {
  317.                 // set rotation amount
  318.                 // divide by tpf to get back to actual axis value (0 to 1)
  319.                 // multiply by 90deg so that 90deg = full axis (value = tpf)
  320.                 anglesCurrent[0] = (scaledValue / tpf * FastMath.HALF_PI);
  321.                
  322.                 System.out.println("X Plus: " + anglesCurrent[0]);
  323.             }
  324.             else
  325.             {
  326.                 // apply an incremental rotation amount based on rotationSpeed
  327.                 anglesCurrent[0] += scaledValue * rotationSpeedX;
  328.             }
  329.         }
  330.         if (string.equalsIgnoreCase(ORIENTATION_X_MINUS))
  331.         {
  332.             if (useAbsolute)
  333.             {
  334.                 // set rotation amount
  335.                 // divide by tpf to get back to actual axis value (0 to 1)
  336.                 // multiply by 90deg so that 90deg = full axis (value = tpf)
  337.                 anglesCurrent[0] = (-scaledValue / tpf * FastMath.HALF_PI);
  338.                
  339.                 System.out.println("X Minus: " + anglesCurrent[0]);
  340.  
  341.             }
  342.             else
  343.             {
  344.                 // apply an incremental rotation amount based on rotationSpeed
  345.                 anglesCurrent[0] -= scaledValue * rotationSpeedX;
  346.             }
  347.         }
  348.         if (string.equalsIgnoreCase(ORIENTATION_Y_PLUS))
  349.         {
  350.             if (useAbsolute)
  351.                 {
  352.                 // set rotation amount
  353.                 // divide by tpf to get back to actual axis value (0 to 1)
  354.                 // multiply by 90deg so that 90deg = full axis (value = tpf)
  355.                 anglesCurrent[1] = (scaledValue / tpf * FastMath.HALF_PI);
  356.                
  357.                 System.out.println("Y Plus: " + anglesCurrent[1]);
  358.  
  359.             }
  360.             else
  361.             {
  362.                 // apply an incremental rotation amount based on rotationSpeed
  363.                 anglesCurrent[1] += scaledValue * rotationSpeedY;
  364.             }
  365.         }
  366.         if (string.equalsIgnoreCase(ORIENTATION_Y_MINUS))
  367.         {
  368.             if (useAbsolute)
  369.             {
  370.                 // set rotation amount
  371.                 // divide by tpf to get back to actual axis value (0 to 1)
  372.                 // multiply by 90deg so that 90deg = full axis (value = tpf)
  373.                 anglesCurrent[1] = (-scaledValue / tpf * FastMath.HALF_PI);
  374.                
  375.                 System.out.println("Y Minus: " + anglesCurrent[1]);
  376.             }
  377.             else
  378.             {
  379.                 // apply an incremental rotation amount based on rotationSpeed
  380.                 anglesCurrent[1] -= scaledValue * rotationSpeedY;
  381.             }
  382.         }
  383.  
  384.     }
  385.  
  386. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement