Android Open Source - 4est Player






From Project

Back to project page 4est.

License

The source code is released under:

MIT License

If you think the Android project 4est 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 com.wordsaretoys.forest;
/*ww  w. j a va2  s  .c  om*/
import com.wordsaretoys.rise.geometry.Camera;
import com.wordsaretoys.rise.geometry.Vector;
import com.wordsaretoys.rise.utility.Interval;

/**
 * player state and interaction
 */
public class Player {

  static final float CollideAt = 0.05f;
  static final float TargetAt = -0.9f;
  
  // player eye-line camera
  public Camera camera;
  
  // looking and moving state
  Vector looking = new Vector();
  boolean moving;

  // timer interval
  Interval interval = new Interval();
  
  // movement handling vectors
  Vector direction = new Vector();
  Vector velocity = new Vector();
  Vector normal = new Vector();
  
  // map listener
  Map.Listener mapper;
  
  // target object id
  long target;
  
  /**
   * ctor
   */
  public Player() {
    camera = new Camera(30, 0.01f, 100);
    mapper = new Map.Listener() {
      Vector op = new Vector();
      @Override
      public void onObject(long id, int what, float x, float y, float z, float r) {
        op.set(x, y, z).sub(camera.position);
        float d = op.length() - r;
        op.norm().neg();
        if (d <= CollideAt) {
          normal.add(op);
        }
        float p = op.dot(camera.front);
        if (p < TargetAt) {
          target = id;
        }
      }
    };
  }
  
  public void setLook(float dx, float dy) {
    looking.set(-0.5f * dy, -0.5f * dx, 0);
  }
  
  public void setMove(boolean on) {
    moving = on;
  }

  /**
   * updates player kinematic state
   * call on every GL frame
   */
  public void update() {
    
    float dt = interval.next();
    
    // rotate camera (with roll stabilization)
    camera.turn(0.25f * looking.x * dt, 0.25f * looking.y * dt, 10 * camera.right.y * dt);
    looking.mul(0.9f);
    
    // move camera if thrusting
    direction.copy(camera.front).mul(moving ? 1 : 0);

    // any collisions or targets in the near distance?
    Vector cp = camera.position;
    target = 0;
    normal.set(0, 0, 0);
    Shared.map.scanVolume(cp.x, cp.y, cp.z, 1, mapper);
    normal.mul(velocity.length());
    Shared.dbg.set("target", target == 0 ? "none" : Long.toString(target));
    if (normal.length() > 0) {
      Shared.audio.crash.start();
    }
    
    // determine new velocity and position
    direction.mul(dt);
    velocity.add(direction).add(normal).mul(0.99f);
    camera.move(velocity.x * dt, velocity.y * dt, velocity.z * dt);
    
    Vector p = camera.position;
    Shared.dbg.set("pos", p.x, p.y, p.z, 100);
  }
  
}




Java Source Code List

com.wordsaretoys.forest.Audio.java
com.wordsaretoys.forest.Debris.java
com.wordsaretoys.forest.Game.java
com.wordsaretoys.forest.GlView.java
com.wordsaretoys.forest.MainActivity.java
com.wordsaretoys.forest.Map.java
com.wordsaretoys.forest.Player.java
com.wordsaretoys.forest.Render.java
com.wordsaretoys.forest.Rotors.java
com.wordsaretoys.forest.Shared.java
com.wordsaretoys.forest.Skybox.java
com.wordsaretoys.rise.geometry.Camera.java
com.wordsaretoys.rise.geometry.Geom.java
com.wordsaretoys.rise.geometry.Mote.java
com.wordsaretoys.rise.geometry.Ortho.java
com.wordsaretoys.rise.geometry.Quaternion.java
com.wordsaretoys.rise.geometry.Vector.java
com.wordsaretoys.rise.glwrapper.Mesh.java
com.wordsaretoys.rise.glwrapper.Shader.java
com.wordsaretoys.rise.glwrapper.Texture.java
com.wordsaretoys.rise.meshutil.HeightMapper.java
com.wordsaretoys.rise.meshutil.IndexBuffer.java
com.wordsaretoys.rise.meshutil.SurfaceMapper.java
com.wordsaretoys.rise.meshutil.VertexBuffer.java
com.wordsaretoys.rise.meshutil.Vindexer.java
com.wordsaretoys.rise.pattern.Bitmap.java
com.wordsaretoys.rise.pattern.F2FSumMap.java
com.wordsaretoys.rise.pattern.I2FCutMap.java
com.wordsaretoys.rise.pattern.I2FMap.java
com.wordsaretoys.rise.pattern.I2IMap.java
com.wordsaretoys.rise.pattern.Pattern.java
com.wordsaretoys.rise.pattern.Ring.java
com.wordsaretoys.rise.utility.Asset.java
com.wordsaretoys.rise.utility.Board.java
com.wordsaretoys.rise.utility.Dbg.java
com.wordsaretoys.rise.utility.Interval.java
com.wordsaretoys.rise.utility.Misc.java
com.wordsaretoys.rise.utility.Needle.java