Advertisement

Trigonometry, matrix math, and radians...

Started by January 12, 2001 09:15 PM
11 comments, last by Yanroy 24 years ago
All of the math stuff in my subject I think I understand, at least on paper (isn''t all math on paper? ). I am having a bit of trouble writing an image rotation function. I thought it would be logical to fudge multipling the image matrix by a matrix that looks like this: [ cos(angle) sin(angle)] [-sin(angle) cos(angle)] Just one problem... all of the math I know is in degrees, and the cos, sin and tan functions are in radians for C++. If someone could provide me with a deg to rad conversion function (macro?), that would help a lot. I have a question regarding that, however: does the same math I know still work when using radians. I am by no strech of the imagination good with radians. About all I know is that 2 * pi = 360 degrees, and I could probably use that to make a conversion function, but I am not sure what the pi will do to the floating point stuff... I guess what I am asking is that is I work that matrix math into a formula, and put in a loop, running it on every pixel in my image, and then plot the resulting pixel at its new location on a second DD surface (determining the size of which is giving me a headache...), and then blt that to the backbuffer, will I have a rotated image? If you could distill my rambling into a coherent idea, you don''t need to upgrade your computer --------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming

You are unique. Just like everybody else.

Yanroy@usa.com

Visit the ROAD Programming Website for more programming help.

--------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

Formula for degrees to radians:

radians = degrees / 180 * pi

For my game I''m making, I''m using arrays with various numbers to represent colors. I send the array to a function which reads each one and plots the pixel. I also send a facing: up, right, left, down, using the same array. This was accomplished simply by plotting the pixels in a different order...sort of .

I think the same stuff applies, as long as you use radians instead.

Anyway, I hope this helps. Let me know if I have your all wrong

Peon




Peon
Advertisement
2 * PI = 360 degrees
there are PI radians in a circle
therefore:

Rad = Deg * (PI * 2 / 360)
or:
Rad = Deg * (PI / 180)

So the macro is:

#define DegToRad(Deg) (Deg * (PI / 180))

for conversions from radians to degrees its just the opposite:

#define RadToDeg(Rad) (Rad * (180 / PI))

Edited by - Quantum on January 13, 2001 11:03:36 PM

Edited by - Quantum on January 13, 2001 11:07:06 PM
Bah, Peon beat me to it by 11 seconds
Hehe, lol

I finally helped someone (I''m usually wrong )
Peon
You have both been extremely helpful... now if I can just remember to offset the pixels in my image to it rotates around the origin, not the top left corner, I should be all set.

Actually, I shouldn''t say that. Is there a way of determining the size of a surface to hold an x by y image when it has been rotated? It won''t exactly fit in the same square anymore... I will go try to rotate something while I wait for a reply. Thanks.

--------------------


You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming


You are unique. Just like everybody else.

Yanroy@usa.com

Visit the ROAD Programming Website for more programming help.

--------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

Advertisement
Well, I got the rotation function working. It looks horrid. As the image rotates, there are holes in it where you can see the background, and also sometimes there are "clumps" where there aren''t any holes... There must be some way to fix this. And I still haven''t figured out how to determine the size of a surface needed to contain the rotated image, so if anyone could help with that, too, it would at least replace my current code that searches through the entire surface to build a RECT that contains the image. Another little problem I seem to be having is that it doesn''t rotate one degree at a time. If I change the input just a little bit (1 degree), the image jumps about 30 degrees! I am betting that is a problem with the deg to rad conversion, but I can''t find anything amiss, and I think I am casting all the right stuff to doubles... Please help!

--------------------


You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming


You are unique. Just like everybody else.

Yanroy@usa.com

Visit the ROAD Programming Website for more programming help.






--------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

Yanroy

The rotations are not working right because Quantum has the macros reversed. They should be...

#define DegToRad(Deg) ((Deg) * (PI / 180.0f))
#define RadToDeg(Rad) ((Rad) * (180.0f / PI))







I''m starting to feel like this thread is a monologue, but I figured out why it is jumping in 30 degree or so hops. The deg-to-rad macro is wrong! That was the only thing I could think of, so I tried to work it out on my own:

360 deg = 2 * PI rad

So I made the degrees into a fraction of a circle:

(Angle / 360) = some part of a circle

If a whole circle is 2 * PI, then part of a circle is:

(Angle / 360) * 2 * PI
or
(Angle / 180) * PI

I put that formula in my program, and voila (how do you do accented characters?), it works! It still rotates in the wrong direction, but that is another problem. And there are still holes in it, but it turns at a normal rate. Any help with my many other problems would be great.

Peon: your formula is correct, and Quantum either made a mistake with the parentheses or I don''t understand what he is driving at.

--------------------


You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming


You are unique. Just like everybody else.

Yanroy@usa.com

Visit the ROAD Programming Website for more programming help.

--------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

First of all, I am in no saying that an image rotation function should use trig at all for rendering...it''s way too slow. BUT if you wanted to do it like you did, you would have to go about it in the opposite way. you are taking each pixel in the source image, and calculating the desination, what you need to do is take every image in the destination, and calculate the source. that way you will have no missing pixels like yours does.

if you want to do it right, you should only calculate the rotation of the corners of the image, and then transvers horizontaly, threw the rotated square, and calculate the source pixel. I know that doesn''t help you much, but I think GameDev has an article or two on the subect.

This topic is closed to new replies.

Advertisement