Android Open Source - SevenWonders Tilt Control






From Project

Back to project page SevenWonders.

License

The source code is released under:

Apache License

If you think the Android project SevenWonders listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package skylight1.sevenwonders.view;
//from   w w  w.  j  a v  a 2  s .c o m
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.view.Display;
import android.view.Surface;
import android.view.WindowManager;

/**
 * Controls the carpet by measuring the tilt of the phone.
 *
 */
public class TiltControl {

  /**
   * The range of roll values you have to tilt the phone through to 
   * access the full speed of the carpet. This makes it so that you 
   * don't have to tilt the phone a full 90 degrees to go from stopped 
   * to full speed.
   */
  private static final int ROLL_TO_MOVE_RANGE_DEGREES = 30;

  /**
   * The roll where the carpet is considered not to be moving. People 
   * do not usually hold the phone vertical, so this allows stopping 
   * the carpet without fully tilting the phone to vertical, which 
   * makes the screen tough to read.
   */
  private static final int MINIMUM_ROLL_TO_MOVE_DEGREES = 25;

  /**
   * Value to start inverting the roll from. The phone's roll goes down
   * as the phone is closer to being flat, but we want this to correspond
   * to going faster.
   */
  private static final int ROLL_INVERT_START_DEGREES = 90;

  /** 
   * Divisor to convert the phone's pitch to the carpet's pitch. 
   * This is used to make the carpet turn more than the actual phone.
   */
  private static final int PHONE_PITCH_TO_CARPET_PITCH_DIVISOR = -10;
  
  private final SensorManager sensorManager;

  private Display display;
  
  private final SevenWondersGLRenderer renderer;
  
  private final Sensor orientationSensor;
  
  private final SensorEventListener orientationChangedListener = new SensorEventListener() {

    @Override
    public void onAccuracyChanged(final Sensor aSensor, final int aAccuracy) {
      // do nothing
      
    }

    @Override
    public void onSensorChanged(final SensorEvent aEvent) {

      //adjust for rotation if landscape is the natural orientation
      //ROTATION_90 from natural orientation would be portrait based devices (only value I see w/phones) 
      if(display.getOrientation() == Surface.ROTATION_0) {
        float temp = aEvent.values[1];
        aEvent.values[1] = aEvent.values[2];
        aEvent.values[2] = temp + 180;
      }
  
      // not using event.values[0], it's the azimuth, rotation around the z-aixs
      // Point the carpet left or right depending on phone being tilted left or right.
      final int phonePitch = (int) aEvent.values[1]; // rotation around x-axis
      final int carpetPitch = phonePitch / PHONE_PITCH_TO_CARPET_PITCH_DIVISOR;
      renderer.setTurningVelocity(carpetPitch);  
      
      // Speed up the carpet when tilted forward.
      final float roll =  aEvent.values[2]; // rotation around y-axis  
      final float velocityPercent = 
        ( (ROLL_INVERT_START_DEGREES - roll) - MINIMUM_ROLL_TO_MOVE_DEGREES ) 
        / ROLL_TO_MOVE_RANGE_DEGREES;
      renderer.setVelocityPercent(velocityPercent);
    }
  };
  
  public TiltControl(final Context aContext, final SevenWondersGLRenderer aRenderer) {
    this.renderer = aRenderer;
    WindowManager windowManager = (WindowManager) aContext.getSystemService(Context.WINDOW_SERVICE);
    display = windowManager.getDefaultDisplay();
    sensorManager = (SensorManager) aContext.getSystemService(android.content.Context.SENSOR_SERVICE);
    orientationSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
  }
  
  public void stop() {
    sensorManager.unregisterListener(orientationChangedListener);
  }

  public void start() {
    // XXX This is deprecated in favor of SensorManager#getOrientation, 
    // but that requires listening to two sensors at once which is more complex.
    sensorManager.registerListener(orientationChangedListener, 
      orientationSensor, SensorManager.SENSOR_DELAY_GAME);
  }
  
}




Java Source Code List

com.facebook.android.AsyncFacebookRunner.java
com.facebook.android.DialogError.java
com.facebook.android.FacebookError.java
com.facebook.android.Facebook.java
com.facebook.android.FbDialog.java
com.facebook.android.Util.java
skylight1.sevenwonders.AboutActivity.java
skylight1.sevenwonders.Adverts.java
skylight1.sevenwonders.Analytics.java
skylight1.sevenwonders.GameState.java
skylight1.sevenwonders.LevelChooserActivity.java
skylight1.sevenwonders.MenuActivity.java
skylight1.sevenwonders.PlayActivity.java
skylight1.sevenwonders.ScoreActivity.java
skylight1.sevenwonders.SettingsActivity.java
skylight1.sevenwonders.Settings.java
skylight1.sevenwonders.SevenWondersApplication.java
skylight1.sevenwonders.SoundTracksStoppingExceptionHandler.java
skylight1.sevenwonders.SplashActivity.java
skylight1.sevenwonders.StoryActivity.java
skylight1.sevenwonders.levels.CoinCollisionAction.java
skylight1.sevenwonders.levels.CollisionAction.java
skylight1.sevenwonders.levels.ExtraTimeAction.java
skylight1.sevenwonders.levels.GameLevel.java
skylight1.sevenwonders.levels.GameObjectDescriptor.java
skylight1.sevenwonders.levels.HazardCollisionAction.java
skylight1.sevenwonders.levels.LevelConstructionToolkit.java
skylight1.sevenwonders.levels.ProtectionAction.java
skylight1.sevenwonders.levels.RubyCollisionAction.java
skylight1.sevenwonders.levels.SpellCollisionAction.java
skylight1.sevenwonders.services.SoundTracks.java
skylight1.sevenwonders.social.DialogUtil.java
skylight1.sevenwonders.social.NoNPEWebView.java
skylight1.sevenwonders.social.facebook.FacebookApplicationPost.java
skylight1.sevenwonders.social.facebook.FacebookConfig.java
skylight1.sevenwonders.social.facebook.FacebookScoreActivity.java
skylight1.sevenwonders.social.facebook.FacebookUtil.java
skylight1.sevenwonders.social.facebook.LoginAndGetPermissions.java
skylight1.sevenwonders.social.facebook.SessionStore.java
skylight1.sevenwonders.social.facebook.WallPost.java
skylight1.sevenwonders.social.twitter.AuthRequest.java
skylight1.sevenwonders.social.twitter.AuthStore.java
skylight1.sevenwonders.social.twitter.TwitterUpdater.java
skylight1.sevenwonders.view.Carpet.java
skylight1.sevenwonders.view.CubeBounds.java
skylight1.sevenwonders.view.GameMessagesDisplay.java
skylight1.sevenwonders.view.GameTexture.java
skylight1.sevenwonders.view.Position.java
skylight1.sevenwonders.view.SevenWondersGLRenderer.java
skylight1.sevenwonders.view.SevenWondersGLSurfaceView.java
skylight1.sevenwonders.view.StoryPagesController.java
skylight1.sevenwonders.view.StyledSpannableStringBuilder.java
skylight1.sevenwonders.view.TextStyles.java
skylight1.sevenwonders.view.TiltControl.java
wave.Constants.java
wave.CreateWave.java
wave.RectangleWave.java
wave.TriangleWave.java
wave.UtilsWave.java
wave.VertexWave.java