Move Object Along Perpendicular Vector

Hi all,



This one's a bit tricky to explain, so a picture will have to speak a thousand words :slight_smile:  In any case, what I'm trying to do is have the object move to the left or right of the direction the camera is facing.  So instead of moving in relation to (0,0,-1), it would be something like (-.421, 0, -.9) (That would be in the first quadrant)



I get the angle between the camera's direction and straight ahead, as well as convert from radians to degrees, by:

float angleToStraightAhead = (float)(gameState.getCamera().getDirection().angleBetween(new Vector3f(0,0,-1))*57.2957795);



I'm kind of stumped as to what to do from here though.  I spent a good chuck of time with a pencil and paper today but came up empty :(

So in this picture, the blue line is the angle from the camera to the object, and the grey line is the line that I would like to move along

well i dont know exactly how to solve your problem, however, if you were going to be using that angle (35 degrees) it would technically be 125 degrees because 35 degrees is in the first quadrant



one thing you could do is have an invisible box which faces the camera which your graphical object attaches to, and then all you would need to do is to move on a relative Vector3f (meaning the way the object is facing is the z, rather than obsolute which is the worlds Z coordinate) and then you could move the object + or - on the relative X axis. But i dont know if JME does relative, I have never tried.

So essentially you want to move toward the line that is perpendicular to the vector between the camera and the object and the up vector.



First find the vector from camera to object


camToObj = obj.getLocation().subtract(cam.getLocation());



Now find vector perpendicular to it. Use the camera up vector to find it.

moveDir = camToObj.cross( Vector3f.UNIT_X );



Now choose the one that faces the camera direction

float faces1 = moveDir.dot(cam.getDirection());
if (faces1 <= 0)
    return moveDir.negate();
else
    return moveDir



Thanks Eggsworth and Momoko…



@Momoko_Fan, so now what would I do with moveDir?  That seems to be the direction I want to move the object in… how would I use that in the translation of the object?

You add it as velocity object's translation. This is something you should know from physics class though…

E.g


Vector3f pos = obj.getLocalTranslation();
Vector3f velocity = moveDir.mult(tpf); // time per frame given in update methods
pos = pos.add(velocity);
obj.setLocalTranslation(pos);

Momoko_Fan said:

You add it as velocity object's translation. This is something you should know from physics class though..
E.g


Vector3f pos = obj.getLocalTranslation();
Vector3f velocity = moveDir.mult(tpf); // time per frame given in update methods
pos = pos.add(velocity);
obj.setLocalTranslation(pos);




oh of course, thanks!  BTW, I slept throuogh most of Physics I, definitely enjoyed Physics II though, 'twas uber awesome :D

Hi,

is there a way to move the object with a constant speed? With this code the Object movementspeed is relative to the distance.

Thanks Edregol