3D Rotation and more, this one is HARD! (For me anyways)

The full source of my problem can be downloaded here:

http://members.home.nl/fearum/dd.zip

It is all about demo2.cpp (included down here too)

Move to the first ****<<<<< Line for my question

**---- DEMO2.CPPP ----

#include "..\DevLib\DevLib.H"

#include <math.h>

Math     *MyMath;
Keyboard *MyKeyboard;
Sound    *MySound;
Video    *MyVideo;

int Points = 8;

float Box [8*3] =
{
  -10,-10,-10,
      10,-10,-10,
      10, 10,-10,
  -10, 10,-10,
  -10,-10, 10,
      10,-10, 10,
      10, 10, 10,
  -10, 10, 10
};

float RotatedBox [8*3];

void main ( )
{
  char KeyCode;
  int  XAngle;
  int  YAngle;
  int  ZAngle;
  int  Loop;

  MyMath     = new Math;
  MyKeyboard = new Keyboard;
  MySound    = new Sound;
  MyVideo    = new Video;

  MyKeyboard->Initialize ( );
  MyVideo->Initialize    ( Mode320x200x256 );

  XAngle = 0;
  YAngle = 0;
  ZAngle = 0;
  while ( KeyCode != 1 )
  {
       MyKeyboard->GetKeyCode ( KeyCode );

       //MyMath->Rotate3D ( Box, RotatedBox, 8, XAngle, YAngle, ZAngle );

*******>>>>> Something must be wrong with my Rotate3D code, but what? If (As I'm doing now) i dont rotate anything the box turns out right, if I rotate I get a line of mess at y=100 (aproxematly).

I tested my multiplymatrix code, so I dont think its that, but who knows, please check it!!!! (just to be sure the math.cpp is included below demo2.cpp )

It's not my sinus tables either, tested them too. HELPP!P!P!P!P!!

       for ( Loop = 0; Loop < 8*3; Loop++ )
       {
            RotatedBox [Loop] = Box [Loop];
       }

       for ( Loop = 0; Loop < Points; Loop++ )
       {
            MyVideo->PutPixel ( 160 + ( 256 * RotatedBox [Loop * 3] / ( RotatedBox [Loop * 3 + 2] - 256 ) ), 100 + ( 256 * RotatedBox [Loop * 3 + 1] / ( RotatedBox [Loop * 3 + 2] - 256 ) ), 15, MyVideo->Screen );
       }

       XAngle += 5;

       if ( XAngle > 359 )
       {
            XAngle = 0;
       }
  }

  MyVideo->Terminate    ( );
  MyKeyboard->Terminate ( );

  delete MyVideo;
  delete MySound;
  delete MyKeyboard;
  delete MyMath;
}

**---- END OF DEMO2.CPP ----

**---- MATH.CPP ------

//----------------------------------------------------------------------------
//
// DevLib Version 1.3.1
//
// Copyright (C) 2001 UtopiC
//
// We are:
//
//   Fearum
//   DNALoR
//   UtopiC DragoN
//   D. Evil alias R3M8O1
//
//----------------------------------------------------------------------------

#ifndef MathCpp
#define MathCpp

//-- Main Math Class Static Variables ----------------------------------------

const float Math::SinusTable [360] =
{
      0.000000,  0.017452,  0.034899,  0.052336,  0.069756,  0.087156,  0.104528,  0.121869,
      0.139173,  0.156434,  0.173648,  0.190809,  0.207912,  0.224951,  0.241922,  0.258819,
      0.275637,  0.292372,  0.309017,  0.325568,  0.342020,  0.358368,  0.374607,  0.390731,
      0.406737,  0.422618,  0.438371,  0.453991,  0.469472,  0.484810,  0.500000,  0.515038,
      0.529919,  0.544639,  0.559193,  0.573576,  0.587785,  0.601815,  0.615662,  0.629320,
   0.642788,  0.656059,  0.669131,  0.681998,  0.694658,  0.707107,  0.719340,  0.731354,
   0.743145,  0.754710,  0.766044,  0.777146,  0.788011,  0.798636,  0.809017,  0.819152,
   0.829038,  0.838671,  0.848048,  0.857167,  0.866025,  0.874620,  0.882948,  0.891007,
   0.898794,  0.906308,  0.913545,  0.920505,  0.927184,  0.933580,  0.939693,  0.945519,
   0.951057,  0.956305,  0.961262,  0.965926,  0.970296,  0.974370,  0.978148,  0.981627,
   0.984808,  0.987688,  0.990268,  0.992546,  0.994522,  0.996195,  0.997564,  0.998630,
      0.999391,  0.999848,  1.000000,  0.999848,  0.999391,  0.998630,  0.997564,  0.996195,
   0.994522,  0.992546,  0.990268,  0.987688,  0.984808,  0.981627,  0.978148,  0.974370,
   0.970296,  0.965926,  0.961262,  0.956305,  0.951056,  0.945519,  0.939693,  0.933580,
   0.927184,  0.920505,  0.913545,  0.906308,  0.898794,  0.891006,  0.882948,  0.874620,
   0.866025,  0.857167,  0.848048,  0.838671,  0.829038,  0.819152,  0.809017,  0.798635,
      0.788011,  0.777146,  0.766044,  0.754710,  0.743145,  0.731354,  0.719340,  0.707107,
      0.694658,  0.681998,  0.669131,  0.656059,  0.642788,  0.629321,  0.615661,  0.601815,
      0.587785,  0.573576,  0.559193,  0.544639,  0.529919,  0.515038,  0.500000,  0.484810,
      0.469472,  0.453991,  0.438371,  0.422618,  0.406737,  0.390731,  0.374606,  0.358368,
      0.342020,  0.325568,  0.309017,  0.292372,  0.275637,  0.258819,  0.241922,  0.224951,
      0.207912,  0.190809,  0.173648,  0.156434,  0.139173,  0.121869,  0.104528,  0.087156,
      0.069756,  0.052336,  0.034899,  0.017452, -0.000000, -0.017452, -0.034899, -0.052336,
  -0.069756, -0.087156, -0.104528, -0.121869, -0.139173, -0.156434, -0.173648, -0.190809,
  -0.207912, -0.224951, -0.241922, -0.258819, -0.275637, -0.292372, -0.309017, -0.325568,
  -0.342020, -0.358368, -0.374607, -0.390731, -0.406737, -0.422618, -0.438371, -0.453991,
  -0.469472, -0.484810, -0.500000, -0.515038, -0.529919, -0.544639, -0.559193, -0.573576,
  -0.587785, -0.601815, -0.615661, -0.629320, -0.642788, -0.656059, -0.669131, -0.681998,
  -0.694658, -0.707107, -0.719340, -0.731354, -0.743145, -0.754710, -0.766045, -0.777146,
  -0.788011, -0.798635, -0.809017, -0.819152, -0.829038, -0.838671, -0.848048, -0.857167,
  -0.866025, -0.874620, -0.882948, -0.891006, -0.898794, -0.906308, -0.913545, -0.920505,
  -0.927184, -0.933581, -0.939693, -0.945519, -0.951056, -0.956305, -0.961262, -0.965926,
  -0.970296, -0.974370, -0.978148, -0.981627, -0.984808, -0.987688, -0.990268, -0.992546,
  -0.994522, -0.996195, -0.997564, -0.998630, -0.999391, -0.999848, -1.000000, -0.999848,
  -0.999391, -0.998630, -0.997564, -0.996195, -0.994522, -0.992546, -0.990268, -0.987688,
  -0.984808, -0.981627, -0.978148, -0.974370, -0.970296, -0.965926, -0.961262, -0.956305,
  -0.951056, -0.945519, -0.939693, -0.933580, -0.927184, -0.920505, -0.913545, -0.906308,
  -0.898794, -0.891006, -0.882948, -0.874620, -0.866025, -0.857167, -0.848048, -0.838670,
  -0.829038, -0.819152, -0.809017, -0.798635, -0.788011, -0.777146, -0.766044, -0.754710,
  -0.743145, -0.731354, -0.719340, -0.707107, -0.694658, -0.681998, -0.669131, -0.656059,
  -0.642788, -0.629320, -0.615661, -0.601815, -0.587785, -0.573577, -0.559193, -0.544639,
  -0.529919, -0.515038, -0.500000, -0.484809, -0.469471, -0.453991, -0.438371, -0.422618,
  -0.406736, -0.390731, -0.374607, -0.358368, -0.342020, -0.325568, -0.309017, -0.292372,
  -0.275638, -0.258819, -0.241922, -0.224951, -0.207912, -0.190809, -0.173648, -0.156434,
  -0.139173, -0.121869, -0.104529, -0.087156, -0.069756, -0.052336, -0.034900, -0.017453
};

const float Math::CosinusTable [360] =
{
      1.000000,  0.999848,  0.999391,  0.998630,  0.997564,  0.996195,  0.994522,  0.992546,
      0.990268,  0.987688,  0.984808,  0.981627,  0.978148,  0.974370,  0.970296,  0.965926,
      0.961262,  0.956305,  0.951057,  0.945519,  0.939693,  0.933580,  0.927184,  0.920505,
      0.913545,  0.906308,  0.898794,  0.891007,  0.882948,  0.874620,  0.866025,  0.857167,
      0.848048,  0.838671,  0.829038,  0.819152,  0.809017,  0.798636,  0.788011,  0.777146,
      0.766044,  0.754710,  0.743145,  0.731354,  0.719340,  0.707107,  0.694658,  0.681998,
      0.669131,  0.656059,  0.642788,  0.629320,  0.615662,  0.601815,  0.587785,  0.573576,
      0.559193,  0.544639,  0.529919,  0.515038,  0.500000,  0.484810,  0.469472,  0.453991,
      0.438371,  0.422618,  0.406737,  0.390731,  0.374607,  0.358368,  0.342020,  0.325568,
      0.309017,  0.292372,  0.275637,  0.258819,  0.241922,  0.224951,  0.207912,  0.190809,
      0.173648,  0.156434,  0.139173,  0.121869,  0.104528,  0.087156,  0.069757,  0.052336,
      0.034899,  0.017452, -0.000000, -0.017452, -0.034899, -0.052336, -0.069756, -0.087156,
  -0.104529, -0.121869, -0.139173, -0.156434, -0.173648, -0.190809, -0.207912, -0.224951,
  -0.241922, -0.258819, -0.275637, -0.292372, -0.309017, -0.325568, -0.342020, -0.358368,
  -0.374607, -0.390731, -0.406737, -0.422618, -0.438371, -0.453991, -0.469472, -0.484810,
  -0.500000, -0.515038, -0.529919, -0.544639, -0.559193, -0.573576, -0.587785, -0.601815,
  -0.615661, -0.629320, -0.642788, -0.656059, -0.669131, -0.681998, -0.694658, -0.707107,
  -0.719340, -0.731354, -0.743145, -0.754710, -0.766044, -0.777146, -0.788011, -0.798635,
  -0.809017, -0.819152, -0.829038, -0.838671, -0.848048, -0.857167, -0.866025, -0.874620,
  -0.882948, -0.891006, -0.898794, -0.906308, -0.913545, -0.920505, -0.927184, -0.933580,
  -0.939693, -0.945519, -0.951056, -0.956305, -0.961262, -0.965926, -0.970296, -0.974370,
  -0.978148, -0.981627, -0.984808, -0.987688, -0.990268, -0.992546, -0.994522, -0.996195,
  -0.997564, -0.998630, -0.999391, -0.999848, -1.000000, -0.999848, -0.999391, -0.998630,
  -0.997564, -0.996195, -0.994522, -0.992546, -0.990268, -0.987688, -0.984808, -0.981627,
  -0.978148, -0.974370, -0.970296, -0.965926, -0.961262, -0.956305, -0.951057, -0.945519,
  -0.939693, -0.933580, -0.927184, -0.920505, -0.913545, -0.906308, -0.898794, -0.891007,
  -0.882948, -0.874620, -0.866025, -0.857167, -0.848048, -0.838671, -0.829038, -0.819152,
  -0.809017, -0.798635, -0.788011, -0.777146, -0.766044, -0.754710, -0.743145, -0.731354,
  -0.719340, -0.707107, -0.694658, -0.681998, -0.669131, -0.656059, -0.642788, -0.629320,
  -0.615662, -0.601815, -0.587785, -0.573576, -0.559193, -0.544639, -0.529919, -0.515038,
  -0.500000, -0.484810, -0.469472, -0.453991, -0.438371, -0.422618, -0.406737, -0.390731,
  -0.374607, -0.358368, -0.342020, -0.325568, -0.309017, -0.292372, -0.275637, -0.258819,
  -0.241922, -0.224951, -0.207912, -0.190809, -0.173648, -0.156435, -0.139173, -0.121869,
  -0.104528, -0.087156, -0.069757, -0.052336, -0.034899, -0.017452,  0.000000,  0.017452,
      0.034899,  0.052336,  0.069757,  0.087156,  0.104528,  0.121869,  0.139173,  0.156435,
      0.173648,  0.190809,  0.207911,  0.224951,  0.241922,  0.258819,  0.275637,  0.292371,
      0.309017,  0.325568,  0.342020,  0.358368,  0.374607,  0.390731,  0.406737,  0.422618,
      0.438371,  0.453991,  0.469472,  0.484810,  0.500000,  0.515038,  0.529919,  0.544639,
      0.559193,  0.573576,  0.587785,  0.601815,  0.615662,  0.629320,  0.642788,  0.656059,
      0.669131,  0.681998,  0.694658,  0.707107,  0.719340,  0.731354,  0.743145,  0.754710,
      0.766044,  0.777146,  0.788011,  0.798636,  0.809017,  0.819152,  0.829038,  0.838671,
      0.848048,  0.857167,  0.866025,  0.874620,  0.882948,  0.891007,  0.898794,  0.906308,
      0.913546,  0.920505,  0.927184,  0.933580,  0.939693,  0.945519,  0.951057,  0.956305,
      0.961262,  0.965926,  0.970296,  0.974370,  0.978148,  0.981627,  0.984808,  0.987688,
      0.990268,  0.992546,  0.994522,  0.996195,  0.997564,  0.998630,  0.999391,  0.999848
};

//-- Main Math Class Functions -----------------------------------------------

void Math::DegreesToRad ( float _Theta, float &_Rad )
{
  _Rad = _Theta * Pi / 180;
}

//----------------------------------------------------------------------------

void Math::ClearMatrix ( float *_Matrix, int _Rows, int _Columns )
{
  int _ClearLoop;

  for ( _ClearLoop = 0; _ClearLoop < _Rows * _Columns; _ClearLoop++ )
  {
       _Matrix [_ClearLoop] = 0;
  }
}

//----------------------------------------------------------------------------

void Math::MultiplyMatrix ( float *_SourceMatrix, float *_SourceMatrix2, float *_TargetMatrix, int _Rows, int _Both, int _Columns )
{
  int _RowLoop;
  int _BothLoop;
  int _ColumnLoop;

  for ( _ColumnLoop = 0; _ColumnLoop < _Columns; _ColumnLoop++ )
  {
       for ( _RowLoop = 0; _RowLoop < _Rows; _RowLoop++ )
       {
            for ( _BothLoop = 0; _BothLoop < _Both; _BothLoop++ )
            {
              _TargetMatrix [_ColumnLoop + _RowLoop * _Rows] += _SourceMatrix [_RowLoop * _Rows+_BothLoop] * _SourceMatrix2 [_BothLoop * _Both + _ColumnLoop];
            }
       }
  }
}

//----------------------------------------------------------------------------

void Math::Rotate3D ( float *_SourceMatrix, float *_TargetMatrix, int _Points, int _XAngle, int _YAngle, int _ZAngle )
{
  int _MultiplyLoop;
  float _RotationMatrix [9];

  _RotationMatrix [0] =  CosinusTable [_ZAngle] *  CosinusTable [_YAngle] + SinusTable   [_ZAngle] * SinusTable [_XAngle] * SinusTable   [_YAngle];
  _RotationMatrix [1] =  CosinusTable [_YAngle] * -SinusTable   [_ZAngle] + CosinusTable [_ZAngle] * SinusTable [_XAngle] * SinusTable   [_YAngle];
  _RotationMatrix [2] =  CosinusTable [_XAngle] *  SinusTable   [_YAngle];
  _RotationMatrix [3] =  SinusTable   [_ZAngle] *  CosinusTable [_XAngle];
  _RotationMatrix [4] =  CosinusTable [_ZAngle] *  CosinusTable [_XAngle];
  _RotationMatrix [5] = -SinusTable   [_XAngle];
  _RotationMatrix [6] = -SinusTable   [_YAngle] *  CosinusTable [_ZAngle] + SinusTable   [_ZAngle] * SinusTable [_XAngle] * CosinusTable [_YAngle];
  _RotationMatrix [7] =  SinusTable   [_ZAngle] *  SinusTable   [_YAngle] + CosinusTable [_ZAngle] * SinusTable [_XAngle] * CosinusTable [_YAngle];
  _RotationMatrix [8] =  CosinusTable [_XAngle] *  CosinusTable [_YAngle];

  for ( _MultiplyLoop = 0; _MultiplyLoop < _Points; _MultiplyLoop++ )
  {
       MultiplyMatrix ( _RotationMatrix, _SourceMatrix + _MultiplyLoop * 3, _TargetMatrix + _MultiplyLoop * 3, 3, 3, 1 );
  }
}

//----------------------------------------------------------------------------

#endif

//----------------------------------------------------------------------------
FearumAsked:
Who is Participating?
 
robpittConnect With a Mentor Commented:
Given the matrix equation:

 A[rows][common] * B[common][cols] = C[rows][cols]


The algorithm for multiplication is...

for (i=0;i<rows;i++)
{
    for (j=0;j<cols;j++)
    {
        C[i][j]=0;
        for (k=0;k<common;k++)
        {
            C[i][j] += A[i][k] * B[k][j];
        }
    }
}

Expanding the [i][j] etc that becomes...

for (i=0;i<rows;i++)
{
    for (j=0;j<cols;j++)
    {
        C[i + j*rows]=0;
        for (k=0;k<common;k++)
        {
            C[i + j*rows] += A[i+k*rows] * B[k+j*rows];
        }
    }
}

...now compare that with yours and well there are a lot of differences.



Rob



PS I haven't studied your RotationMatrix setup at all.

0
 
AxterCommented:
What is your question?
0
 
smitty1276Commented:
A couple of questions...

1) Why are you doing this stuff the old fashioned/long way?  Why not just use the 3d APIs?

2) How are you determining your rotation matrix?

I'm sorry, but I'm just having a difficult time sorting out all of the matrices.  It would make the matrices easier to figure out (for us other programmers, anyway) if they were held in arrays that were dimensioned similarly to the actual matrix.  For example, when I see this....

float *_SourceMatrix, float *_SourceMatrix2

I naturally make the assumption that those are both matrices, and that they are probably similar.  But I become confused when I see something like...

x = _SourceMatrix[yada, yada] + _SourceMatrix2;

It would help if they were used like this...

float RotationMatrix[3][3];

not like this...

float *RotationMatrix;

0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
smitty1276Commented:
Robpitt's code is much, much more readable.  He is correct.  ALthough, I can't sort out your matrices, it does at least APPEAR at a glance to be incorrect.


Also... what's up with the sin/cos table?  You already included math.h.  Why don't you just convert the angle to radians and use the math library functions sin() and cos()?
0
 
FearumAuthor Commented:
I'll check your code robpitt,

And to answer smitty's questions:

 - This is a DOS real mode application
 - I do everything myself, so I understand and know what I am doing, I don't want to use a line routine, without knowing how it works, and then I can just as well make my own, which is faster and better coded :)
 - I am using pointers to a table of floats, because that allows for variable sizes, I must have done something wrong there, as it seems to overwrite stuff, the pointers go crazy??

If i do this:

float *Test=new float [10];

and then

Test[0]=.01;

will that affect the ptr?

Anyways the question is:

Why the hack my program doesn't work! (I'll now try to test robbrett's answer)
0
 
FearumAuthor Commented:
If you were standing besides me I'd give you a big hug and a bucket-o-beer :)

I could slap myself for making such a dumb mistake in matrix equations, I should have used other values to test my matrix :)

I myself can multiply matrices with ease, but when I had to make the PC do it I had to think hard, and when I;ve done it it proves to be wrong.

But instead of slapping myself silly - I'll laugh about my stupid mistake -  thank everyone that looked at my question, award robpitt with his points and learn from my mistake.

Thx for pushing my nose into the facts robpitt! You're a miracle!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.