Android Open Source - flingbox Render Camera






From Project

Back to project page flingbox.

License

The source code is released under:

GNU General Public License

If you think the Android project flingbox 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

/*
 *  Flingbox - An OpenSource physics sandbox for Google's Android
 *  Copyright (C) 2009  Jon Ander Pe?alba & Endika Guti?rrez
 */*from w  ww.j  a va2s. c  om*/
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package edu.eside.flingbox.graphics;

import edu.eside.flingbox.math.Vector2D;

/**
 * Specifies OpenGL camera interface.
 * By setting camera's position and width camera could 
 * be moved.
 */
public class RenderCamera {
  /** Used by OpenGL */
  public float left, rigth, top, bottom;
  
  /** Flag to change OpenGL's camera */
  public boolean isChanged;
  
  /** Camera position and aperture */
  private final Vector2D mPosition, mAperture;
  
  /** Surface size */
  private float mSurfaceWidth, mSurfaceHeight;

  /**
   * Default constructor for a surface
   */
  public RenderCamera(float surfaceWidth, float surfaceHeight) {
    mPosition = new Vector2D();
    mAperture = new Vector2D(surfaceWidth, surfaceHeight);
    
    this.setSurface(surfaceWidth, surfaceHeight);
  }
  
  /**
   * Changes surface size
   */
  public void setSurface(float surfaceWidth, float surfaceHeight) {
    mSurfaceWidth = surfaceWidth;
    mSurfaceHeight = surfaceHeight;
    
    mAperture.set(surfaceWidth, surfaceHeight);
  
    updateGLCamera();
  }
  
  /**
   * Sets Camera's position.
   * 
   * @param x Center of the focus, x
   * @param y Center of the focus, y
   * @param width Width of camera's frame.
   *     height is calculated to keep aspect ratio
   */
  public void setPosition(final Vector2D newPosition) {
    mPosition.set(newPosition);

    updateGLCamera();
  }
  
  /**
   * Sets camera's aperture
   * 
   * @param horizontalAperture camera's horizontal aperture
   */
  public void setAperture(Vector2D aperture) {
    mAperture.set(aperture);
    
    updateGLCamera();
  }

  /** Sets coordinates needed by OpenGL from camera's position */
  private void updateGLCamera() {
    final float halfWidth = mAperture.i / 2;
    final float halfHeight = mAperture.j / 2;
    
    this.left = mPosition.i - halfWidth;
    this.rigth = mPosition.i + halfWidth;
    this.bottom = mPosition.j - halfHeight;
    this.top = mPosition.j + halfHeight;
    
    this.isChanged = true;
  }
  
  /** @return projected vector */
  public Vector2D project(Vector2D v) {
    return v.set(left + (v.i * mAperture.i / mSurfaceWidth), 
        top - (v.j * mAperture.j / mSurfaceHeight));
  }
  
  /** @return scaled vector */
  public Vector2D scale(Vector2D v) {
    return v.set(v.i * mAperture.i / mSurfaceWidth, 
        -v.j * mAperture.j / mSurfaceHeight);
  }
  
  /** @return  camera's position */
  public Vector2D getPosition() {
    return mPosition;
  }
  
  /** @return  camera's aperture */
  public Vector2D getAperture() {
    return mAperture;
  }
}




Java Source Code List

edu.eside.flingbox.BodySettingsDialog.java
edu.eside.flingbox.FlingboxActivity.java
edu.eside.flingbox.PreferencesActivity.java
edu.eside.flingbox.Preferences.java
edu.eside.flingbox.bodies.Body.java
edu.eside.flingbox.bodies.Polygon.java
edu.eside.flingbox.graphics.RenderBody.java
edu.eside.flingbox.graphics.RenderCamera.java
edu.eside.flingbox.graphics.RenderPolygon.java
edu.eside.flingbox.graphics.SceneRenderer.java
edu.eside.flingbox.input.SceneGestureDetector.java
edu.eside.flingbox.math.Intersect.java
edu.eside.flingbox.math.Matrix22.java
edu.eside.flingbox.math.PolygonUtils.java
edu.eside.flingbox.math.Vector2D.java
edu.eside.flingbox.physics.PhysicAtomicBody.java
edu.eside.flingbox.physics.PhysicBody.java
edu.eside.flingbox.physics.PhysicPolygon.java
edu.eside.flingbox.physics.ScenePhysics.java
edu.eside.flingbox.physics.collisions.Arbiter.java
edu.eside.flingbox.physics.collisions.ColliderPolygon.java
edu.eside.flingbox.physics.collisions.Collider.java
edu.eside.flingbox.physics.collisions.ContactSolver.java
edu.eside.flingbox.physics.collisions.Contact.java
edu.eside.flingbox.physics.gravity.GravitySource.java
edu.eside.flingbox.scene.DrawingBody.java
edu.eside.flingbox.scene.DrawingPolygon.java
edu.eside.flingbox.scene.Scene.java
edu.eside.flingbox.utils.PositionComparator.java
edu.eside.flingbox.xml.InvalidXmlException.java
edu.eside.flingbox.xml.XmlExporter.java
edu.eside.flingbox.xml.XmlImporter.java