so do you want to move the object in the direction that it is faceing?
edit:
oh well you clearly said that you want it to move in the direction it is faceing.
so you want a function like this:
void move(float dist);
can't do it, you would need a direction. think about it, what would you do if i told you to move an object 33ft? you'd probably ask which direction i wanted you to move in. you'd need something more like this:
matrix4x4 move(float dist, vector3 dir)//dir should be a unit vector
{
dir*=dist;
matrix4x4 T = MatrixIdentity(T);
T._41 = dist.x;
T._42 = dist.y;
T._43 = dist.z;
return T;
}
or, even better:
matrix4x4 move(vector3 dir)//dir does NOT need to be a unit vector
{
matrix4x4 T = MatrixIdentity(T);
T._41 = dist.x;
T._42 = dist.y;
T._43 = dist.z;
return T;
}
either of those would translate some object however you specified.
note: consider the above fonctions psudocode.
Moving an Object in 3D
Youl only have to do the sine on the Z/Y plane...
Same as in 2D, there's probably a much better way though.
Same as in 2D, there's probably a much better way though.
that doesnt work. If i did this:
X,Y,Z are coordinates
xa,ya are angles
dist = distance i want to move
X=sin(ya)*dist
Y=sin(xa)*dist
Z=cos(ya)*dist
Lets say I was in coordinates (0,0,0), xa was 45 degrees, and ya was 30, and I wanted to move 100 units. If I plug that in I get:
X=50 units
Y=70.71 units
Z=86.60 units
But that's not moving 100 units. That would be 122.47 units.
[edit]
I didn't see your update minamur, sorry. I see what you mean, but the problem is that I don't know how far I want to move along each axis. I want a resultant (or hypotonuse if you will) to equal 100. I'd already have the objects angles stored, so my function doesn't need to have them put in it (although I don't mind for purposes of examples and what not). All I know is how far I want to move (in total movement, not along each axis), and what angles to move at. What I need to figure out is dist.x, dist.y, and dist.z, (the distance I want to move along each axis to produce a total movement of 100), but I don't know how too.
X,Y,Z are coordinates
xa,ya are angles
dist = distance i want to move
X=sin(ya)*dist
Y=sin(xa)*dist
Z=cos(ya)*dist
Lets say I was in coordinates (0,0,0), xa was 45 degrees, and ya was 30, and I wanted to move 100 units. If I plug that in I get:
X=50 units
Y=70.71 units
Z=86.60 units
But that's not moving 100 units. That would be 122.47 units.
[edit]
I didn't see your update minamur, sorry. I see what you mean, but the problem is that I don't know how far I want to move along each axis. I want a resultant (or hypotonuse if you will) to equal 100. I'd already have the objects angles stored, so my function doesn't need to have them put in it (although I don't mind for purposes of examples and what not). All I know is how far I want to move (in total movement, not along each axis), and what angles to move at. What I need to figure out is dist.x, dist.y, and dist.z, (the distance I want to move along each axis to produce a total movement of 100), but I don't know how too.
First calculate the three factors without multiplying them.
eg:
fx: sin(phetaXZ) = 0.7
fy: sin(phetaYZ) = 0.5
fz: cos(phetaXZ) = 0.3
then the final result is:
x=speed*(fx/(x+y+z))
y=speed*(fy/(x+y+z))
z=speed*(fz/(x+y+z))
Then just add these to your x, y and z coordinates :D, enjoy!
Hope this is right, It's just an idea i got... havn't tried and theres probably something wrong.
eg:
fx: sin(phetaXZ) = 0.7
fy: sin(phetaYZ) = 0.5
fz: cos(phetaXZ) = 0.3
then the final result is:
x=speed*(fx/(x+y+z))
y=speed*(fy/(x+y+z))
z=speed*(fz/(x+y+z))
Then just add these to your x, y and z coordinates :D, enjoy!
Hope this is right, It's just an idea i got... havn't tried and theres probably something wrong.
Imagine that you've got a vector coming from the center of the object which represents its "look direction." When the object is entirely unrotated, this vector is (0,0,1) in world space - pointing straight down the Z axis. So the object, in its 'rest position,' points down the Z axis.
Now say that you apply a couple of rotations to the object. You'd apply these to the look direction vector too; say you end up with the vector (0.2, 0.5, 0.7) (though in reality you'd normalise it).
Now you want to move 10 units in that direction: It's as simple as "position += 10 * lookDirection". You add 10*0.2=2 to the X, 10*0.5=5 to the Y, and 10*0.7=7 to the Z.
The matrix math you've been seeing is basically a shorter form of this.
Now say that you apply a couple of rotations to the object. You'd apply these to the look direction vector too; say you end up with the vector (0.2, 0.5, 0.7) (though in reality you'd normalise it).
Now you want to move 10 units in that direction: It's as simple as "position += 10 * lookDirection". You add 10*0.2=2 to the X, 10*0.5=5 to the Y, and 10*0.7=7 to the Z.
The matrix math you've been seeing is basically a shorter form of this.
noteventime: it doesn't work, although it was a good idea. also, it wouldn't work if the player was at (0,0,0), since you can't divide by 0.
superbig: that doesn't work either. if you use the distance formula (in this case it would be sqrt(4+25+49)) you don't get 10 units, you get 8.83176. Thanks though
Please people, I have no idea how to do this, and I'm willing to hear whatever you've got. Thanks for all the replies I've had so far though.
superbig: that doesn't work either. if you use the distance formula (in this case it would be sqrt(4+25+49)) you don't get 10 units, you get 8.83176. Thanks though
Please people, I have no idea how to do this, and I'm willing to hear whatever you've got. Thanks for all the replies I've had so far though.
Good news everyone. I've almost got it. I was sitting there, looking at all this junk and formulas and triangles and everything, when suddenly I got a great idea. What came to me was something similar to what noteventime said, but a little different. Here is the code.
Now I do have one problem though. If I angle it on the x axis, then I still move an improper amount on the z axis. I'm not sure if I just have to do a little
I still have to test it, and it probably needs a few revisions to make it more accurate and faster, but if you have any comments that would be great.
#include <iostream>#include <math.h>#define pi 3.14159265358979323846264338327950288419716939937510582097494459230781640628620//note: I know pi is already defined.....but this is what boredom leads too....//and no I didn't figure it out, its from a website that has pi up to 1 million places.using namespace std;struct ANGLE { float x,y,z;};struct OBJECT { float x,y,z; ANGLE angle;};OBJECT obj;float dist; //variable to state how far you want to moveANGLE temp; //thing for manipulating stufffloat t; //used for the ratio, very importantint main(){ cout << "Enter Object's Angles" << endl; cout << "X: "; cin >> obj.angle.x; cout << "Y: "; cin >> obj.angle.y; obj.angle.x *= pi/180; obj.angle.y *= pi/180; cout << endl << endl; cout << "Enter Object's Current Position" << endl; cout << "X: "; cin >> obj.x; cout << "Y: "; cin >> obj.y; cout << "Z: "; cin >> obj.z; cout << endl << endl; cout << "Enter Move Distance: "; cin >> dist; cout << (temp.x = sin(obj.angle.y)*dist) << endl; cout << (temp.y = sin(obj.angle.x)*dist) << endl; cout << (temp.z = cos(obj.angle.y)*dist) << endl; //this is where the magin happens. I find the ratio its at, and that way //I scale the movement so it is proper, otherwise, I would move to far/short t = dist / sqrt(temp.x*temp.x + temp.y*temp.y + temp.z*temp.z); temp.x *= t; temp.y *= t; temp.z *= t; obj.x += temp.x; obj.y += temp.y; obj.z += temp.z; cout << endl << "Object's Position" << endl << "X: " << obj.x << endl <<"Y: " << obj.y << endl << "Z: " << obj.z << endl; cout << "Total Move Distance: " << sqrt(temp.x*temp.x + temp.y*temp.y + temp.z*temp.z) << endl; getchar(); getchar();}
Now I do have one problem though. If I angle it on the x axis, then I still move an improper amount on the z axis. I'm not sure if I just have to do a little
if (obj.angle.x == 90 || obj.angle.x == 270) temp.z = 0;
I still have to test it, and it probably needs a few revisions to make it more accurate and faster, but if you have any comments that would be great.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement