advanced 3D graphics

Started by
7 comments, last by Zipster 24 years, 1 month ago
ha! this isn''t about 3D graphics. my other post wasn''t getting any attention , even though it was a problem all you hard-core programmers could solve in 3 nano seconds. here it is; its not hard (well, to me it is). i am working on my polygon engine, and for some reason there is a delema. i have a function that rotates a polygon, but however, whenever i pass the function a positive angle, the polygon rotates the polygon clock-wise, and vise versa, which is wrong. i know i can easily get around it by negating the angle, but it''s still bothering me. heres the code (BTW, i have look-up tables for the sin and cos functions for optimization, and i''m SURE they work): int Rotate_Polygon(POLYGON_PTR poly, int angle) { int index; if (poly->side == NULL) { return(-1); } if (angle < 0) { angle += 360; } for (index = 0; index < poly->num_sides; index++) { float xr = (float)poly->side[index].x1*cos_look[angle] - (float)poly->side[index].y1*sin_look[angle]; float x2r = (float)poly->side[index].x2*cos_look[angle] - (float)poly->side[index].y2*sin_look[angle]; float yr = (float)poly->side[index].x1*sin_look[angle] + (float)poly->side[index].y1*cos_look[angle]; float y2r = (float)poly->side[index].x2*sin_look[angle] + (float)poly->side[index].y2*cos_look[angle]; float cx = (float)poly->side[index].Cen_X*cos_look[angle] - (float)poly->side[index].Cen_Y*sin_look[angle]; float cy = (float)poly->side[index].Cen_X*sin_look[angle] + (float)poly->side[index].Cen_Y*cos_look[angle]; poly->side[index].x1 = xr; poly->side[index].y1 = yr; poly->side[index].x2 = x2r; poly->side[index].y2 = y2r; poly->side[index].Cen_X = cx; poly->side[index].Cen_Y = cy; } return(1); }
Advertisement
I could be going mad, but it looks like you have a left handed matrix transformation going on, and you need a right handed one.
This is the rotation formula:
xr = x cos a - y sin ayr = x sin a + y cos a 


From what I can see this is exactly what you are using. What you are missing is that the computer screen has the origo at the top-left corner, as opposed to your normal cartesian system that has it in the bottom left corner.





Edited by - Spellbound on 3/13/00 4:34:55 PM
unfortunatly, im no trig genius. all i know is that the formula works. when i was programming in DOS in Mode X 320x240, i used the exact same code and it worked.

however, i understand what you mean. so how do i fix it? switch sin and cos? switch signs? both?
If I remember it correctly it should be enough to transpose the transform matrix. Or as in the case with separate formulas:

xr = x cos a + y sin a
yr = -x sin a + y cos a

I''m not sure though. Try it, if it wasn''t correct then ask again.

Just negate all the sin terms to convert between rt-handed and lt-handed systems. This is exactly the same as simply negating the angle. This is why:

sin(-x) == -sin(x)
cos(-x) == cos(x)
Mike Weldon, a.k.a. FalloutBoymweldon@san.rr.com
thats what ive been doing to solve the problem. i just wasn''t sure if that was the most "professional" way to do it.
If it works and is fast, it''s profressional. (Actually, in my experience if it works most of the time and is somewhat fast then it is professional. I''m not bitter, oh, no of course not.)

Wraith
BasketQase Software
[email=cdickinson@scu.edu]Wraith[/email]BasketQase Software
In my opinion, the professional way is to understand what goes on.

This topic is closed to new replies.

Advertisement