[UNRESOLVED] Predicting a 3D Point given a rotation

Started by
18 comments, last by BlackSeeds 15 years, 5 months ago
@BlackSeeds: I am running into problems trying to implement your code snippet. The "point" I am dragging is lagging behind my cursor unstably. I think you are using OpenGL, and I am using DirectX so some things are slightly different. DirectX uses a left hand coordinate system so I -= when you +='d.

Here is my source. cursor_x_drag is initially calculated using normal coordinates (0 to screen width, 0 to screen height) so I divide them 383.5 (since my viewport is 768x768) to get them into "3d" coordinates (-1.0 to 1.0 and all that wonderful stuff). Still something is wrong, probably in my implementation. Could you elaborate more? If you need any more information from me I'll gladly provide it.

	float increment_x = cursor_x_drag / 383.5f;	float increment_y = cursor_y_drag / 383.5f;	double rotation_x = D3DXToRadian(object_rotation_x);	double rotation_y = D3DXToRadian(object_rotation_y);	object.vertice[1].position_x -= float((cos(rotation_y) * increment_x) + (-(sin(rotation_x) * sin(rotation_y)) * increment_y));
Advertisement
Quote:
in perspective view as well as most other applications. They just don't allow xyz to be modified simultaneously. I just want to predict a single x y OR z, not all 3.


Well, with a mouse you can update two axis at the same time, but you can make your program to update just one axist at one time if you want.

For doing just the PAN effect I dont use any complex math, really, but maybe in my editor i am not really interested in move objects at exact milimetric positions.

Like BlackSeeds tould, you can use windgets (or push butons that can be On/Off to tell which axis you want to update); then you use the "drag" mouse method, mean you clik and hold a mouse button, at that moment you save current x,y mouse position, then use an "On mouse move" event and there you check the new mouse x,y position and calc the displacement from the initial position and update your vertex position in the selected axis using the displacement from the X or Y mouse coordinate; if i have selected to update XZ then mouse X displacement will update X vertice coordinate, and Y mouse displacement will update Z vertice coordinate

Like i said i dont use any complex math, i just define how much units in world coordinate the vertice will be updated for any mouse x,y displacement; low factors will drag the vertice slowly, high factors will drag faster.


Now, for me this work becouse i really know what is the current position in world coordinate for all vertices in my model so i can add any displacement i want to any vertice; BUT if you only know the original model vertices and to that you have done a lot translation and rotation a matrix and then you use that matrix to show in screen the resulted transformed model then the above aproach will not work.



@tpascal: I understand how mouse dragging works.
I still don't understand how to get the vertice to stay aligned to my cursor. It always lags behind.
I think that is what BlackSeeds was trying to show me. Alas I could not get his code sample to work. The vertice would always lag behind my cursor. This only occurs when the scene is rotated.
I get how to move a vertice based on mouse movement but I cannot get it to align to my mouse cursor if any rotations take place. This is the core problem I am facing. Also I have full access to the vertices original and transformed coordinates. Its again just getting its movement to align to my cursor that I cannot grasp.

Quote:
I get how to move a vertice based on mouse movement but I cannot get it to align to my mouse cursor if any rotations take place.


When you say "if any rotations take place." do you mean the camera was rotated to see the model from different angle or do you mean the model vertices was actually rotated?

Right now the only rotation that takes place is a world transformation. So yes, the rotation was used to see the vertices from another angle. The view matrix is static, and no "other" rotations are occurring.
hey dahos,

so did you manage to get the code sample i gave you working in any way, if the only problem is the lagging of the vertex behind the pointer we can add some stuff to resolve that.

the main thing is....Does the vertex move in the correct direction you are dragging the mouse? if so then were almost there

Basically what I do is take a vector from the camera position to the vertex and get the distance of this vector, you can then scale your movement by this distance.

so for instance:

position x += (the math i showed you earlier) * distance

so the further you are away the vertex will move larger distances, the closer you are the more accurate it will be. this may take a bit of tweaking to get it how you want.

If this is not the problem you are having then just let me know what issues you have and ill try and help.

yes my implementation is OpenGL, but looks like you negated correctly to get it working in the other coordinate system :)

also what are your variables cursor_x_drag I didn't quiet understand that bit.
Yes, the vertices move in the direction I am dragging. However they lag behind more and more based on the rotation used. More lag at 45rotation_y than say 15rotation_y.

I should point out that simply setting vertice's x & y to my cursor position produces a nearly identical effect. The sin/cos adjustments in your code snippet seemed to have little effect.

cursor_x_drag & cursor_y_drag are just variables that store the difference between your current and last cursor position. Updated every frame.

I will try your distance scaling method and see if it adjusts things appropriate. If it still causes problems I'll upload some more screenshots to avoid any confusion on what is happening.
what you need to do is move it on x,y and z at the same time.

if you consider at 0 degrees rotation on the x and y axis, +x mouse drag will cause the vertex to move only on the x axis, because of the math z and y will have 0 increment,

if you got to 90 degrees rotation on the y-axis the x position of the vertex will move by nothing because we are looking directly down the x axis and hence z will become the axis that we want to move there vertex on by dragging the mouse in the x axis of the screen.

now if we consider you are at 45 degrees rotated about the y axis, we want the vertex to effectively strafe the screen so here we move its position by 0.5 x and 0.5 z sop half and half.

it effectively works like spherical coordinates
BlackSeeds, I think you just solved my problem. I never even THOUGHT of adjusting the z coordinate with the x coordinate. That keeps my vertice *perfectly* lined up with the cursor. (I would assume by this logic, you translate using 2 axis while rotating by the 3rd, xz rotated by y, yz rotated by x, xy rotated by z). The resulting distance from the vertice to the camera is the distance you need to translate x by. Or rather where x *would* be if no translation had occured...or rather your mouse cursor's location.

I'm so excited...I knew this was going to be so simple it's silly. I'm going to do some more extensive testing to make sure this is fully working properly. I'll post back again if any other issues crop up.

Seriously, thank you for taking time out to explain this to me. It's been driving me completely insane for over a week now. Mostly because the majority of programs never need this kind of information, so very little information on the subject is available.
Quote:Original post by Dhaos
BlackSeeds, I think you just solved my problem. I never even THOUGHT of adjusting the z coordinate with the x coordinate. That keeps my vertice *perfectly* lined up with the cursor. (I would assume by this logic, you translate using 2 axis while rotating by the 3rd, xz rotated by y, yz rotated by x, xy rotated by z). The resulting distance from the vertice to the camera is the distance you need to translate x by. Or rather where x *would* be if no translation had occured...or rather your mouse cursor's location.

I'm so excited...I knew this was going to be so simple it's silly. I'm going to do some more extensive testing to make sure this is fully working properly. I'll post back again if any other issues crop up.

Seriously, thank you for taking time out to explain this to me. It's been driving me completely insane for over a week now. Mostly because the majority of programs never need this kind of information, so very little information on the subject is available.


excellent :) hope it works out for you, always glad to help someone out as on numerous occasions I have been helped on this forum.

you are right about translating 2 axis about the 3rds rotation. whats more, if you translate about all 3 in one go,

ie. translate x+z about y rotation, translate y about z rotation,

so now when you drag the cursor +x and +y on the screen the vertex will move precisely with the cursor. The only issue left is scaling the translation so that it lines up nicely with the cursor regardless of distance.

don't hesitate to post back if there any other issues that arise



This topic is closed to new replies.

Advertisement