Archived

This topic is now archived and is closed to further replies.

To vector translate or not to vector translate...

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

Recommended Posts

Hello all, do any of you brainy folk have a code snippet that translates a vector without the use of matrices, or is that a bad idea??

Share on other sites
When you say "translate a vector" I assume you have a vector that represents a coordinate location. I don''t know why you''d want to translate a pure vector, such as a normal vector.

Ultimately, even using a matrix, a translation leads to something
like:

vector.x = vector.x + translation.x;
vector.y = vector.y + translation.y;
vector.z = vector.z + translation.z;

I see no harm in doing that directly.

(If you need to "transform" the vector----which can mean scale or rotate----then you can do that in a closed form manner as well, and it can be cheaper when you know you''re rotating about a given axis for example.)

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.

Share on other sites
Thanks for the reply Graham, I''ll clarify my exact problem.
I am using opengl''s tranlation function to posistion a model at a certain location. I am calculating a bounding box for this model before I translate it and I obviously want the bounding box to move to the same location. So I''m after a similar function that will actualy update a vectors posisiton.

Share on other sites
OK, your clarification helps me to ask these new questions. Hopefully we''ll get to the bottom of the solution!

So the vector you want to translate is actually the position of the bounding box? And, since the bounding box is used for collisions or culling or something and not merely a graphic to be drawn, you''d like to know how to translate the vector *independent* of the glTranslate() methods? (Because if the bounding box was merely to be rendered, why not just draw it with the object once you''ve caleld glTranslate()?)

If the answer to both questions is yes, then here is a new question. How do you represent the vector? Is it a property of the bounding box object? What does the vector object itself look like?

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.

Share on other sites
Well Graham, the answer to the first 2 questions are indeed yes.
The bounding box is represented by 2 3d co-ordinates, minimum and max values. I can translate the box using gl_translate() and display it correctly. I''m using the box for collision perposes and what I want to do is to translate the actual pair of co-ordinates. For example:

--calculate bounds;
--translate model;
--display model;
--translate bounds;
--check collisions;

Theres nothing fancy going on at all as regards to the bounding box. I hope I''ve made the problem clearer.

Share on other sites
I''ve come to the conclusion that this is really a programming question and not a math or physics question. I''ll give my thoughts, but if they don''t really give you the guidance you''re looking for I''ll move the thread to the graphics programming and theory forum.

You have *two* coordinates that need to be translated, the minimum and maximum 3d points of the bounding box. I see this as straightforward. I''ll write *one* approach in terms of your list of steps:

  --read model;{ do stuff;}--calculate bounds;{ do stuff;}--translate model;{ glTranslate(dx, dy, dz);}--display model;{ glBegin(GL_something); glVertex() and stuff; glEnd();}// AND NOW, the moment you''ve been waiting for!--translate bounds;{ bbox->min.x += dx; bbox->min.y += dy; bbox->min.z += dz; bbox->max.x += dx; bbox->max.y += dy; bbox->max.z += dz;}--check collisions;{ do stuff using the updated bounding boxes;}

So, really all I did was explicitly increment the 2 bounding box points using the *same* values that were passed into the glTranslate() call.

This approach is simple as long as you are only doing a translate. If you''re doing a rotate as well, then you may be better off constructing a 4x4 transformation matrix and then applying that matrix to each of the two box points, for example:

bbox->min = transformation_matrix * bbox->min;
bbox->max = transformation_matrix * bbox->max;

where transformation_matrix is your 4x4 matrix that contains both translation and rotations, and * is overloaded to multiply a 4 x 4 matrix times a vector of length 4. NOTE: For arbitrary transformations, vectors are represented as either (x, y, z, 1) or (x, y, z, 0). The former case is a coordinate in space----which your bounding box points are. The latter case is a vector direction that doesn''t represent a physical point in space.

If you do need to do rotations, but are not familiar with transformation matrices, let me know here. That is something that you can get an answer to in the graphics programming and theory forum, and I''ll move the thread there if you need some assistance learning about this.

If you''re only doing translation, then you can save a bit of work by representing your bounding box as one minimum point, and then the vector representing the width, height, and depth. This way, you only have to update the minimum point in the translate bounds step. But this does make your collision checks a bit more expensive. If you have very few collisions it might be a good idea.

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.

Share on other sites
Thankyou Graham, the problem has finally been resolved!
Your example did not not work as expected as the bounding values were constantly iterated every frame, due to the fact that the code is in the main rendering loop. So once I moved it, the box was translated correctly. Before posting my prob I remember trying your method at one stage, and since getting the undesired results I got a bit confused, thinking that there must be more to translation than I thought.
Thanks for your advice on matrices, I''m trying to steer clear of them for now until I really know why I need to use them.(It''s working so far!).

Share on other sites
Coolio!

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.

1. 1
Rutin
27
2. 2
3. 3
4. 4
5. 5

• 11
• 11
• 10
• 13
• 20
• Forum Statistics

• Total Topics
632948
• Total Posts
3009401
• Who's Online (See full list)

There are no registered users currently online

×