Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Pivot

This thread is locked; no one can reply to it. rss feed Print
Pivot
scriptX
Member #6,574
November 2005
avatar

How can I pivot a bitmap so that it follows the mouse, like this? I've got:

pivot_sprite(buffer, playerbuffer[PLAYER_ARM],
     (playerx+(playerbuffer[PLAYER_CURRENTFACING]->w/2)),
     (playery+(playerbuffer[PLAYER_CURRENTFACING]->h/2)),
     16,
     5,
     itofix( ???????? ));

??????? Should be...

Pedro Avelar Gontijo
Member #5,372
January 2005
avatar

I didn't quite get what you want. Is it just a line? If so, do

line (bitmap, sprite_center_x, sprite_center_y, mouse_x, mouse_y, color);

----------
兩隻老虎,兩隻老虎,跑得快!

scriptX
Member #6,574
November 2005
avatar

Sorry, no it's a bitmap.

Thomas Harte
Member #33
April 2000
avatar

Otherwise look into atan2 and remember to convert the output of that (radians) correctly to the input of pivot_sprite (Allegro units in fixed point).

Pedro Avelar Gontijo
Member #5,372
January 2005
avatar

Then explain better what are you rotating ;)
[edit]
Beaten and not understanding a thing ;D
[edit]
Now understanding but still beaten ;D

----------
兩隻老虎,兩隻老虎,跑得快!

scriptX
Member #6,574
November 2005
avatar

thanks thomas.

pedro: bitmap

[edit]
Negatory...

pivot_sprite(buffer, playerbuffer[PLAYER_ARM],
     (playerx+(playerbuffer[PLAYER_CURRENTFACING]->w/2)),
     (playery+(playerbuffer[PLAYER_CURRENTFACING]->h/2)),
     16,
     5,
     itofix(atan2((double)mouse_y, (double)mouse_x)));

It moves about 1 pixel.

Jonatan Hedborg
Member #4,886
July 2004
avatar

No idea what you want, but doesent

pivot_sprite(buffer, playerbuffer[PLAYER_ARM],
     (playerx+(playerbuffer[PLAYER_CURRENTFACING]->w/2)),
     (playery+(playerbuffer[PLAYER_CURRENTFACING]->h/2)),
     16,
     5,
     itofix(atan2((double)mouse_y, (double)mouse_x)));

require that atan2 returns an integer, when it (if we're talking math.h here) actually returns radians (double)?

-------
Sweden: Free from the shackles of Democracy since 2008-06-18!

scriptX
Member #6,574
November 2005
avatar

...
itofix((int)(atan2((double)mouse_y, (double)mouse_x))));

Still doesn't work (moves 1 pixel)

gnolam
Member #2,030
March 2002
avatar

Gah.

Thomas said:

Otherwise look into atan2 and remember to convert the output of that (radians) correctly to the input of pivot_sprite (Allegro units in fixed point).

1) Use ftofix(). Casting atan2's output to int will just throw away information (hint: how many integers are there between -π and +π?). :P

2) Convert between radians (period: 2π ) and Allegro units (period: 256).

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

miran
Member #2,407
June 2002

Quote:

itofix((int)(atan2((double)mouse_y, (double)mouse_x))));

Hmm, wouldn't you want to convert to a local coordinate system for the sprite you want to pivot? Like this:

ftofix(radians_to_allegro_degrees(atan2(sprite_y - mouse_y, mouse_x - sprite_x))));

--
sig used to be here

scriptX
Member #6,574
November 2005
avatar

Quote:

2) Convert between radians (period: 2π ) and Allegro units (period: 256).

wtf?

[edit]
Let me rephrase that... I did read that 2π = 360 degrees (correct?). So are you saying that 2π = 256 Allegro units?

Jonatan Hedborg
Member #4,886
July 2004
avatar

Yes. One allegro "full circle" is 256 units.
Also, what mirian said is probably a good idea.

-------
Sweden: Free from the shackles of Democracy since 2008-06-18!

scriptX
Member #6,574
November 2005
avatar

I'm having trouble with this.

...
ftofix((atan2(playery - mouse_y, mouse_x - playerx)) * (128/3.14) ));

I chose 128 because it's half of 256. 180 is half of 360.

Kitty Cat
Member #2,815
October 2002
avatar

What's the trouble? Other than the 180 is an integer which may cause the compiler to return an integer, which would be 0 (or it may not, dunno).

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

scriptX
Member #6,574
November 2005
avatar

...
ftofix((atan2(playery - mouse_y, mouse_x - playerx)) * (128.0/3.14) ));

KittyCat: It's not "following" the mouse. In fact, it appears to face the opposite direction and I've tried:

...
//     /
ftofix(- ((atan2(playery - mouse_y, mouse_x - playerx)) * (128.0/3.14) )));

[edit]
a4hj.jpghttp://img386.imageshack.us/img386/2571/a4hj.jpg[/img]

Go to: