rotate mesh towards a certain direction
in
Contributed Library Questions
•
2 years ago
hello everyone,
I am trying to rotate a mesh towards the center of the screen so that it 'looks' at the center. This needs 3 angles to rotate the mesh in a x, y and z direction. The problem is, i dont know how to retrieve those values. At this moment i am in a fight with vectors, and trying to understand the dot and cross product, but with no succes...
to run the sketch, you need the peasycam library and the hemesh library
I am trying to rotate a mesh towards the center of the screen so that it 'looks' at the center. This needs 3 angles to rotate the mesh in a x, y and z direction. The problem is, i dont know how to retrieve those values. At this moment i am in a fight with vectors, and trying to understand the dot and cross product, but with no succes...
to run the sketch, you need the peasycam library and the hemesh library
- import peasy.*;
import wblut.math.*;
import wblut.hemesh.modifiers.*;
import wblut.hemesh.kdtree.*;
import wblut.hemesh.creators.*;
import wblut.hemesh.tools.*;
import wblut.hemesh.*;
import wblut.hemesh.subdividors.*;
import wblut.geom.*;
import processing.opengl.*;
import javax.media.opengl.*;
// cam
PeasyCam cam;
PVector center, angle, child;
HE_Mesh m;
PVector direction = new PVector();
void setup() {
size(800,600, OPENGL);
background(255);
// camera
cam = new PeasyCam(this, 100);
cam.setMinimumDistance(1);
cam.setMaximumDistance(500);
center = new PVector(0,0,0);
angle = new PVector(1,0.1,0.3);
angle.normalize();
child = new PVector();
child.set(angle);
child.mult(100);
//angle from child to center
PVector angleToCenter = new PVector();
angleToCenter.set(angle);
angleToCenter.mult(-1);
float d = 10;
float h = 50;
float w = 70;
HEC_Creator c = new HEC_Box(this).setDepth(d).setHeight(h).setWidth(w).setWidthSegments(10).setHeightSegments(10).setDepthSegments(10);
m = new HE_Mesh(c);
double[] limits = m.limits();
PVector minBoundingBox = new PVector( (float) limits[0], (float) limits[1], (float) limits[2]);
PVector maxBoundingBox = new PVector( (float) limits[3], (float) limits[4], (float) limits[5]);
float volumeX = calculateDifferences( minBoundingBox.x, maxBoundingBox.x);
float volumeY = calculateDifferences( minBoundingBox.y, maxBoundingBox.y);
float volumeZ = calculateDifferences( minBoundingBox.z, maxBoundingBox.z);
int direction_id = 0;
float[] volumes = {
volumeX, volumeY, volumeZ
};
for(int i = 0; i < volumes.length; i++) {
if(min(volumes) == volumes[i]) {
direction_id = i;
break;
}
}
switch(direction_id) {
case 0:
direction.set(1,0,0);
break;
case 1:
direction.set(0,1,0);
break;
case 2:
direction.set(0,0,1);
break;
}
direction.mult(30);
angle.mult(20);
WB_Point rotationCenter = new WB_Point(0,0,0);
WB_Vector axisDirection = new WB_Vector(1,0,0);
float rotationAngle = 0;
// horizontal axis = depth rotation
// m.rotateAboutAxis(rotationAngle, rotationCenter, axisDirection);
axisDirection.set(0,1,0);
rotationAngle = 0;
// vertical axis = horizontal rotation
// m.rotateAboutAxis(rotationAngle, rotationCenter, axisDirection);
axisDirection.set(0,0,1);
rotationAngle = 0;
// depth axis = vertial rotation
// m.rotateAboutAxis(rotationAngle, rotationCenter, axisDirection);
}
void draw() {
background(255);
stroke(0);
strokeWeight(1);
lights();
pushMatrix();
translate(center.x, center.y, center.z);
box(10);
strokeWeight(3);
stroke(255,0,0);
beginShape();
vertex(0,0,0);
vertex(angle.x, angle.y, angle.z);
endShape();
popMatrix();
strokeWeight(1);
pushMatrix();
translate(child.x, child.y, child.z);
stroke(0);
m.drawFaces();
strokeWeight(3);
stroke(255,0,0);
beginShape();
vertex(0,0,0);
vertex(direction.x, direction.y, direction.z);
endShape();
popMatrix();
}
1