Recommended Posts

Im currently using c++ with the library language allegro.
the problem is i cant get my sprit to move from the source to the target x c an any on hlp me resolve this problem.I already a discorouaged soul

Share this post

Link to post

Share on other sites

I assume you have the current position of your sprite stored somehow, and you can draw the sprite at any given position.

Now, for moving the sprite towards a target position:

- Subtract the current position from the target position, this will give you a vector pointing from the current position to the target

- Normalize the vector, which is done by dividing it's elements (x and y coordinate in 2D) by it's size (given by sqrt(x^2 + y^2)), this will be your direction vector (it will point in the direction your bullet has to travel and have a size of 1)

- In each update cycle, add v*direction_vector to your sprite's current position, where v is a constant defining the speed of the bullet

Hope this helps

P.S: there's an alternate, but very similar way to do this with interpolation, but for a bullet I think this method is more suitable