Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (576)
games submitted by our members
Games in WIP (498)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: Roquen (posted 2014-01-24 14:24:55, viewed 150 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220   221   222   223   224   225   226   227   228   229   230   231   232   233   234   235   236   237   238   239   240   241   242   243   244   245   246   247   248   249   250   251   252   253   254   255   256   257   258   259   260   261   262   263   264   265   266   267   268   269   270   271   272   273   274   275   276   277   278   279   280   281   282   283   284   285   286   287   288   289   290   291   292   293   294   295   296   297   298   299   300   301   302   303   304   305   306   307   308   309   310   311   312   313   314   315   316   317   318   319   320   321   322   323   324   325   326   327   328   329   330   331   332   333   334   335   336   337   338   339   340   341   342   343   344   345   346   347   348   349   350   351   352   353   354   355   356   357  
package wiki.roquen;

public class ArcBall {

  //***** TODO: Placeholder classes - replace with real ones
  public static class Vect2f {
    public float x, y;

    public Vect2f(float x, float y) {
      this.x = x;
      this.y = y;
    }
  }
  
  public static class Quat {
    public float x, y, z, w;

    /** new instance set to multiplicative identity */
    public Quat() {
      w = 1.f;
    }
    
    public final Quat setIdentity()
    {
      return set(0,0,0,1);
    }
    
    public final Quat set(Quat q)
    {
      return set(q.x,q.y,q.z,q.w);
    }
    
    public final Quat set(float x, float y, float z, float w)
    {
      this.x = x; this.y = y; this.z = z; this.w = w;
      return this;
    }
    
    public final Quat mul(Quat a, Quat b)
    { 
      float rx = a.w * b.x + b.w * a.x + a.y * b.z - a.z * b.y;
      float ry = a.w * b.y + b.w * a.y - a.x * b.z + a.z * b.x;
      float rz = a.w * b.z + b.w * a.z + a.x * b.y - a.y * b.x;
      float rw = a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z;
      
      return set(rx,ry,rz,rw);
    }
    
    public final float normL2()
    {
      return x*x+y*y+z*z+w*w;
    }
    
    /** assumes (x,y,z) is unit */
    public final Quat fromAxisAngle(float x, float y, float z, float a)
    {
      float s = (float)Math.sin(a);
  
      return set(s*x, s*y, s*z, (float)Math.cos(a));
    }
    
    public final void getRotMatrix(java.nio.FloatBuffer m, boolean t, Vect3f o)
    {
      float m00,m01,m02;
      float m10,m11,m12;
      float m20,m21,m22;
      
      float xs = x + x;
      float ys = y + y;
      float zs = z + z;
      
      float wx = w * xs;
      float wy = w * ys;
      float wz = w * zs;
      
      float xx = x * xs;
      float xy = x * ys;
      float xz = x * zs;
      
      float yy = y * ys;
      float yz = y * zs;
      float zz = z * zs;
      
      m00 = 1.f - (yy + zz);
      m11 = 1.f - (xx + zz);
      m22 = 1.f - (xx + yy);

      if (!t) {
        m01 = xy - wz;
        m02 = xz + wy;
        
        m10 = xy + wz;
        m12 = yz - wx;
        
        m20 = xz - wy;
        m21 = yz + wx;
      }
      else {
        m01 = xy + wz;
        m02 = xz - wy;
        
        m10 = xy - wz;
        m12 = yz + wx;
        
        m20 = xz + wy;
        m21 = yz - wx;
      }

      m.put(m00).put(m01).put(m02).put(0.f);
      m.put(m10).put(m11).put(m12).put(0.f);
      m.put(m20).put(m21).put(m22).put(0.f);
      m.put(o.x).put(o.y).put(o.z).put(1.f);
    }
    
  }
  
  public static class Vect3f {

    public float x, y, z;

    public final Vect3f set(float x, float y, float z)
    {
      this.x=x; this.y=y; this.z=z;
      return this;
    }
    
    public final Vect3f cross(Vect3f a, Vect3f b) 
    {
      float rx = a.y*b.z - a.z*b.y;
      float ry = a.z*b.x - a.x*b.z;
      float rz = a.x*b.y - a.y*b.x;
      
      return set(rx,ry,rz);
    }

    public final float dot(Vect3f v) {
      return x*v.x + y*v.y + z*v.z;
    }
    
    public final float normL2()
    {
      return x*x+y*y+z*z;
    }

    public final float length() {
      return (float)Math.sqrt(x * x + y * y + z * z);
    }

  }
  
//***** TODO: Placeholder classes - end

  public enum MODE {
    ARCBALL,
    CONSTRAIN_Z,
  };
  
  
  // this is a stupid choice
  private static final float ESP = 0.0001f;

  // This class is a zero performance concern.  Use
  // object composition instead of hard baking.
  
  /** Orientation prior to controller interaction. */
  private final Quat   q0;
  
  /** Incremental change in orientation from controller. */
  private final Quat   q1;
  
  /** Composed orientation: q<sub>1</sub>q<sub>0</sub> */
  private final Quat   qt;
  
  private float radius2;
  
  private float iradius;
  
  /** Projected 3D vector at mouse down */
  private final Vect3f downV;
  
  /** Current projected 3D vector  */
  private final Vect3f dragV;
  
  /** Temporary for finding axis */
  private final Vect3f dir;
  
  /** Post translation to auto-apply when returning a matrix. */
  private final Vect3f t;
  
  /** Scaling factors for screen -> normalized coordinates */
  private float scaleX, scaleY;
  
  private MODE  mode;

  public ArcBall(float w, float h) {
    mode   = MODE.ARCBALL;
    q0     = new Quat();
    q1     = new Quat();
    qt     = new Quat();
    downV  = new Vect3f();
    dragV  = new Vect3f();
    dir    = new Vect3f();
    t      = new Vect3f();
    setRadius(.8f);
    setBound(w, h);
    
    // TODO: temp hack
    t.z = -2.f;
  }
  
  public final MODE getMode() { return mode; }
  
  public final void setMode(MODE mode)
  {
    this.mode = mode;
  }
  
  /**
   * Reset the composed rotation and controller to identity.
   */
  public final void reset()
  {
    qt.setIdentity();
    q0.setIdentity();
    q1.setIdentity();
  }
  
  public final void discard()
  {
    q1.set(q0);
    q0.setIdentity();
    q1.setIdentity();
  }
  
  public final void setTo(float x, float y, float z, float a)
  {
    q0.fromAxisAngle(x, y, z, a);
  }
  
  /** Fills 'q' with the current composed rotation. */
  public final void get(Quat q)
  {
    q.set(qt);
  }
  
  /**
   * Set the screen space normalized radius of the circle
   * which bounds the virtual sphere.
   * <p>
   * Legal values are on (0,1].
   */
  public final void setRadius(float r)
  {
    assert(r > 0 && r <= 1.f);
    radius2 = r*r;
    iradius = 1.f/radius2;
  }
  
  public final float getRadius()
  {
    return (float)Math.sqrt(radius2);
  }

  /** Project the 2D screen position to a 3D axis */
  private boolean project(float x, float y, Vect3f r) 
  {
    // map screen coordinates -> normalized
    x = (x * scaleX) - 1.f;
    y = (y * scaleY) - 1.f;
    
    float mag2 = x*x + y*y;

    // Is the point inside the projected circle?
    if (mag2 <= radius2) {
      // yeap: find unit vector in the direction of origin to
      // the projected point on the sphere.
      r.x = x * iradius;
      r.y = y * iradius;
      r.z = (float)Math.sqrt(1.f-iradius*iradius*mag2);
      
      return true;
    }
    
    // nope: find unit vector in the direction of the origin
    // to the edge of the sphere toward normalized point.
    float norm = (float)(1.0/Math.sqrt(mag2));
    r.x = x * norm;
    r.y = y * norm;
    r.z = 0.f;
    
    return false;
  }

  public void setBound(float w, float h) {
    assert ((w > 1.0f) && (h > 1.0f));

    // scale factors for screen to normalized coordinates
    scaleX = 1.f / ((w - 1.f) * 0.5f);
    scaleY = 1.f / ((h - 1.f) * 0.5f);
  }

  /**
   * Called on the appropriate mouse down event with
   * global screen space coordinates of the mouse
   * position.
   */
  public void onDown(float mouseX, float mouseY)
  {
    project(mouseX, mouseY, downV);
  }
  
  /**
   * Called when the appropriate mouse button is released. If
   * compose is <i>true</i> the active controller rotation
   * is composed, otherwise it is discarded.
   */
  public void onUp(boolean compose)
  {
    if (compose) {
      q0.set(qt);
      q1.setIdentity();
    }
    else discard();
  }

  /**
   * Called on mouse drag events with global
   * screen space coordinates of the mouse position.
   */
  public void onDrag(float mouseX, float mouseY)
  {
    // project the current 2D input position
    project(mouseX, mouseY, dragV);
    
    // find a vector orthogonal: sin(t)v
    dir.cross(downV, dragV);

    if (dir.normL2() > ESP) {
      // controller rotation: (sin(t)v, cos(t))
      q1.x = dir.x;
      q1.y = dir.y;
      q1.z = dir.z;
      q1.w = downV.dot(dragV);
      
      // compose the rotations 
      qt.mul(q1,q0);
    }
  }

  /** Fills fb with a rotation matrix and rewinds...temp hack */
  public void getMatrix(java.nio.FloatBuffer fb)
  {
    qt.getRotMatrix(fb, true, t);
    fb.rewind();
  }
  
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (17 views)
2014-04-15 18:08:23

BurntPizza (14 views)
2014-04-15 03:46:01

UprightPath (27 views)
2014-04-14 17:39:50

UprightPath (12 views)
2014-04-14 17:35:47

Porlus (29 views)
2014-04-14 15:48:38

tom_mai78101 (52 views)
2014-04-10 04:04:31

BurntPizza (110 views)
2014-04-08 23:06:04

tom_mai78101 (211 views)
2014-04-05 13:34:39

trollwarrior1 (180 views)
2014-04-04 12:06:45

CJLetsGame (185 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!