Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Point A to Point B

This thread is locked; no one can reply to it. rss feed Print
Point A to Point B
Cerpin Taxt
Member #8,304
February 2007
avatar

Just trying to get a dot moving smoothly from predecided point A to moving, player-controlled point B. Because of the way I'm doing it, the dots stop just short of point B. I'd just like to know about any better ways of doing this.

                for( iRe2 = 0 ; iRe2 < n; ++iRe2)
                {
                    iLX = (mouse_x-iEnemyX[iRe2])*0.1;
                    iLY = (mouse_y-iEnemyY[iRe2])*0.1;
                    iEnemyX[iRe2] += iLX;
                    iEnemyY[iRe2] += iLY;

                    circlefill( buffer, iEnemyX[iRe2], iEnemyY[iRe2], 2, makecol( 255, 0, 0 ));

                    if( iEnemyX[iRe2] == mouse_x && iEnemyY[iRe2] == mouse_y ) return(0);
                }

Relevant code and exe attatched.

I know it's because of the 0.1's at the end of the iLX and iLY lines.

CursedTyrant
Member #7,080
April 2006
avatar

float Angle = atan2f(B.Y-Dot.Y, B.X-Dot.X);

Dot.X += cos(Angle)*Speed;
Dot.Y += sin(Angle)*Speed;

I'm not sure if that's what you want tough...

---------
Signature.
----
[My Website] | [My YouTube Channel]

Cerpin Taxt
Member #8,304
February 2007
avatar

This seems to give me the same result as when I had the super-simple movement. Enemies moving in either straight lines or fourty-five degree angles.

I'm looking for lines directly toward the target, but the way I was doing it earlier kept the enemies from reaching the target.

Any more help would be fantastic.

Kikaru
Member #7,616
August 2006
avatar

Get x/y distances, use Pythagorean Theorem to find total distance, divide the total distance by the speed, divide x and y distances total distance, you got x and y speeds. Only slow part is the sqrt() in the total distance. :)

Cerpin Taxt
Member #8,304
February 2007
avatar

Okay, I'm sure I'm doing that wrong, but it's not going well. It seems way more complicated than what I was doing. Is there any way to just fix what I did before, so that it doesn't fail at the end?

Kikaru
Member #7,616
August 2006
avatar

Some example code:

point get_speeds(point A, point B, int speed)
{
    int xd = abs(a.x-b.x);
    int yd = abs(a.y-b.y);
    int td = int(sqrt((xd*xd)+(yd*yd)));
    td /= speed;
    yd /= td;
    xd /= td;
    point speeds(xd, yd);
    return speeds;
}

This assumes you declared a struct/class called point, with member x and y. It also assumes you set up a proper constructor for that class.

(P.S. sorry of it's really messy)

Paul whoknows
Member #5,081
September 2004
avatar

Your game ends abruptly when I intentionally "touch" a red dot.
CursedTyrant's algo is what you need, I am using something like that in my game and it works fine.

____

"The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner.

Cerpin Taxt
Member #8,304
February 2007
avatar

Sorry about that. It's not really much of a game yet, huh?

I must have been doing something wrong with that, but I was able to adapt Kikaru's code to something that sems to work for me.

Thank you very much.

Go to: