Jump to content

Trig calculations not returning the correct values.

SilicateWielder

Hello,

 

So currently I am learning how to create a 3D graphics renderer, I'm still in the early phases of writing the code and have yet to even write fill algorithms for generating solid surfaces, let alone work out how that would even work in it's entirety.

 

Now, here is the equation I'm using.

x^1 = x * cos(Zr) - y * sin(Zr)

y^1 = x * sin(Zr) + y * cos(Zr)

x^2 = x * cos(Yr) - z * sin(Yr)

z^1 = x * sin(Yr) + z * cos(Yr)

y^2 = y^1 * cos(Xr) - z^1 * sin(Xr)

z^2 = y^1 * sin(Xr) + z^1 * cos(Xr)

 

Where X, Y, Z correspond to coordinates on a 3 dimensional coordinate system 

And Xr, Yr, and Zr correspond to degrees of rotation on a right-handed coordinate system.

 

Now, if I rotate a point at 45,45,45 by 90 degrees on all axis, I get these results

X = -44.768636

Y= 43.581672

Z= 43.774559

 

If the calculations were accurate the absolute value of the coordinate of that point should then be equal to 45, however this is not the case, and running the code long enough will cause the object to shrink until it's just a tiny blit on my screen.

 

Could anyone explain what might be causing this? I'm currently attempting to implement a buffer to keep the effect minimal, however there are issues getting values to pass into the list and then into my projection function for drawing.

My procrastination is the bane of my existence.

I make games and stuff in my spare time.

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Most programs I've worked with have done trig calculations in radians, not degrees. If you are doing your calculations in radians (Pi/2 as 90) then what are you using for Pi? 3.14? 

Link to comment
Share on other sites

Link to post
Share on other sites

14 minutes ago, corrado33 said:

Most programs I've worked with have done trig calculations in radians, not degrees. If you are doing your calculations in radians (Pi/2 as 90) then what are you using for Pi? 3.14? 

I'm doing my calculations in degrees, and not radians, so I'm not even using Pi.

My procrastination is the bane of my existence.

I make games and stuff in my spare time.

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

23 minutes ago, M.Yurizaki said:

That would be your problem probably. Trig functions in almost all programming languages expect radians.

I've looked up a few (just to be sure), and C++ (among others) uses radians, not degrees. So the above is correct, at least to my knowledge. I'm sure there are other header files that are programed to use degrees instead, but the default math.h or tgmath.h uses radians. 

 

Seriously, it's not hard to try. Instead of typing 90 just type in 1.570796 (which is Pi/2)

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, corrado33 said:

I've looked up a few (just to be sure), and C++ (among others) uses radians, not degrees. So the above is correct, at least to my knowledge. 

It would make sense to me, considering programming languages were usually designed by mathematicians and radians is the preferred angle unit.

 

https://qedinsight.wordpress.com/2011/03/14/why-radian-measure-makes-life-easier-in-mathematics-and-physics/

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, M.Yurizaki said:

It would make sense to me, considering programming languages were usually designed by mathematicians and radians is the preferred angle unit.

 

https://qedinsight.wordpress.com/2011/03/14/why-radian-measure-makes-life-easier-in-mathematics-and-physics/

Oh you don't have to tell me, I'm a mathematician and scientist. I haven't worked in degrees in years... :)

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, M.Yurizaki said:

You should try mils some time. :)

Oh god don't get me started. I recently had something machined and I was confused for a while until I understood what they were actually saying. ("A few mils" wtf? no it's a few thousandths of an inch... not mm ohhhh) (I'm in the US, so all machine shops work in imperial measurements.)

Link to comment
Share on other sites

Link to post
Share on other sites

MX33 * MY33 * MZ33  = Rotation. Multiply each rotation matrix of each axis together and you get a combined rotation. You then would multiply your3D coordinate vector by this rotation matrix. Also set the rotation matrix to an identity after each use because if you don't you will apply rotation upon rotation and things will get screwed.

 

https://en.wikipedia.org/wiki/Rotation_matrix

 

Use a 3D math library such as DirectXMath if you don't want to spend the time to create your own.

CPU: Intel i7 - 5820k @ 4.5GHz, Cooler: Corsair H80i, Motherboard: MSI X99S Gaming 7, RAM: Corsair Vengeance LPX 32GB DDR4 2666MHz CL16,

GPU: ASUS GTX 980 Strix, Case: Corsair 900D, PSU: Corsair AX860i 860W, Keyboard: Logitech G19, Mouse: Corsair M95, Storage: Intel 730 Series 480GB SSD, WD 1.5TB Black

Display: BenQ XL2730Z 2560x1440 144Hz

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×