Hi all:
I normally use Allegro5 rotation function! But I decided to try my
hand at using the sine and cosine functions. BAD IDEA! Now I have a
program that is a complete MESS! My image is dancing on the screen
like crazy! I believe my rotation part is correct hopefully. Computer
is returning all kinds of errors!! Don't have a clue what is wrong!
Dizzy has helped me before on some 3D rotations but hasn't help me
on 2D rotations. I have check back on some 2D rotations I did in DOS
and think I am correct but who knows. I have uploaded the program if
someone wants to take a look at it. Thanks for your time. I have never
seen anything like this before!
Hello Scooter. You are looping 4 times when the timer changes, and clearing/drawing 4 times. Why did you need to loop 4 times? Also, radians work for al_rotate_image but you don't need them if you just want your square to rotate around the center.
Try my attached code, should help you get started....
Hi Dizzy:
Thanks for the reply! Works great! I have worked most of the day
on trying to rotate image at its center. Below is all I could come
up with, which is what I had before. Using your program I plugged
in another formula but still doing the same thing, Only difference
is the program exits properly with no errors.
I know I have to zero out the coordinate, do the rotation and add back
in what i subtracted out but something is still missing somewhere.
Here is what I have now:
if (ev.type == ALLEGRO_EVENT_TIMER &&
al_event_queue_is_empty(event_queue))
{
update_angle();
box_x[0] -= 640;
box_y[0] -= 360;
new_x = (box_x[0] * cos(angle)) - (box_y[0] * sin(angle));
new_y = (box_x[0] * sin(angle)) + (box_y[0] * cos(angle));
box_x[0] = new_x + 640;
box_y[0] = new_y + 360;
redraw = true;
}
Now I see why you were using 4 co-orinates, you wanted to draw a box with the 4 x/y points. You could do that using al_draw_prim with a texture, but personally I would just use al_draw_rotated_bitmap.
If you really want to rotate around the center, but don't want to use al_draw_rotated_bitmap, then I would just use a transform. I've attached an example for you.
If you want to use the 4 x/y co-oridinates, let me know and I'll do a demo for you using al_draw_prim.
EDIT:
I've attached another example, using vertices, which is closer to what you originally wanted. This method:
void rotate_vertices() { float angleR = angle * PI / 180.0; for (int i = 0; i < 4; i++) { box_x[i] = cos(angleR) * (original_x[i] - center_x) - sin(angleR) * (original_y[i] - center_y) + center_x; box_y[i] = sin(angleR) * (original_x[i] - center_x) + cos(angleR) * (original_y[i] - center_y) + center_y; } }
will do what you were trying. Notice before drawing that you have to set the image as target and transform it, otherwise you get a spinning square with a non-spinning texture!
EDIT2:
For some reason I can't attach the second version, so here is the full code, using the 4 points:
Hi Dizzy:
I have discovered my problem. What I need is to rotate the upper
left corner of the image at 1 degree intervals counter clockwise
using this formula:
new_x = (box_x[0] * cos(angle)) - (box_y[0] * sin(angle));
new_y = (box_y[0] * cos(angle)) + (box_x[0] * sin(angle));
I wrote a separate program to determine that this will not work
with allegro5. I believe you mention in your post that it did not
work without declaring the image a target and using transformations.
Is there any way possible to do it without using transformations?
I don't understand why it doesn't work, I guess its the way the library
was written. I would like to not use them if possible. This is the
reason the image was jumping all over the screen because those
functions do not work on their own. I did not know that. If you have
the time, could you work something up for me?
Thanks for your time!!!!
Let's say you have a rectangle with 4 points.
1. pick a point of rotation cx, cy
2. center it to your point of rotation by subtracting cx, cy from px, py
3. apply rotation to point (your formula above)
4. add cx, cy back to px, py
repeat steps 2 to 4 for each point in your rectangle
Actually that what Dizzy put here
void rotate_vertices() { float angleR = angle * PI / 180.0; for (int i = 0; i < 4; i++) { box_x[i] = cos(angleR) * (original_x[i] - center_x) - sin(angleR) * (original_y[i] - center_y) + center_x; box_y[i] = sin(angleR) * (original_x[i] - center_x) + cos(angleR) * (original_y[i] - center_y) + center_y; } }
Scooter, I'm not really sure what you're asking for. You already had a rotating image in the centre when I helped with this post:
I don't know why you don't want to use al_draw_rotated_bitmap. If you want to use 4 points like you originally tried, I posted that above in Edit2.
If you use al_draw_prim you have to transform the texture or it will be a spinning square with a non-spinning texture.
At this point I don't know why you don't just use al_draw_rotated_bitmap, like you did in your shoot_a_ball question??
You can't rotate an image using al_draw_bitmap, because it just draws a bitmap at x/y?
Again, I'm not sure what you're trying to achieve....
Hi Dizzy:
I have been trying to work with transformations, Not going
too good. It's trying to rotate the whole screen. Not sure why.
I am going to try a very simple program to see if I can get a
handle on what is going on. I am still confused an how this works.
Maybe I need to find a tutorial on this. Can't get my head around
not using coordinates like before.
Stay tuned, it"s going to be a bumpy ride!
You need to get a copy of the transform before you enter your game loop. Then after making a new transform and drawing (rotating), set back the original before doing any more drawing.
Look at the code for rotating a bitmap.
It saves current transform. Processes all needed data for the bitmap transform. draws the bitmap, then restores the backup to the current transform.