It depends on the desired freedom of movement for the object what you would map the new ray to. Do you want to map it to a sphere (equal distance to the camera), a plane facing the camera, the floor or maybe even another model? You would only need to cast a ray in the last case.
@I3DB I can’t find how relativeHandPosition is calculated in your example. That might contain the whole problem I have, I don’t know how to calculate how far the object should move so that it stays under the finger.
@Modanung In my case, I just want to move a box along a single axis, and keep it under the finger the whole time. The camera distance and position can change, so I need to take that into account for the translate/position difference calculation. But it can be assumed that the camera won’t change while dragging.
That comes from the event generator. This depends upon the device you’re using. You tap into a suitable event and handle it. If you’re using a touch screen device, you will get events that indicate the start and location (in x,y coordinates) and likely get updates as the finger slides. You’ll have to add the z coordinate if you want 3D movement, whereas the hololens provides a Vector3 already. This is no different than handling mouse click events, which indicate the position of mouse at click time.
Well, I figured out a solution.
I create a Plane for possible movement at the point of the first/previous touch raycast hit and then find a point on the same plane for the subsequent touch using this code which I found here https://github.com/urho3d/Urho3D/issues/1504 :
Vector3 result = cameraRay.origin_ + cameraRay.direction_ * dist;
Then I subtract the coordinate(s) for the first point from this point to get the translate amount.
I am disappointed that such a common process still must be done manually, unless there’s something easier that I am missing.
Something like this should work:
float movementAlongAxis = (ray.origin_ + ray.HitDistance( Plane(objectPos, ray.origin_.Orthogonalize(movementAxis)) ) * ray.direction_).DistanceToPlane(objectPos, movementAxis);
@Modanung I don’t think that would work. Movement shift is not in there. Both your points are objectPos. The DistanceToPlane would always be zero. Also, you’re missing a closing parentheses.
It projects the ray onto a plane that lies along the movement axis and faces the camera after which the distance from that point to a plane - perpendicular to the axis and centered at the object - is calculated. This gives the distance from the object’s center to the visually nearest point on the axis from the mouse cursor.
It may need some modifications, but that’s the lines I believe you should think along.