数学java处理arcball四元数trig

dgiusagp  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(313)

我正在修改一个arcball类,使其在每次调用rollforward()时旋转1度。我阅读代码有困难,但我认为我需要编写一个替代xy\u to\u sphere()的方法,其中;设点1=v1,点2=v2,这样

pi/180 = (v1.x * v2.x + v1.y * v2.y + v1.z * v2.z) / (abs(v1.x) * abs(v2.x) + abs(v1.y) * abs(v2.y) + abs(v1.z) * abs(v2.z))


<-更新编辑->
我试着简单地添加一个Angular ((sin(pi/180)*半径)转换成y轴(在前滚()中)

v_drag = XY_to_sphere(center_x, center_y - ((sin(PI/180) * radius))/2);

在100次滚动后,它会偏离几度(这可能是浮点舍入错误,我会尝试找到它,但我必须学习一些东西)
(新问题)当它达到完全旋转时,它消失了大约1度,我不知道为什么。
我不确定我最初的想法,上面的解决方案是否会更好
现在我要尝试设置它,使它复位后,它达到360度,以规避浮点错误和我消失的错误
</-更新编辑->
弧球类

// Ariel and V3ga's arcball class with a couple tiny mods by Robert Hodgin

class Arcball {
  float center_x, center_y, radius;
  Vec3 v_down, v_drag;
  Quat q_now, q_down, q_drag;
  Vec3[] axisSet;
  int axis;
  float mxv, myv;
  float x, y;

  Arcball(float center_x, float center_y, float radius){
    this.center_x = center_x;
    this.center_y = center_y;
    this.radius = radius;

    v_down = new Vec3();
    v_drag = new Vec3();

    q_now = new Quat();
    q_down = new Quat();
    q_drag = new Quat();

    axisSet = new Vec3[] {new Vec3(1.0f, 0.0f, 0.0f), new Vec3(0.0f, 1.0f, 0.0f), new Vec3(0.0f, 0.0f, 1.0f)};
    axis = -1;  // no constraints...    
  }

  void rollforward(){
    q_down.set(q_now);
    v_down = XY_to_sphere(center_x, center_y);
    q_down.set(q_now);
    q_drag.reset();

    v_drag = XY_to_sphere(center_x, center_y - 1);
    q_drag.set(Vec3.dot(v_down, v_drag), Vec3.cross(v_down, v_drag)); 
  }

/*
  void mousePressed(){
    v_down = XY_to_sphere(mouseX, mouseY);   
    q_down.set(q_now);
    q_drag.reset();
  }

  void mouseDragged(){
    v_drag = XY_to_sphere(mouseX, mouseY);
    q_drag.set(Vec3.dot(v_down, v_drag), Vec3.cross(v_down, v_drag));
  }

* /

  void run(){
    q_now = Quat.mul(q_drag, q_down);
    applyQuat2Matrix(q_now);

    x += mxv;
    y += myv;
    mxv -= mxv * .01;
    myv -= myv * .01;
  }

  Vec3 XY_to_sphere(float x, float y){
    Vec3 v = new Vec3();
    v.x = (x - center_x) / radius;
    v.y = (y - center_y) / radius;

    float mag = v.x * v.x + v.y * v.y;
    if (mag > 1.0f){
      v.normalize();
    } else {
      v.z = sqrt(1.0f - mag);
    }

    return (axis == -1) ? v : constrain_vector(v, axisSet[axis]);
  }

  Vec3 constrain_vector(Vec3 vector, Vec3 axis){
    Vec3 res = new Vec3();
    res.sub(vector, Vec3.mul(axis, Vec3.dot(axis, vector)));
    res.normalize();
    return res;
  }

  void applyQuat2Matrix(Quat q){
    // instead of transforming q into a matrix and applying it...

    float[] aa = q.getValue();
    rotate(aa[0], aa[1], aa[2], aa[3]);
  }
}

static class Vec3{
  float x, y, z;

  Vec3(){
  }

  Vec3(float x, float y, float z){
    this.x = x;
    this.y = y;
    this.z = z;
  }

  void normalize(){
    float length = length();
    x /= length;
    y /= length;
    z /= length;
  }

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

  static Vec3 cross(Vec3 v1, Vec3 v2){
    Vec3 res = new Vec3();
    res.x = v1.y * v2.z - v1.z * v2.y;
    res.y = v1.z * v2.x - v1.x * v2.z;
    res.z = v1.x * v2.y - v1.y * v2.x;
    return res;
  }

  static float dot(Vec3 v1, Vec3 v2){
    return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;    // cos(1) = (v1.x * v2.x + v1.y * v2.y + v1.z * v2.z) / (abs(v1.x) * abs(v2.x) + abs(v1.y) * abs(v2.y) + abs(v1.z) * abs(v2.z))
  }

  static Vec3 mul(Vec3 v, float d){
    Vec3 res = new Vec3();
    res.x = v.x * d;
    res.y = v.y * d;
    res.z = v.z * d;
    return res;
  }

  void sub(Vec3 v1, Vec3 v2){
    x = v1.x - v2.x;
    y = v1.y - v2.y;
    z = v1.z - v2.z;
  }
}

static class Quat{
  float w, x, y, z;

  Quat(){
    reset();
  }

  Quat(float w, float x, float y, float z){
    this.w = w;
    this.x = x;
    this.y = y;
    this.z = z;
  }

  void reset(){
    w = 1.0f;
    x = 0.0f;
    y = 0.0f;
    z = 0.0f;
  }

  void set(float w, Vec3 v){
    this.w = w;
    x = v.x;
    y = v.y;
    z = v.z;
  }

  void set(Quat q){
    w = q.w;
    x = q.x;
    y = q.y;
    z = q.z;
  }

  static Quat mul(Quat q1, Quat q2){
    Quat res = new Quat();
    res.w = q1.w * q2.w - q1.x * q2.x - q1.y * q2.y - q1.z * q2.z;
    res.x = q1.w * q2.x + q1.x * q2.w + q1.y * q2.z - q1.z * q2.y;
    res.y = q1.w * q2.y + q1.y * q2.w + q1.z * q2.x - q1.x * q2.z;
    res.z = q1.w * q2.z + q1.z * q2.w + q1.x * q2.y - q1.y * q2.x;
    return res;
  }

  float[] getValue(){
    // transforming this quat into an angle and an axis vector...

    float[] res = new float[4];

    float sa = (float) Math.sqrt(1.0f - w * w);
    if (sa < EPSILON){
      sa = 1.0f;
    }

    res[0] = (float) Math.acos(w) * 2.0f;
    res[1] = x / sa;
    res[2] = y / sa;
    res[3] = z / sa;
    return res;
  }
}

我的代码,使用w键滚动立方体//arcball=newarcball(width/2,height/21115);非常接近于每个弧球1度;

Arcball arcball;

//framecount
int fcount, lastm;
float frate;
int fint = 3;

//int test_count = 0;

boolean[] keys = new boolean[13];
    final int w = 0;
    final int s = 1;
    final int a = 2;
    final int d = 3;
    final int q = 4;
    final int e = 5;
    final int r = 6;
    final int f = 7;
    final int z = 8;
    final int x = 9;
    final int t = 10;
    final int g = 11;
    final int m = 12;

boolean fullscreen = false;
int aa = 8; //2,4,8

void settings() {
  size(900, 700, P3D); 
  screensizex = 900;
  screensizey = 700;
}

  smooth(aa);
}

void setup() {
  frameRate(60);
  noStroke();
  keys[m] = false;

  arcball = new Arcball(width/2, height/2, 115);  
}

void draw() {
  lights();
  background(255,160,122);

  if(keys[w]) { 
    arcball.rollforward(); 
   // test_count = test_count + 1; 
   // print(" " + test_count);  // needs to be 360 at 1 full rotation
  }

  translate(width/2, height/2-100, 0);
  box(50);

  translate(0, 200, 0);
  arcball.run();
  box(50);  

}

void keyPressed() {
  switch(key) {
    case 97: 
        keys[a] = true;
        break;
    case 100: 
        keys[d] = true;
        break;      
    case 101: 
        keys[e] = true;
        break; 
    case 102: 
        keys[f] = true;
        break;     
    case 103: 
        keys[g] = true;
        break;
    case 109: 
        if(keys[m] == true) keys[m] = false; 
        else keys[m] = true;
        break;
    case 113:
        keys[q] = true;
        break;
    case 114:
        keys[r] = true;
        break;
    case 115: 
        keys[s] = true;
        break;
    case 116: 
        keys[t] = true;
        break;
    case 119: 
        keys[w] = true;
        break;
    case 120: 
        keys[x] = true;
        break;
    case 122: 
        keys[z] = true;
        break;
    } 
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题