Exemple #1
0
 public Vector getSurfaceNormal(Coordinates c) {
   Vector s = immerseVector(c, ddu);
   Vector t = immerseVector(c, ddv);
   s.normalize();
   t.normalize();
   return s.crossProduct(t);
 }
  public static Transform rotate(final double angle, final Vector vector) {
    vector.normalize();

    final double sin = Math.sin(Math.toRadians(angle));
    final double cos = Math.cos(Math.toRadians(angle));

    final double[][] m = new double[4][4];

    m[0][0] = vector.getX() * vector.getX() + (1.0D - vector.getX() * vector.getX()) * cos;
    m[0][1] = vector.getX() * vector.getY() * (1.0D - cos) - vector.getZ() * sin;
    m[0][2] = vector.getX() * vector.getZ() * (1.0D - cos) + vector.getY() * sin;
    m[0][3] = 0.0D;

    m[1][0] = vector.getX() * vector.getY() * (1.0D - cos) + vector.getZ() * sin;
    m[1][1] = vector.getY() * vector.getY() + (1.0D - vector.getY() * vector.getY()) * cos;
    m[1][2] = vector.getY() * vector.getZ() * (1.0D - cos) - vector.getX() * sin;
    m[1][3] = 0.0D;

    m[2][0] = vector.getX() * vector.getZ() * (1.0D - cos) - vector.getY() * sin;
    m[2][1] = vector.getY() * vector.getZ() * (1.0D - cos) + vector.getX() * sin;
    m[2][2] = vector.getZ() * vector.getZ() + (1.0D - vector.getZ() * vector.getZ()) * cos;
    m[2][3] = 0.0D;

    m[3][0] = 0.0D;
    m[3][1] = 0.0D;
    m[3][2] = 0.0D;
    m[3][3] = 1.0D;

    final Matrix matrix = Matrix.newInstance(m);
    final Matrix matrixInversed = matrix.transpose();

    return newInstance(matrix, matrixInversed);
  }
Exemple #3
0
  void Update(Vector target, List<Enemy> enem, int i, Background BACK) {

    if (State == 2 || State == 3) {

      timeRecoil++;

      if (timeRecoil >= timerRecoil) {
        State = 0;
        timeRecoil = 0;
        recoilVel = new Vector(0, 0);
      }
    }

    if (State != 1) {
      Vector deltaPos = new Vector(0, 0);
      if (State == 2 || State == 3) {
        deltaPos = recoilVel;
      } else {
        vel = new Vector(target.x - pos.x, target.y - pos.y);
        vel.normalize();
        deltaPos = new Vector(vel.x * speed, vel.y * speed);
      }

      for (int k = 0; k < enem.size(); k++) {
        if (k != i) {
          Vector4 A = collisions.Coll(this, enem.get(k));
          if (A.B < 0) {
            Vector proj = deltaPos.proj(A.A);
            Vector perp = deltaPos.proj(new Vector(-A.A.y, A.A.x));
            if (proj.SizeSQ() > A.B * A.B) {
              Vector M = new Vector(A.A.x * A.B, A.A.y * A.B);
              if (M.dot(proj) < 0) {
                deltaPos = new Vector(perp.x, perp.y);
              } else {
                deltaPos = new Vector(A.A.x * A.B + perp.x, A.A.y * A.B + perp.y);
              }
            }
          }
        }
      }
      deltaPos.normalize();
      vel = deltaPos;

      this.UpdatePos(BACK);
      if (State != 2 && State != 3) {
        angle = Math.atan2(vel.y, vel.x);
        angle = (angle * 360) / (2 * Math.PI);
      }
      this.animate();
    } else if (State == 1) {
      if (AttackTimer >= ATTACKT) {
        State = 0;
        AttackTimer = 0;
      } else {
        AttackTimer++;
      }
    }
  }
 private void calculateReflectionVector() {
   r =
       Vector.add(
           l,
           Vector.multiplyVector(
                   n, (2 * Vector.dotProduct(l, n) / (Math.pow(n.getMagnitude(), 2))))
               .neg());
   r.normalize();
 }
Exemple #5
0
  // This method is wrong, but yields an OK approximation.
  public Coordinates move(Coordinates start, Vector direction, double distance) {
    Vector v = new Vector(direction);
    v.normalize();
    v.scale(distance);
    Coordinates retval = new Coordinates(start.u, start.v);
    retval.u += v.components[0];
    retval.v += v.components[1];

    return retval;
  }
 public void testNormalize() throws Exception {
   Vector val = test.normalize();
   double mag = Math.sqrt(1.1 * 1.1 + 2.2 * 2.2 + 3.3 * 3.3);
   for (int i = 0; i < test.size(); i++) {
     if (i % 2 == 0) {
       assertEquals("get [" + i + ']', 0.0, val.get(i));
     } else {
       assertEquals("dot", values[i / 2] / mag, val.get(i));
     }
   }
 }
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    int x = (int) event.getX();
    int y = (int) event.getY();
    int[] colors = new int[] {Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW};
    // color = colors[new Random().nextInt(colors.length)];
    // radius = new Random().nextInt(30) + 50;

    // 引力
    Vector touch = new Vector(x, y);
    acc = Vector.sub(touch, loca);
    acc.normalize();
    acc.mult(15.0f);

    return super.onTouchEvent(event);
  }
  @Override
  public final Vector getNormal() {
    if (normal != null) {
      return normal;
    } else if (vectors.size() < 3) {
      return null;
    } else {
      Vector vector1 = vectors.get(0).copy();
      Vector vector2 = vectors.get(1);
      Vector vector3 = vectors.get(2).copy();
      Vector vector4;

      vector1.subtract(vector2);
      vector3.subtract(vector2);
      vector4 = Vector.toCrossProduct(vector3, vector1);
      vector4.normalize();

      return vector4;
    }
  }
  public static Transform lookAt(final Point source, final Point target, final Vector up) {
    final double[][] m = new double[4][4];

    m[0][3] = source.getX();
    m[1][3] = source.getY();
    m[2][3] = source.getZ();
    m[3][3] = 1.0D;

    final Vector direction = Vector.copyAndSubtract(target, source).normalize();
    final Vector vector0 = Vector.copyAndNormalize(up);
    final Vector vector1 = Vector.toCrossProduct(vector0, direction);

    if (vector1.length() == 0.0D) {
      return newInstance();
    }

    final Vector vector2 = vector1.normalize();
    final Vector vector3 = Vector.toCrossProduct(direction, vector2);

    m[0][0] = vector2.getX();
    m[1][0] = vector2.getY();
    m[2][0] = vector2.getZ();
    m[3][0] = 0.0D;
    m[0][1] = vector3.getX();
    m[1][1] = vector3.getY();
    m[2][1] = vector3.getZ();
    m[3][1] = 0.0D;
    m[0][2] = direction.getX();
    m[1][2] = direction.getY();
    m[2][2] = direction.getZ();
    m[3][2] = 0.0D;

    final Matrix matrix = Matrix.newInstance(m);

    return newInstance(matrix.inverse(), matrix);
  }
Exemple #10
0
  public void display(GLAutoDrawable drawable) {
    update();

    GL2 gl = drawable.getGL().getGL2();
    // Clear Color Buffer, Depth Buffer
    gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);

    Matrix TmpMatrix = new Matrix(); // Temporary MATRIX Structure ( NEW )
    Vector TmpVector = new Vector(), TmpNormal = new Vector(); // Temporary
    // VECTOR
    // Structures
    // ( NEW )

    gl.glLoadIdentity(); // Reset The Matrix

    if (outlineSmooth) { // Check To See If We Want Anti-Aliased Lines ( NEW
      // )
      gl.glHint(GL2.GL_LINE_SMOOTH_HINT, GL2.GL_NICEST); // Use The Good
      // Calculations
      // ( NEW )
      gl.glEnable(GL2.GL_LINE_SMOOTH); // Enable Anti-Aliasing ( NEW )
    } else
      // We Don't Want Smooth Lines ( NEW )
      gl.glDisable(GL2.GL_LINE_SMOOTH); // Disable Anti-Aliasing ( NEW )

    gl.glTranslatef(0.0f, 0.0f, -2.0f); // Move 2 Units Away From The Screen
    // ( NEW )
    gl.glRotatef(modelAngle, 0.0f, 1.0f, 0.0f); // Rotate The Model On It's
    // Y-Axis ( NEW )

    gl.glGetFloatv(GLMatrixFunc.GL_MODELVIEW_MATRIX, TmpMatrix.Data, 0); // Get
    // The
    // Generated
    // Matrix
    // (
    // NEW
    // )

    // Cel-Shading Code //
    gl.glEnable(GL2.GL_TEXTURE_1D); // Enable 1D Texturing ( NEW )
    gl.glBindTexture(GL2.GL_TEXTURE_1D, shaderTexture[0]); // Bind Our
    // Texture ( NEW
    // )
    gl.glColor3f(1.0f, 1.0f, 1.0f); // Set The Color Of The Model ( NEW )

    gl.glBegin(GL2.GL_TRIANGLES); // Tell OpenGL That We're Drawing
    // Triangles
    // Loop Through Each Polygon
    for (int i = 0; i < polyNum; i++)
      // Loop Through Each Vertex
      for (int j = 0; j < 3; j++) {

        // Fill Up The TmpNormal Structure With
        TmpNormal.X = polyData[i].Verts[j].Nor.X;
        // The Current Vertices' Normal Values
        TmpNormal.Y = polyData[i].Verts[j].Nor.Y;
        TmpNormal.Z = polyData[i].Verts[j].Nor.Z;
        // Rotate This By The Matrix
        TmpMatrix.rotateVector(TmpNormal, TmpVector);
        // Normalize The New Normal
        TmpVector.normalize();

        // Calculate The Shade Value
        float TmpShade = Vector.dotProduct(TmpVector, lightAngle);

        // Clamp The Value to 0 If Negative ( NEW )
        if (TmpShade < 0.0f) {
          TmpShade = 0.0f;
        }
        // Set The Texture Co-ordinate As The Shade Value
        gl.glTexCoord1f(TmpShade);
        // Send The Vertex Position
        gl.glVertex3f(
            polyData[i].Verts[j].Pos.X, polyData[i].Verts[j].Pos.Y, polyData[i].Verts[j].Pos.Z);
      }

    gl.glEnd(); // Tell OpenGL To Finish Drawing
    gl.glDisable(GL2.GL_TEXTURE_1D); // Disable 1D Textures ( NEW )

    // Outline Code
    // Check To See If We Want To Draw The Outline
    if (outlineDraw) {
      // Enable Blending
      gl.glEnable(GL2.GL_BLEND);
      // Set The Blend Mode
      gl.glBlendFunc(GL2.GL_SRC_ALPHA, GL2.GL_ONE_MINUS_SRC_ALPHA);

      // Draw Backfacing Polygons As Wireframes
      gl.glPolygonMode(GL2.GL_BACK, GL2.GL_LINE);
      // Set The Line Width
      gl.glLineWidth(outlineWidth);
      // Don't Draw Any Front-Facing Polygons
      gl.glCullFace(GL2.GL_FRONT);

      // Change The Depth Mode
      gl.glDepthFunc(GL2.GL_LEQUAL);
      // Set The Outline Color
      gl.glColor3fv(outlineColor, 0);

      // Tell OpenGL What We Want To Draw
      gl.glBegin(GL2.GL_TRIANGLES);

      // Loop Through Each Polygon
      for (int i = 0; i < polyNum; i++) {

        // Loop Through Each Vertex
        for (int j = 0; j < 3; j++) {

          // Send The Vertex Position
          gl.glVertex3f(
              polyData[i].Verts[j].Pos.X, polyData[i].Verts[j].Pos.Y, polyData[i].Verts[j].Pos.Z);
        }
      }
      gl.glEnd(); // Tell OpenGL We've Finished
      // Reset The Depth-Testing Mode
      gl.glDepthFunc(GL2.GL_LESS);
      // Reset The Face To Be Culled
      gl.glCullFace(GL2.GL_BACK);
      // Reset Back-Facing Polygon Drawing Mode
      gl.glPolygonMode(GL2.GL_BACK, GL2.GL_FILL);

      // Disable Blending
      gl.glDisable(GL2.GL_BLEND);
    }

    // Check To See If Rotation Is Enabled
    if (modelRotate) {
      // Update Angle Based On The Clock
      modelAngle += .2f;
    }
  }
Exemple #11
0
  public void init(GLAutoDrawable drawable) {
    GL2 gl = drawable.getGL().getGL2();

    // Storage for the 96 Shader Values ( NEW )
    FloatBuffer shaderData = GLBuffers.newDirectFloatBuffer(96);

    // Start Of User Initialization
    // Really Nice perspective calculations Light Grey Background
    gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);
    gl.glClearColor(0.7f, 0.7f, 0.7f, 0.0f);
    // Depth Buffer Setup
    gl.glClearDepth(1.0f);

    // Enable Depth Testing
    gl.glEnable(GL2.GL_DEPTH_TEST);
    // The Type Of Depth Test To Do
    gl.glDepthFunc(GL2.GL_LESS);

    // Enables Smooth Color Shading ( NEW )
    gl.glShadeModel(GL2.GL_SMOOTH);
    // Initially Disable Line Smoothing ( NEW )
    gl.glDisable(GL2.GL_LINE_SMOOTH);

    // Enable OpenGL Face Culling ( NEW )
    gl.glEnable(GL2.GL_CULL_FACE);

    // Disable OpenGL Lighting ( NEW )
    gl.glDisable(GL2.GL_LIGHTING);

    StringBuffer readShaderData = new StringBuffer();

    try {
      InputStream inputStream =
          ResourceRetriever.getResourceAsStream("demos/data/models/Shader.txt");
      int info;
      while ((info = inputStream.read()) != -1) readShaderData.append((char) info);
      inputStream.close();
    } catch (IOException e) {
      e.printStackTrace();
      throw new RuntimeException(e);
    }

    StringTokenizer tokenizer = new StringTokenizer(readShaderData.toString());

    // Loop Though The 32 Greyscale Values
    while (tokenizer.hasMoreTokens()) {
      float value = Float.parseFloat(tokenizer.nextToken());
      shaderData.put(value);
      shaderData.put(value);
      shaderData.put(value);
    }
    shaderData.flip();

    gl.glGenTextures(1, shaderTexture, 0); // Get A Free Texture ID ( NEW )
    gl.glBindTexture(GL2.GL_TEXTURE_1D, shaderTexture[0]); // Bind This
    // Texture. From
    // Now On It
    // Will Be 1D
    // For Crying Out Loud Don't Let OpenGL Use Bi/Trilinear Filtering!
    gl.glTexParameteri(GL2.GL_TEXTURE_1D, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_NEAREST);
    gl.glTexParameteri(GL2.GL_TEXTURE_1D, GL2.GL_TEXTURE_MIN_FILTER, GL2.GL_NEAREST);

    gl.glTexImage1D(
        GL2.GL_TEXTURE_1D, 0, GL2.GL_RGB, 32, 0, GL2.GL_RGB, GL2.GL_FLOAT, shaderData); // Upload

    // Set The X Direction
    lightAngle.X = 0.0f;
    // Set The Y Direction
    lightAngle.Y = 0.0f;
    // Set The Z Direction
    lightAngle.Z = 1.0f;
    lightAngle.normalize();

    try {
      // Return The Value Of ReadMesh
      readMesh();
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
 private void normalizeVectors() {
   l.normalize();
   v.normalize();
   n.normalize();
 }
  @Override
  public final BoundingBox getBounds() {
    BoundingBox boundingBox = BoundingBox.newInstance();

    double minimum = Double.MAX_VALUE;

    Vector u = Vector.newInstance(0.0D, 0.0D, 0.0D);
    Vector v;
    Vector d = Vector.newInstance(0.0D, 0.0D, 0.0D);

    int size = vectors.size();

    for (int i = 0; i < size; i++) {
      Vector vector1 = vectors.get((i + 1) % size);
      Vector vector2 = vectors.get(i);

      u.set(vector1);
      u.subtract(vector2);
      u.normalize();

      v = Vector.toCrossProduct(getNormal(), u);
      v.normalize();

      double uMaximum = 0.0D;
      double uMinimum = 0.0D;
      double vMaximum = 0.0D;
      double vMinimum = 0.0D;

      for (int j = 0; j < size; j++) {
        if (i != j) {
          Vector vector3 = vectors.get(j);

          d.set(vector3);
          d.subtract(vector2);

          double uLength = d.getDotProduct(u);
          double vLength = d.getDotProduct(v);

          uMaximum = Math.max(uLength, uMaximum);
          uMinimum = Math.min(uLength, uMinimum);
          vMaximum = Math.max(vLength, vMaximum);
          vMinimum = Math.min(vLength, vMinimum);
        }
      }

      double current = (uMaximum - uMinimum) * (vMaximum - vMinimum);

      if (current < minimum) {
        minimum = current;

        Vector origin = boundingBox.getOrigin();

        origin.set(vector2);

        d.set(u);
        d.multiply(uMinimum);

        origin.add(d);

        d.set(v);
        d.multiply(vMinimum);

        origin.add(d);

        boundingBox.getU().set(u);
        boundingBox.getV().set(v);
        boundingBox.setHeight(vMaximum - vMinimum);
        boundingBox.setWidth(uMaximum - uMinimum);
      }
    }

    return boundingBox;
  }