How to move an object along a vector
by Todd D. Degani · in Torque Game Engine · 05/10/2005 (11:57 am) · 7 replies
First off I am pretty new to 3d math. With that said this is what I am trying to accomplish.
In the world editor I would like to move a selected object in the direction of the key I press. For example, I press up arrow I want the object to move "forward", which would be the direction I am facing. How far it moves is based on the scales supplied in the world options.
I can get the object to move along any of the axis just fine by setting its position and applying it, but I would like the object to move in a direction relative to me.
A rough guess at this is that I would want to move the object the scale distance along the vector I am facing. I can get my forward vector with getForwardVector. What I dont understand is how I would apply this to the selected object.
Any help would be appreciated. Thanks.
In the world editor I would like to move a selected object in the direction of the key I press. For example, I press up arrow I want the object to move "forward", which would be the direction I am facing. How far it moves is based on the scales supplied in the world options.
I can get the object to move along any of the axis just fine by setting its position and applying it, but I would like the object to move in a direction relative to me.
A rough guess at this is that I would want to move the object the scale distance along the vector I am facing. I can get my forward vector with getForwardVector. What I dont understand is how I would apply this to the selected object.
Any help would be appreciated. Thanks.
#2
05/10/2005 (12:09 pm)
Correct. So if I push the forward key the object would move scale distance in the direction of my forward vector, or basically away from me. Back would have it move closer to me, etc.
#3
This should then be the vector you want to add to your object's position. Probably want to scale it too.
-s
05/10/2005 (9:58 pm)
Ah, this is where the euler matrix comes in handy if I do remember... lets see, ah yep- Here's my code for finding cardinal directions relative to getForwardVector%yAngle=0;
%xAngle=0;
%zAngle=mDegToRad(90); //0 is fwd, 90 left, etc
%eye = %user.getForwardVector();
%matrix=MatrixCreateFromEuler( %xAngle SPC %yAngle SPC %zAngle);
%vec=MatrixMulVector(%matrix, %eye);This should then be the vector you want to add to your object's position. Probably want to scale it too.
-s
#4
05/11/2005 (2:58 am)
Something like this:%selPos = %selectedObject.getPosition(); %forwardVec = LocalClientConnection.player.getForwardVector(); %offset = VectorScale(%forwardVec, %distanceToMove); %newPos = VectorAdd(%selPos, %offset); %selectedObject.setTransform(%newPos);
#6
Basically, think a shotgun, except instead of a random spread you've got a perfect grid. I start with, say, the top left and interpolate all the others basically using the two algorithms above. But when the player looks up or down the spread gets (much) bigger, and what I think I understand is that this is simply because of the bigger Z numbers I'm getting (which is perpendicular to the spread).
I've been busting my brain on it for a couple days, wishing I was better educated :-)
08/12/2008 (10:24 pm)
@Matt: do you happen to know of an algorithm to translate along a vector X distance specificially? Reason I ask is I've been doing the VectorScale method for some time now and it's fine, apparently, if you're moving some arbitrary distance along a single vector. But I've run into a glitch where I'm making projectiles in a grid spread pattern, and what happens is when the player looks up or down the scale returns a larger spread between the various vectors.Basically, think a shotgun, except instead of a random spread you've got a perfect grid. I start with, say, the top left and interpolate all the others basically using the two algorithms above. But when the player looks up or down the spread gets (much) bigger, and what I think I understand is that this is simply because of the bigger Z numbers I'm getting (which is perpendicular to the spread).
I've been busting my brain on it for a couple days, wishing I was better educated :-)
#7
Basically, think a shotgun, except instead of a random spread you've got a perfect grid. I start with, say, the top left and interpolate all the others basically using the two algorithms above. But when the player looks up or down the spread gets (much) bigger, and what I think I understand is that this is simply because of the bigger Z numbers I'm getting (which is perpendicular to the spread).
I've been busting my brain on it for a couple days, wishing I was better educated :-)
08/13/2008 (7:28 pm)
@Matt: do you happen to know of an algorithm to translate along a vector X distance specificially? Reason I ask is I've been doing the VectorScale method for some time now and it's fine, apparently, if you're moving some arbitrary distance along a single vector. But I've run into a glitch where I'm making projectiles in a grid spread pattern, and what happens is when the player looks up or down the scale returns a larger spread between the various vectors.Basically, think a shotgun, except instead of a random spread you've got a perfect grid. I start with, say, the top left and interpolate all the others basically using the two algorithms above. But when the player looks up or down the spread gets (much) bigger, and what I think I understand is that this is simply because of the bigger Z numbers I'm getting (which is perpendicular to the spread).
I've been busting my brain on it for a couple days, wishing I was better educated :-)
Torque 3D Owner Stephen Zepp
So if your player is facing at an angle of 90 degrees, and the object happens to be facing at 270 degrees, the object would move to the east (right/90 degree line), instead of the west/left/270 degree line?