Game Development Community


#1
01/28/2003 (10:12 am)
It appears to me that all you need to use is a standard rotation matrix. Store the values of the vertices in the base position (relative to the origin). Then calculate the rotation matrix for the coordinate system, and apply it to the original vertices.

I believe the rotation matrix would be like so for the third one (the hardest case)...

x | y
cos 45 | sin 45
sin 45 | cos 45
#2
01/28/2003 (10:29 am)
Dean,

On the assumption that you don't want to calculate how to rotate the points but to find the min/max bounds in X/Y axis and you don't want to use trig then assuming you have two functions such as ...
float min(float a, float b) { return ( a < b ) ? a : b; }
float max(float a, float b) { return ( a > b ) ? a : b; }
... and assuming that the object-space coords are in two arrays such as ...
float x[4];
float y[4];
... then you could do something approximating...
float minx = 1e+7;
float miny = 1e+7;
float maxx = 1e-7;
float maxy = 1e-7;
for (int n = 0; n < 4; ++n)
{
   if ( x[n] < minx ) minx = x[n];  
   if ( x[n] > maxx ) maxx = x[n];  
   if ( y[n] < miny ) miny = y[n];  
   if ( y[n] > maxy ) maxy = y[n];  
}
This isn't the most efficient way of doing this (by far) but I wanted to give you a simple example of how it could be done. You can make this much more efficient.

If it's simply the rotation you want then Lees answer is what you need to follow.

Hope this answers your question.

- Melv.
#3
01/28/2003 (2:09 pm)
Here is some code I took from an engine I was trying to make in QB, it should work with a port to C++ but I don't know C so I'm not much help on that part.

Figuring out X:
VERTXTEMP(C) = VERTX(C) * (SINTAB(RDX) * SINTAB(RDY) * SINTAB(RDZ) + COSTAB(RDY) * COSTAB(RDZ)) + VERTY(C) * (COSTAB(RDX) * SINTAB(RDZ)) + VERTZ(C) * (SINTAB(RDY) * COSTAB(RDZ) - COSTAB(RDY) * SINTAB(RDX) * SINTAB(RDZ))

Figuring out Y:
VERTYTEMP(C) = VERTX(C) * (SINTAB(RDX) * SINTAB(RDY) * SINTAB(RDZ) - COSTAB(RDY) * SINTAB(RDZ)) + VERTY(C) * (COSTAB(RDX) * COSTAB(RDZ)) + VERTZ(C) * (COSTAB(RDY) * -1 * SINTAB(RDX) * COSTAB(RDZ) - SINTAB(RDY) * SINTAB(RDZ))

Figuring out Z:
VERTZTEMP(C) = VERTX(C) * (SINTAB(RDY) * -1 * COSTAB(RDX)) + VERTY(C) * (SINTAB(RDX)) + VERTZ(C) * (COSTAB(RDY) * COSTAB(RDX))

Where:

VERTXTEMP, VERTYTEMP, VERTZTEMP = The results of the equation (i.e., the new location of the point)
VERTX, VERTY, VERTZ = The position of the point you want to move
C = the point you wish to calculate position for, assuming theyre all stored in arrays
SINTAB = A precalculated Sin table
COSTAB = A precalculated Cos table
RDX, RDY, RDZ = Rotation along the X, Y and Z-axis in degrees

How to calculate the sin and cos tables (which are in radians):
FOR D = 0 TO 359
COSTAB(D) = COS(D * .01745) '.01745 so we dont have to convert degrees to radians every time
SINTAB(D) = SIN(D * .01745)
NEXT

So basically what you need to do is plug in your X, Y and degrees of rotation; and then do a nifty m=x/y or however you calculate distance (I cant remember the formula at the moment) with the new points and the originals. It took me a long while to finger out these formulas, but they work. Drop another post if you have any questions aboot the formulas.

P.S. - assuming 40 is half the length and 20 is half the width, you would use X = 140, Y = 220 , Z = 0 and RDZ = 45 (degrees) in the formula. (GRRRRR....I can't get my thoughts together today)
#4
01/28/2003 (3:26 pm)
Just wondering if that code snippet helped any.
#5
01/29/2003 (10:26 am)
Roughly ...
float angle = 45;
float x1 = x + (Width/2) * mCos( mDegToRad(angle) );
float y1 = y + (Length/2) * mSin( mDegToRad(angle) );
- Melv.
#6
01/30/2003 (1:33 pm)
No problem. :)

- Melv.