Pivot
scriptX

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

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

Sorry, no it's a bitmap.

Thomas Harte

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

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

scriptX

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

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)?

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

Still doesn't work (moves 1 pixel)

gnolam

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).

miran
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))));

scriptX
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

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

scriptX

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

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).

scriptX
...
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]

Thread #550273. Printed from Allegro.cc