Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » how to draw sprite from 0,0 to mouse x y?

This thread is locked; no one can reply to it. rss feed Print
how to draw sprite from 0,0 to mouse x y?
rxtype
Member #8,352
February 2007

im trying to make a simple game where the player shoots stuff on the screen by aiming with the mouse.

when the player clicks mouse b 1, you will see the bullet firing off lets say x0y0 to the mouse's position.

i tried to make my bullet move by x++ y++, but that will not make it travel in a straight line.
i tried making a temp variable temp = mouse_x / mouse_y and have x add temp when mouse_x > mouse_y, but that doesnt work well either..

how can i make this work?? i cant think of a solution..

kazzmir
Member #1,786
December 2001
avatar

Wouldn't it just be

double slope = mouse_y / mouse_x;
double x = 0;
double y = 0;

while ( x < mouse_x && y < mouse_y ){
    y += 1;
    x += slope;

}

Or something? I usually just use angles

double angle = atan2( mouse_y, mouse_x );
double x = 0;
double y = 0;

while ( x < mouse_x && y < mouse_y ){
   x += cos( angle );
   y += -sin( angle ); // negative because you want the bullet to go "up" the screen
}

rxtype
Member #8,352
February 2007

i tried using ur angle code, but i get errors..

error C2664: 'atan2' : cannot convert parameter 1 from 'volatile int' to 'fix'
error C2664: 'cos' : cannot convert parameter 1 from 'double' to 'fix'
error C2664: 'sin' : cannot convert parameter 1 from 'double' to 'fix'
Constructor for class 'fix' is declared 'explicit'

kazzmir
Member #1,786
December 2001
avatar

Include math.h before allegro.h.

rxtype
Member #8,352
February 2007

thank you!!!!

Go to: