C++ Rotation function works in 2D perspective but fails in 3D

In summary: The function rotates an object around its origin by using the following four arguments:u is the rotation angle in degreesOx is the X coordinate of the rotation centerOy is the Y coordinate of the rotation centerOx2 and Oy2 are the new X and Y coordinates after the rotationUb is the angle of rotation in degreesThe function returns the new X and Y coordinates after the rotation.
  • #1
dot_binary
6
0
I'm making a 3D rotation of a poligon.The problem is when I rotate some points in 2D, it works, but if I use the same function to rotate the same points in a 3D perspective , while rotating the vertices move to the center of the object and remain there.
I also wrote the 3D rendering part.At first I thought that my rotation function is not precise, I made it more precise and still it it doesn't work.The 3D rendering function only takes the values stored in the objects array and puts vertices on screen.

The values stored in o[0][0] and o[0][1] represent the X and Y coordonates of the origin
and the ones stored in o[1][0] and o[1][1] are the coordonates of a point.
For now I'm only rotating on X and Y axis.

This is the rotation function:

int rotate(int t,double Ox, double Oy , double Ox2, double Oy2 , double Ub ){
double pi = 3.14159265 ;

if( Ub > 359 ){ Ub = Ub-360; }
if( Ub < 0 ){ Ub = 360-abs(int(Ub)); }

double U = (Ub*pi)/180 ;
double Tx = Ox2 - Ox ;
double Ty = Oy2 - Oy ;
double x = ( Tx *cos( U ) ) - ( Ty *sin( U ) );
double y = ( Ty *cos( U ) ) + ( Tx *sin( U ) );

Ox2 = floor( (Ox + x) + 0.5 ) ;
Oy2 = floor( (Oy + y) + 0.5 ) ;

if ( t == 1 ){
return int(Ox2) ;
}else{
return int(Oy2) ;
}
}


This is how I'm doing the rotation of one point around another point:

o[1][0] = rotate(1,o[0][0],o[0][1],o[1][0],o[1][1],Ub2);
o[1][1] = rotate(2,o[0][0],o[0][1],o[1][0],o[1][1],Ub2);

where Ub2 is the angle of rotation.
 
Technology news on Phys.org
  • #2
You didn't explain why you are doing all the conversions between int to double so I don't really understand what the routine is doing, but I'll take a guess at the problem.

The first call to rotate() changes the values of o[1][0].
In the second call, should you pass in the original value of o[1][0], not the changed value?

Ignoring the int-to-double conversions, it would probably be better to change the routine to
Code:
void rotate(int t,double Ox, double Oy , double& Ox2, double& Oy2 , double Ub )
and update both of Ox2 and Oy2 with one call
 
  • #3
The first call changed the value of o[1][0] , that was the problem.I overlooked that.
Thank you for pointing that out.
 
  • #4
I still have a problem with my rotation function.

I rewrote the rotation function because the previous one didn't work as I wanted.
Now if I rotate a line in a 2D space, if I give it fixed values linke this:

line(400,400,rotate(1,400,400,400,600,Ub2),rotate(2,400,400,400,600,Ub2));

...it works but if the values come from an array, the line shrinks while rotating.And I'm shure I didn't made the mistake I previously made.
This is how I do the rotation now:

rx = rotate(1,aq[1][1],aq[1][2],aq[2][1],aq[2][2],Ub2) ;
ry = rotate(2,aq[1][1],aq[1][2],aq[2][1],aq[2][2],Ub2);
aq[2][1] = rx ;
aq[2][2] = ry ;
line(aq[1][1],aq[1][2],aq[2][1],aq[2][2]);

The rotation function:

int rotate(int u ,int Ox, int Oy , int Ox2, int Oy2 , float Ub ){
float PI = 3.1415926535;

if( Ub > 359 ){ Ub = Ub-360; }
if( Ub < 0 ){ Ub = 360-abs(int(Ub)); }

int Tx = abs(Ox2 - Ox) ;
int Ty = abs(Oy2 - Oy) ;

// get size of line
int hkj = int( round( sqrt( (Tx*Tx) + (Ty*Ty) ) ) );

if( ( Ub >=0 ) and ( Ub < 90 ) ){
Ox2 = int( sin((Ub*PI)/180) * hkj );
Oy2 = int( cos((Ub*PI)/180) * hkj );
}
if( ( Ub >=90 ) and ( Ub < 180 ) ){
Ox2 = int( cos(((Ub-90)*PI)/180) * hkj );
Oy2 = int( (sin(((Ub-90)*PI)/180) * hkj)* -1);
}
if( ( Ub >=180 ) and ( Ub < 270 ) ){
Ox2 = int( (sin(((Ub-180)*PI)/180) * hkj )* -1);
Oy2 = int( (cos(((Ub-180)*PI)/180) * hkj )* -1);
}
if( ( Ub >=270 ) and ( Ub < 360 ) ){
Ox2 = int( (cos(((Ub-270)*PI)/180) * hkj )* -1 );
Oy2 = int( sin(((Ub-270)*PI)/180) * hkj ) ;
}

if ( u == 1 ){
return Ox+Ox2 ;
}else{
return Oy+Oy2 ;
}
}
 
  • #5


I would first suggest checking the mathematical accuracy and precision of your rotation function. It is possible that there may be errors or limitations in the calculations that are causing the issue in the 3D perspective. I would also recommend testing the function with different angles and points to see if the issue persists.

Another aspect to consider is the order of operations in your rotation function. In 3D rotations, the order in which the rotations are applied can affect the final outcome. It is important to use a consistent and standardized method for 3D rotations to ensure accuracy.

Additionally, I would suggest checking the 3D rendering function to ensure that it is properly interpreting and displaying the rotated points. It is possible that there may be an issue in the rendering code that is causing the points to move to the center of the object.

In summary, it is important to thoroughly review and test both the rotation function and the rendering function to identify and address any potential sources of error. It may also be helpful to consult with other experts in the field of 3D graphics for further insights and suggestions.
 

1. Why does the rotation function in C++ work in 2D perspective but not in 3D?

The rotation function in C++ works by manipulating the coordinates of points in a 2D plane based on a specified angle. This works well in 2D because all points lie on the same plane and can be easily rotated around a single axis. However, in 3D, points are located on different planes and rotating them around a single axis may cause distortions or unexpected results.

2. Can the C++ rotation function be modified to work in 3D?

Yes, the C++ rotation function can be modified to work in 3D by using matrices or quaternions. These mathematical operations allow for rotation in 3D space around multiple axes, making it more suitable for 3D graphics applications.

3. Is there a difference in the code for 2D and 3D rotation in C++?

Yes, there is a difference in the code for 2D and 3D rotation in C++. In 2D, the rotation function typically takes in an angle as a parameter, while in 3D, it may take in multiple angles or a rotation matrix. Additionally, the calculations for 3D rotation are more complex and involve vector operations.

4. What are some common errors when implementing the C++ rotation function in 3D?

Some common errors when implementing the C++ rotation function in 3D include using incorrect mathematical operations, not properly accounting for the 3D coordinate system, and not considering the order of rotations. It is important to thoroughly understand the mathematical concepts behind 3D rotation and carefully implement them in the code.

5. Are there any alternative approaches to rotation in 3D besides the C++ function?

Yes, there are alternative approaches to rotation in 3D besides the C++ function. These include using libraries or graphics engines that have built-in functions for 3D rotation, using other programming languages with more advanced graphics capabilities, or implementing your own custom rotation algorithm specific to your project's needs.

Similar threads

  • Programming and Computer Science
Replies
1
Views
947
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
Replies
5
Views
2K
  • Programming and Computer Science
Replies
2
Views
4K
  • Programming and Computer Science
Replies
1
Views
2K
Replies
2
Views
714
  • Differential Equations
Replies
1
Views
2K
  • Programming and Computer Science
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
Back
Top