We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hey!
I am trying to do object picking with rays in Processing. Everything works fine, except I cannot find the right 'up' vector of the camera. I am using Peasy library.
I could get the cameraUp vector by:
right = PVector.cross(view,new PVector(0,0,1));
then
cameraUp = PVector.cross(view, right);
However the problem is that when I am orbiting with the camera the up vector (0,0,1) gets rotated. I would like to find the up vector of the rotated coordinate system. Peasy lets you access the pitch, yaw and roll.
I tried doing it with:
up.x = cos(cam.getRotations()[0])*sin(cam.getRotations()[1]);
up.y = -cos(cam.getRotations()[1]);
up.z = sin(cam.getRotations()[0])*sin(cam.getRotations()[1]);
and a few similar versions, but I just can't get it to work. it might be due to peasy defining the axis differently.
Any hint on how I could solve this?
Any answer would be much appreciated:)
Answers
The up vector is normally defined as [0,1,0] in other words the y axis
The white rectangle represents the viewport when clicked in world space. After clicking I zoomed out so its visible. My problem is that as you can see if I only define an upvector according to the original coordinate system, then the rotation distorts what is considered 'up'
what information do you have?
camera position? look at position? right?
runnable examples will get you the best results.
I have cameraPosition, cameraLookAt. That part of my code works. What I have is I can already find my mouse position where I clicked in 3D world (and the viewport in 3D coordinates), when the cameras 'roll' is 0.
However when I rotate around the lookAt these vectors won't correspond.
I can also access: cam.getDirection[0] = rotation on the X-Axis cam.getDirection[1] = rotation on the Y-Axis cam.getDirection[2] = rotation on the Z-Axis
Bump up please
A runnable example will help.
I did initially think that your code snippet was a bit suspect - defining right in terms of view and 0, 0, 1 and then defining up in terms of view and right - but I guess that will work
Wait... Do you mean 0, 0, 1 or 0, 1, 0? Y is traditionally up.
(Ha, quark said the same thing 4 days ago)
:) I understand this already. The way my code is written (and how Peasy defines Z) is by (0,0,1).
However I would like to understand what the new 'up' is when the camera is rotated. In the end I would like to get the cameraUP, which is a vector perpendicular to both the view vector of the camera (the vector between position of the cam and where it looks) and the world up.
The camera gets however rotated, which means the camera also 'rolls' on the axis of the view vector. I would like to calculate what this roll is. This is not straight forwards as the library offers the rotations around the world axis but not on the view vector's axis.
If I understand correctly:)
A runnable example will still help 8)
What you can see at calcNewUp() is that I would like to get the new vector by calculating the rotation matrix from the roll, pitch and yaw rotations that peasy gives.
There are six cases, as I don't know which order peasy applies the rotations: X * Y then XY*Z or which order. Then I multiply the resulting matrix with the upvector (0,0,1) to get the new upvector. This means basically selecting the third column of the rotation matrix.
Unfortunately it doesn't work with any order.
"The function drawGrid() does not exist."
Sorry, forgot it, just posted it!
@szemy2 --
This is just brainstorming without deeply engaging your problem, but have you looked at using QueasyCam instead? I thought maybe it returned the frame-of-reference vectors you need.
I have not used QueasyCam before, but I downloaded it now that you suggested it. Maybe it is easier to get it working with Quesy, however I find Peasy more intuitive so I wouldn't want to switch. Also I used Peasy in nearly all my projects. Anyway: thank you for the suggestion.
I promise I will make a tutorial on this object picking thing or a library (that doesn't use off-screen coloured buffer like the picking library), once I figure this out :D
Great! Looking forward to what you share.
Understood about wanting to stick with the camera library you know.
In case it is helpful, here are the relevant parts of the QueasyCam README:
QueasyCam also makes its variables "pan" and "tilt" public.
ok, thanks for the code. but you might need to explain what i'm seeing and what you want to be seeing instead 8)
if i rotate a bit and click i get a dark red ray into the screen (which looks correct in that it's going directly away from me). and a white rectangle which i think is meant to be straight up and down in relation to the screen, but is often rotated around the dark red ray. i also get another dark red ray, attached to the origin, which is, i think, the up vector.
What you are seeing: the white rectangle is the canvas calculated in world space. The white cross is the position of the click on the canvas in world space.
The dark red is the X-Axis, the green the Y-Axis and the blue the Z-Axis. Firstly, you were right the Y-Axis is the 'up' in this case (the rotations are zero when we are looking straight down.) There is also a ray (I think this is what you asked about) that is simply a line from the camera position to where the pickingRay would intersect with the xy plane. The white rectangle gets rotated around the middle of the canvas not around this ray.
The canvas doesn't get calculated correctly if the camera is rotated (if it is only rotated around the X axis, then it works). If it was working correctly the white rectangle would touch the edges of the canvas and the white cross would be exactly below where you clicked with the mouse.
Also I started extracting the rotation according to the world matrix:
myMatrix contains 16 floats that represent the world matrix. I multiply this matrix by (0,-1,0,0), which means simply picking the second column of the rotation matrix and multiplying x, y and z values with -1.
Still doesn't work, but seems to behave a little better!
bump up please!
This is a very difficult question. You may need to bump many more times.
@szemy2 -- when you looked at the example of
getUp()
from QuesyCam that I already shared with you that accomplishes what you are trying to do, did the method work for you, or no? When you looked at the QuesyCam source code, was the source code that implements the method clear, or no?I'm trying to understand the specific nature of your continuing problem given that this seemed to answer your question.