We are about to switch to a new forum software. Until then we have removed the registration on this forum.
hi! Im writing a 3D sketch in which the user rotates the camera with peasyCam while left clicking and moving the mouse. The thing is that I want to move the objects while right click is pressed so that the user can drag the object across the screen's X and Y axis. Of course I know how to use mouseX and mouseY inputs to modify the translation but only across the 3D space coordinates as it shows on the GIF below:
example code of whats happening in the image:
import peasy.*;
import peasy.org.apache.commons.math.*;
import peasy.org.apache.commons.math.geometry.*;
PeasyCam cam;
float x=15;float y=15; float z=15;
float e;
void setup(){
size (700,700,P3D);
cam = new PeasyCam(this, 200);
cam.setRightDragHandler(null);
}
void draw(){
background(0);
pushMatrix();
translate(5, 5, 0);
fill(255);
stroke(255);
sphere(5);
popMatrix();
pushMatrix();
fill(255,0,0);
stroke(255,0,0);
translate(x, y, z);
sphere(5);
popMatrix();
stroke(0,0,255);
line(5,5,0,x,y,z);
//obvoiusly not working method
if(mousePressed && (mouseButton == RIGHT)){
x= x+(mouseX-pmouseX);
y= y+(mouseY-pmouseY);
}
}
void mouseWheel(MouseEvent event) {
e = event.getCount();
z=z+e;
println(e);
}
void mousePressed(){
if (mouseButton==RIGHT){
cam.setActive(false);
}
}
void mouseReleased(){
cam.setActive(true);
}
What I would need is to be able to drag the sphere only on the screens X/Y axis, at a fixed Z just like image below shows(simple simulation I made of the behaviour im looking for).
PeasyCam is for exploring the 3D space. The question might be difficult to undesrtand. The problem is about moving the object on the 3D world, using the screen/canvas 2D coordinates to make the object follow the cursor's movement. If the mouse goes to the left (x axis decreases), the object should move to the left on the screen, and not just on the worlds X axis. This is how the second example image behaves, but achieved by just simulating the 3D space with no actual rotations to the x,y,z axis.
I've been scratching my head with this thing and I cant seem to figure it out. I wouldn't have asked here otherwise. Thanks in advance guys.
Answers
Here is my attempt. First thing is to have a reference frame to be able to make comparisons and see things more clear. The program begin with an axis drawn,RGB for x,y,z. In this mode the cam is active. Then if you hit the 'g' key (for grid) you get a grid. The cam is disable. Notice that only in the grid state you can unlock the mouse. The mouse always starts lock in the grid state. If you hit m, you can toggle the mouse's state. The mouseX/mouseY dynamics changes the position of your sphere of interest and the sphere's position is mapped to the width and height of your sketch. Remember, you need to lock/unlock the mouse to be able to adjust x and y. Remark: Locking/unlocking the mouse's state in non-grid mode just does nothing.
You might consider checking the getRotations/getPosition from the peasycam's reference page. You could also use that information to map the 2D plane of your sketch into the 3D world set by the peasy cam. However, I think this solution below could be good but probably not sufficient.
Kf
//axisgrid gridaxis scene rotation
Thanks kf! your code makes it pretty easy to understand the problem in question . I've been trying to grasp the concept of rotations to use peasycam's getRotations() to implement a solution, but I haven't figured it out yet (I probably need to know a little bit more about rotation matricies). Thanks a lot for spending the time man, I'm going to use your code to experiment with getRotations(). The grid and the axis are really helpfull.
@kfrajer nice.
@mossmossmoss As far as i understand it, you need a Picking Library .
Some short, updated, example i did for myself a while ago to handle interactive 3D mouse actions. It handle picking and object transformations.
The code is (at some points) probably a bit complicated, some matrix stuff, lots of PShapes, etc. But it is kind of an universal solution and also quite efficient ... I tried to add some comments.
@T_D how does your picking example above compare to the Picking Library -- are they both essentially the same approach?