Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Yanroy

Trigonometry, matrix math, and radians...

This topic is 6514 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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))


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!