Public Group

# Moving an Object in 3D

This topic is 4522 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I know this probably has been asked before, but every time I search for it, I can't seem to find what I'm looking for, and if I have found it, then I really don't understand it. I want to be able to move an object in 3D, say with a resultant of 100. But if I rotate the object around on the x and y axis, I don't know how to find the new coordinates. I've looked around on the internet, and I often see all this Matrix transormation stuff and rotations, but I'm not sure exactly how to do it, the tutorials are confusing and I'm not sure it does what I want it to. Anyways, all I'm really looking for is a clear explanation. --Thanks, Michael

##### Share on other sites
matrices are what you need. with a matrix you can describe the translation, rotation, scale etc of an object.

check out "mathematics for 3d game programming & computer graphics 2nd ed." :
<http://www.amazon.com/gp/product/1584502770/qid=1136436369/sr=8-1/ref=sr_8_xs_ap_i1_xgl14/104-0176799-2308757?n=507846&s=books&v=glance>

also "the cg tutorial" covers the basics of this stuff, and you could learn shaders while you're at it:

<http://www.amazon.com/gp/product/0321194969/qid=1136436540/sr=2-1/ref=pd_bbs_b_2_1/104-0176799-2308757?s=books&v=glance&n=283155>

##### Share on other sites
The short of it is that you are looking for a Translation matrix, which is a type of transform.

In your game or program, keep track of an objects position any way you like, I personally use a 3D vector to do so. If your object moves in the positive X direction by 100 units, simply add 100 to the x value of the vector.

Now when you are creating the visual representation of your object, (be it a quad, mesh, sphere etc.) make sure it is centered around the origin. Now when you go to draw it on the screen, simply set the world transfrom equal to a translation matrix based off of your objects position. How this is actually done will depend on the API you are using. Could you let us know if you are using directx or opengl?

##### Share on other sites
c++ and OpenGL, sorry about that

##### Share on other sites
glBegin(GL_TRIANGLES);	glRotatef(rx, ry, rz);	glTranslatef(tx, ty, tz);	glVertex3f(0.0f, 0.0f, 0.0f);	glVertex3f(-2.0f, 0.0f, 0.0f);	glVertex3f(0.0f, 2.0f, 0.0f);glEnd();

This code just draws a triangle that is rotated by 'rx' on the x-axis, 'ry' on the y-axis and 'rz' on the z-axis. It is also translated (moved) by 'tx' on the x-axis, 'ty' on the y-axis and 'tz' on the z-axis.

##### Share on other sites
glBegin(GL_TRIANGLES);	glRotatef(rx, 1.0f, 0.0f, 0.0f);	glRotatef(ry, 0.0f, 1.0f, 0.0f);	glRotatef(rz, 0.0f, 0.0f, 1.0f);	glTranslatef(tx, ty, tz);	glVertex3f(0.0f, 0.0f, 0.0f);	glVertex3f(-2.0f, 0.0f, 0.0f);	glVertex3f(0.0f, 2.0f, 0.0f);glEnd();

This code just draws a triangle that is rotated by 'rx' on the x-axis, 'ry' on the y-axis and 'rz' on the z-axis. It is also translated (moved) by 'tx' on the x-axis, 'ty' on the y-axis and 'tz' on the z-axis.

Everything happens behind the scenes of those function calls.
The glRotatef function will multiply the modelview matrix by rotation matrices it constructs. And the glTranslatef function does the same thing, but with translation matrices.

##### Share on other sites
Quote:
 But if I rotate the object around on the x and y axis, I don't know how to find the new coordinates

I'm a little confused. If you apply a rotation matrix to a point (multiply), what you get are the new coordinates.

##### Share on other sites
Quote:
 Original post by MikeTacularc++ and OpenGL, sorry about that

If you haven't done so already, take a look at the NeHE OpenGL tutorials. They've helped many a beginner get started with 3D graphics.

##### Share on other sites
I think i know what your problem is, you want camera relative movement, right?

newz += speed*cos(rotation)
newx += speed*sin(rotation)

This formula is for the "forward" button, I think you can figure out the others yourself.

I think rotation has to be in radians.

##### Share on other sites
Well....actually I think I may have poorly explained the first time. noteventime was close, except I already know how to do it for 2D, I just need to do it with 3D. The problem with glTranslate(tx,ty,tz) is that I don't know tx, ty, or tz. Let me try to do a little example.

A,B,C are sidesa,b,c are angles         /|        /b|               A=80       /  |              B=60    C /   | A           C=100     /    |    /     |          a=53.13 degrees   /      |          b=36.87  /a_____c|         c=90      B

I can do it in 2D as you see, but my problem is 3D.

I don't know how to find the new coorodinates (X,Y,Z). I've looked a lot at matrices, but the major problem is that it has a matrix for translating along certain values, with a matrix of
[ 1,  0,  0, 0][ 0,  1,  0, 0][ 0,  0,  1, 0][tx, ty, tz, 1]

But I don't know how to find tx, ty, or tz.

Another way of saying this, is that I want a function called move(float distance). I want that function to change the object's (or camera's) cooridanites depending on how far I want it moved and its rotation. I want it to move the way its facing.

Thanks for the replies, but none have answered my question yet.

• 10
• 17
• 9
• 13
• 41