Suggestions for incremental 3d rotation
in
Programming Questions
•
10 months ago
Hi all, I've been thinking about 3d rotation using the mouse - normally I've mapped the mouse x/y coordinates to rotation values. However, this means that when you click on the screen the model jumps to that rotation.
Instead, I wanted to click-and-drag to incrementally rotate, more like in a 3d modeling program. Here's my working version:
Instead, I wanted to click-and-drag to incrementally rotate, more like in a 3d modeling program. Here's my working version:
- int boxSize = 600; // size for 3d box
- float inc = 0.05; // increment to rotate during drag
- color orange = color(212, 124, 23); // fill color
- float rx, ry; // keep track of rotation x/y
- void setup() {
- size(500,500, OPENGL);
- strokeWeight(20);
-
- // initial rotation
- rx = radians(-25);
- ry = radians(45);
- }
- void draw() {
- background(0);
-
- // rotate from center
- translate(width/2, height/2, -boxSize*5);
- rotateX(rx);
- rotateY(ry);
-
- // box!
- stroke(0);
- fill(orange);
- box(boxSize);
-
- // directional lines
- pushMatrix();
- translate(-boxSize*2, boxSize/2, boxSize/2);
- stroke(255,0,0);
- line(0,0,0, boxSize, 0, 0);
- stroke(0,255,0);
- line(0,0,0, 0, -boxSize,0);
- stroke(0,0,255);
- line(0,0,0, 0,0,-boxSize);
- popMatrix();
- }
- // the magic happens here!
- void mouseDragged() {
-
- // normally we'd do something like this
- // ry = map(mouseX, 0,width, 0,TWO_PI);
- // rx = map(mouseY, 0,height, 0,TWO_PI);
-
- // drag in x direction, rotate y
- if (mouseX > pmouseX) {
- ry += inc;
- }
- else if (mouseX < pmouseX) {
- ry -= inc;
- }
-
- // ditto y
- if (mouseY > pmouseY) {
- rx -= inc;
- }
- else if (mouseY < pmouseY) {
- rx += inc;
- }
- }
1