Moving an Object in 3D

Started by
16 comments, last by Cornstalks 18 years, 3 months ago
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
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Advertisement
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>
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?
c++ and OpenGL, sorry about that
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
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.
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
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.
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
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.
Quote:Original post by MikeTacular
c++ 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.
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.
radians = degrees*(pi/180)
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.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

This topic is closed to new replies.

Advertisement