Jump to content
  • Advertisement
Sign in to follow this  
corner

touble firing a bullet

This topic is 4236 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
Hi!

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!