I wanted to ask, how can i make a square rotate in base of
event.mouse.x
event.mouse.y
The square is al_draw_rectangle(x-15,y-15,x+15,y+15,al_map_rgb(0,0,255),3.0);
Is there any way to do that? The square cannot move with mouse, only with keyboard.
Your question is a bit incomplete... let me use my A5 mind reading addon and guess what you actually want.
You can do that using transformations:
ALLEGRO_TRANSFORM trans; al_identity_transform(&trans); al_translate_transform(&trans, -cx, -cy); //cx,cy are set to the mouse coordinates in the mouse event al_rotate_transform(&trans, theta); al_translate_transform(&trans, cx, cy); al_use_transform(&trans); al_draw_rectangle(x - 15, y - 15, x + 15, y + 15, al_map_rgb(0, 0, 255), 3.0); /* Reset transform */ al_identity_transform(&trans); al_use_transform(&trans);
Your code works perfect. But i need to rotate the square.
Here is what i want to do:
The square must be at the center and wont move. If i move the mouse, it wont move again.
It will only rotate in base of the mouse e.mouse.x and e.mouse.y
Sorry, what? You need to be clearer.
What I gather so far :
1) The square is centered on the center of the screen.
2?) You want to rotate the square according to the direction from the center of the square to the mouse position?
double theta_radians = atan2(event.mouse.y - square_center_y , event.mouse.x - square_center_x);
Then use a transform to center on the center of the square, then add in a rotate transform to rotate by theta_radians, then draw the square.
I have used the AMCerasoli video of networking, because it is clearer.
You see the player rotates when he moves the mouse?
I want to make the same thing, just with a square.
Also, the code of siege worked, but the center was not the square, but the top left corner of the screen. How to change the center?
How about this:
ALLEGRO_TRANSFORM trans; al_identity_transform(&trans); al_rotate_transform(&trans, theta); al_translate_transform(&trans, x, y); al_use_transform(&trans); al_draw_rectangle(-15, -15, 15, 15, al_map_rgb(0, 0, 255), 3.0); /* Reset transform */ al_identity_transform(&trans); al_use_transform(&trans);
You'd compute the theta using the formula Edgar posted.
Same thing again, it just moved a little
Who in the world understands this from the manual
"Apply a translation to a transformation."
Also, i tried scale transform and it didnt work at all with the mouse.
atan2 is wrong in my configuration after including math.h which wasnt even mentioned to use:
double theta_radians = atan2(e.mouse.y - (x + 15) , e.mouse.x - (y - 15));
Same thing again, it just moved a little
Then you're doing something wrong, as this example shows that it works fine:
atan2 doesnt work. I dont know why. I am using visual studio 2012
I copy pasted your code and it doesnt work on my machine only because of atan2
Are you linking with math.lib?
I added the math.lib in the Linker -> General -> Additional Library Dependencies
Used this path
C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include;%(AdditionalLibraryDirectories)
Then i tried this other one
C:\Program Files %28x86%29\Microsoft Visual Studio 11.0\VC\include
Still the same
theta = atan2(my - y, mx - x);
I have the same problem 
Try this
theta = atan2((float)(my - y), (float)(mx - x));
Theta in this case must be float 
I am using visual studio 2012
o.O
Visual Studio for Windows 8?
Is there Intelisense? I'm using 2008, because 2010 haven't got intelisense :/
And I think, that this is better way to do rotate something:
Theta in this case must be float
There is no integer version of atan2, that's just nonsensical.
What does this code print?
#include <iostream> #include <typeinfo> #include <math.h> int main() { std::cout << typeid(atan2((int)0, int(0))).name() << std::endl; return 0; }
And I think, that this is better way to do rotate something:
My way is better for primitives and in fact works for all drawables, while your way only works for bitmaps.
Oh... but there is double, long dobule and float atan2 version!
In fact, you can create bitmap from all drawable primitives and then rotate it 
While I compile your code, there is error:
error C2668: 'atan2' : ambiguous call to overloaded function
c:\program files\microsoft visual studio 9.0\vc\include\math.h(547): could be 'long double atan2(long double,long double)'
c:\program files\microsoft visual studio 9.0\vc\include\math.h(499): or 'float atan2(float,float)'
c:\program files\microsoft visual studio 9.0\vc\include\math.h(110): or 'double atan2(double,double)'
while trying to match the argument list '(int, int)'
I must do cast to float.
Sory, but my previous words are logical
In fact, you can create bitmap from all drawable primitives and then rotate it
Inefficient and unnecessary.
While I compile your code, there is error:
Well a compile error makes sense. It compiling without error and then not functioning does not. Either way, the original code is C not C++
.
But if you using bitmaps in this case, application works better 
Processor has less to count
At least I think so...
But if you using bitmaps in this case, application works better
If you only need to rotate a single pre-existing bitmap, then yeah... no need for transformations.
Well, I'm currently not using transformations at all...
@codestepper
WOW THANKS! It works perfect! Can you make some notes in english to the program so i can understand it better? I mean especially the transformation, because i dont understand it well.
Visual Studio 2012 is the same as 2010, with some changes and it has intellisense.
I have had 2010 and it had intellisense, i dont know what version you had. I have worked on both for Allegro 5. I recently moved a game from VC9 to VC10 and it was very easy and fast.
I dont get it why it is a bad thing to do it like that, because it works 100%
Also, if anyone can make a pacman which opens its mouth using the al_draw_pieslice that would be great as i was trying to do it today without any success.
@cerasoli
What is your method of doing it?
Hmm... i think i don't make any bugs in comments 
If yes, sorry 
Sorry for variable names - some of them don't reflect their roles
So, code for pac-man primitives version with comments:
Wow, you are amazing! 
Thank you very much man, it worked perfect
I forgot about this:
redraw = false;
Paste this before:
al_clear_to_color(al_map_rgb_f(0, 0, 0));
And it will run better