Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » rotating primitives?

This thread is locked; no one can reply to it. rss feed Print
rotating primitives?
sensmaster
Member #14,767
December 2012

What is the best way to rotate a primitive?
For example, I am trying to rotate a rectangle with a reference point.
I have no idea how to do this..

Please help me :(

William Labbett
Member #4,486
March 2004
avatar

Take a look at this :-

https://www.allegro.cc/manual/5/al_draw_rotated_bitmap

Is it what you need ?

You could make a bitmap of the rectangle first and then rotate it.

SiegeLord
Member #7,827
October 2006
avatar

/* Setup the transformation */
ALLEGRO_TRANSFORM trans;
al_identity_transform(&trans);
al_translate_transform(&trans, -ref_x, -ref_y);
al_rotate_transform(&trans, angle_in_radians);
al_translate_transform(&trans, ref_x, ref_y);

al_use_transform(&trans);
/* Draw whatevs here */

/* Turn off the rotation */
al_identity_transform(&trans);
al_use_transform(&trans);

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

sensmaster
Member #14,767
December 2012

First of all,
thanks for the reply.

I am not asking for the bitmap to rotate,
Just a simple primitive that I have drawn using
al_draw_rectangle(...)

any ideas? :(

SiegeLord
Member #7,827
October 2006
avatar

Quote:

any ideas? :(

Did I post in invisible ink or something?

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Arthur Kalliokoski
Second in Command
February 2005
avatar

SiegeLord said:

Did I post in invisible ink or something?

It's all geek to him...

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

sensmaster
Member #14,767
December 2012

I have seen your code, but doesn't that rotate the whole screen?

For instance, I have a circle and a rectangle.
I only want to rotate the rectangle and circle will remain as it is.
If I use your code, doesn't it rotate the whole screen?

Arthur Kalliokoski
Second in Command
February 2005
avatar

It rotates whatever's drawn next until the al_identity_transform() and al_use_transform() resets it to normal.

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

sensmaster
Member #14,767
December 2012

oh thanks...
I am new at this :(

Thanks so much. I got it working now.

I have one more question.
Could you please explain how its working?

al_translate_transform(&trans, - ref_x, - ref_y);
al_rotate_transform(&trans, Degrees * 3.1415 / 180);
al_translate_transform(&trans, ref_x, ref_y);

I do not understand those 3 steps...

Arthur Kalliokoski
Second in Command
February 2005
avatar

SiegeLord said:

al_translate_transform(&trans, - ref_x, - ref_y);
al_rotate_transform(&trans, Degrees * 3.1415 / 180);
al_translate_transform(&trans, ref_x, ref_y);

al_translate_transform() moves it across/updown to set position on the screen. The reason the minus signs are stuck in there is because you don't move the camera, you move the world (think about that a minute).

al_rotate_transform() twists it around to rotate it.

al_translate_transform() moves it once again on screen to re-adjust position because al_rotate_transform() moved it from the rotation point.

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

Thomas Fjellstrom
Member #476
June 2000
avatar

The main reason for the two transforms is it makes it so your origin is the center of the object you're trying to rotate. If you didn't transform like that, you'd end up rotating around a different center, and not actually just rotating your object.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Go to: