Welcome, Guest. Please login or register.

Author Topic: My Amos-vector question moved here  (Read 3401 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline mdwh2

  • Hero Member
  • *****
  • Join Date: Jun 2002
  • Posts: 565
    • Show all replies
Re: My Amos-vector question moved here
« on: May 22, 2004, 05:34:50 PM »
Firstly, was there a previous thread - you seemed to refer to earlier things, I wasn't sure if there was another thread I should be looking at. If not, what is it you are actually trying to achieve?

Secondly, there appear to be two issues here: what algorithms should be used to do vector maths, and how to actually code it (in that you don't know C that well, but the rest of us get totally confused by AMOS - I mean yes, I know what it means, but it's very unreadable code;)

Your way of doing things is quite reasonable - you have an orthonormal axis (ie, the 3 vectors) which completely define the rotation, and then you're doing rotations with respect to this local axis. This is certainly a lot better in general than say using Euler angles (which suffer from "Grimbol lock").

For some simple speed ups, it depends on what you're trying to do. Maybe the three rotations in each loop can be combined into a single rotation? It's easiest to work this out by expressing the three rotations as matrices, and multiplying them together. If you're doing the same rotation each time, then the 9 elements of the matrix can then be precomputed.

Each of the 9 elements in the set of 3 vectors then requires 3 multiplys and 2 additions, so multiplying the 3 vectors by the rotation matrix costs 27 multiplys and 18 additions.

Where as in your method, each individual rotation costs 12 multiplys and 6 additions, which gives a total of 36 multiplys and 18 additions.

I think that's right..

One thing to be careful is numerical instability - due to inaccuracies in floating point calculations, your 3 vectors will drift from being orthogonal and normalised vectors, so this may have to be corrected every so often.

Now, if you want a faster way of doing things, I suggest you look up about "Quaternions". Basically, they represent a rotation by only 4 numbers (they are actually 4-dimensional equivalents of complex numbers, but don't let that put you off;) instead of 9, so this means less calculations are required. A knowledge of vector operations such as cross product and dot product is highly recommended before you start learning about Quaternions.

Some good information on these is available at http://www.j3d.org/matrix_faq/matrfaq_latest.html (A warning - a lot of tutorials and FAQs on Quaternions have mistakes in some of the calculations! This FAQ as far as I can tell seems to be correct).
 

Offline mdwh2

  • Hero Member
  • *****
  • Join Date: Jun 2002
  • Posts: 565
    • Show all replies
Re: My Amos-vector question moved here
« Reply #1 on: May 22, 2004, 05:41:03 PM »
Quote

GreatLor wrote:
Well, I dont have any problems whatsoever with not using structures, simply because the languages I'm playing with right now doesnt support them, anyway, I didnt get what "return_vec.x = first_vec.x * second_vec.x / position;" is supposed to do, will "return_vec.x = first_vec.x * second_vec.x / position;" give the ROTATED x position ??? :-?
No it won't - he said "I can't really see what you are trying to do though :-/ (as demonstrated by my random example)."

Quote
Its a shame that others do not contribute in this thread, I find vector-programming quite intresting, anyone who knows of any forum(s) (doesnt matter what platform - although Amiga is preferable) about this subject ? If yes then link, if no then dont bother  :-D
As I sort of said in my first reply, it's probably better to separate into the two issues of "algorithms" and "how to write this in AMOS". This forum may be good for AMOS programming, but isn't particularly suited towards 3D programming. I'd recommend going to a more general forum, and either just writing out the maths, or using pseudo code (eg, a1.x = sin(91) * a.x). That way people who have never used AMOS (and even those who have but still find it hard to read;) will understand, and hopefully be able to help:)

Some good forums for 3D programming are:

http://www.gamedev.net/
http://www.gamasutra.com/
http://www.flipcode.com/
 

Offline mdwh2

  • Hero Member
  • *****
  • Join Date: Jun 2002
  • Posts: 565
    • Show all replies
Re: My Amos-vector question moved here
« Reply #2 on: May 29, 2004, 01:40:33 PM »
Quote

Cymric wrote:
Quote
Karlos wrote:
Your best bet is to create a Vector type that has x,y,z,1. You can use a 4x4 matrix to perform any transformation on such a vector - rotation, scaling, translation, projection, you name it.

What is the purpose of the fourth element (1) in that type? Someone already mentioned quaternions in this thread, are you mimicking those by any chance?

It's not quaternions. The thing is that with a 3x3 matrix there are some transformations you cannot do - in particular, there is no way you can translate a vector. But using 4x4 matrices allow this. The extra "1" is to make this work - you need a four dimension vector to multiply with a 4x4 matrix.

It typically looks something like:

( R11 R12 R13 Tx )
( R21 R22 R23 Ty )
( R31 R32 R33 Tz )
( 0   0   0   1  )

When you multiply this by the vector with elements {x, y, z, 1}, you get the same as if you'd multiplied by vector {x, y, z} by the 3x3 matrix {Rij}, and then translated by the vector T. The bottom row of the 4x4 matrix is such that the 4th element in the vector remains as 1.

You can also consider values other than 1 to be treated as a scaling factor, which makes it easier to implement more types of transformations.

However, I don't think that all this is any help as far as rotations are concerned - but it's a useful  way of doing things if you want to represent translations in your matrix operations.