Loading...
Logo
Processing Forum
Still trying to realize how to align an object to a vector.

Now I have implemented the method which uses the cross and dot products of the original and targer vector. The code below seems to work except for the rotate method doing strange thing. By replacing theta in line 37 with smaller numbers such as 0.001 and 0.01 I have found that the rotate() does not rotate ok. It also translates and resizes everything. If the 0.001 or 0.01 is in place in line 37, then I can only see the box when I move both points close to each other in the bottom left of the screen, which is reeeeely strange for me. When theta is in place, the box is almost too big to fit on the screen even at that case. The similar (in my mind) method of glRotatef() on contrary, does nothing at all. How to make the rotate() method behave as expected? (use 'qweasd' to move the bottom point and 'uiojkl' to move the upper one).

Copy code

  1. import processing.opengl.*;

  2. PVector v0;      // always 0, 0, 1
  3. PVector v1;      // point 1
  4. PVector v2;      // point 2
  5. PVector v;      // resulting vector from v2-v1

  6. float speed = 4;

  7. void setup(){
  8.   size(800, 600, OPENGL);
  9.   v1 = new PVector(0, 500, 0);
  10.   v2 = new PVector(0,-500, 0);
  11.   v0 = new PVector(0, 0, 1);
  12. }

  13. void draw(){
  14.   background(255, 155, 50);
  15.    strokeWeight(1);
  16.   fill(255);
  17.  translate(width / 2, height / 2, -1000);
  18.  pushMatrix();
  19.  //center the scene
  20.  translate(v1.x + (v2.x - v1.x)/2, v1.y + (v2.y - v1.y)/2, v1.z + (v2.z - v1.z)/2);

  21. v = new PVector (v2.x - v1.x, v2.y - v1.y, v2.z - v1.z);

  22. PVector cross = v0.cross(v);
  23. PVector vn = new PVector(v.x, v.y, v.z);
  24. vn.normalize();

  25. float theta = acos(v0.dot(vn));

  26. //rotation
  27. rotate(theta, cross.x, cross.y, cross.z);
  28. //((PGraphicsOpenGL)g).gl.glRotatef(theta, cross.x, cross.y, cross.z);


  29. // scale(10, 10, 10);
  30.  box(100);
  31.  stroke(255,0,0);
  32.  strokeWeight(3);
  33.  line(0, 0, 0, 200, 0, 0);
  34.   stroke(0, 255,0);
  35.  line(0, 0, 0, 0, 200, 0);
  36.  stroke(0,0,255);
  37.  line(0, 0, 0, 0, 0, 200);
  38.  
  39.  stroke(155,20,255);
  40.  line(0, 0, 0, vn.x * 100, vn.y * 100, vn.z * 100);
  41.  
  42.  println("vector : " + v.toString());
  43.  
  44.  
  45.  popMatrix();
  46.  
  47.  stroke(0);

  48.  
  49.  drawV(v1);
  50.  drawV(v2);
  51.  line(v1.x, v1.y, v1.z, v2.x, v2.y, v2.z);
  52.  controls();
  53.  fill(0);
  54. }

  55. void drawV(PVector v){
  56.   pushMatrix();
  57.   translate(v.x, v.y, v.z);
  58.   sphere(30);
  59.   popMatrix();
  60. }

  61. void controls(){
  62.   if(keyPressed){
  63.     switch(key){
  64.     //  one
  65.      case 'w':
  66.        v1.z -= speed;
  67.        break;
  68.      case 'a':
  69.        v1.x -= speed;
  70.        break;
  71.      case 's':
  72.        v1.z += speed;
  73.        break;
  74.      case 'd':
  75.        v1.x += speed;
  76.        break;
  77.      case 'q':
  78.        v1.y -= speed;
  79.        break;
  80.      case 'e':
  81.        v1.y += speed;
  82.        break;  
  83.        
  84.      //two
  85.    case 'i':
  86.      v2.z -= speed;
  87.      break;
  88.    case 'j':
  89.      v2.x -= speed;
  90.      break;
  91.    case 'k':
  92.      v2.z += speed;
  93.      break;
  94.    case 'l':
  95.      v2.x += speed;
  96.      break;
  97.    case 'u':
  98.      v2.y -= speed;
  99.      break;
  100.    case 'o':
  101.      v2.y += speed;
  102.      break;  
  103.      }
  104.   }
  105. }

Replies(2)

When you calculate theta try using  PVector.angleBetween() just to see if you get different results.

Usually the reference vector for this kind of rotations is the Up vector in your case your reference vector is v0 try setting v0 to (0, 1, 0) to see what happens.

This post may help 
Yes, I have tested your example and was able to adopt the technique! There are so many approaches to this problem that I got lost among them. Thankfully the one you posted in the other post works fine!

HOWEVER!!! It is very curious to me, why in my code, the box gets stretched like that. There are only translate and rotate transformations applied so no resize should be possible. I think, this is either a bug or a feature that I do not understand yet.