Jump to content
  • Advertisement
Sign in to follow this  
ScottNCSU

2D (vector?) question

This topic is 4865 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

What should happen: (Using OpenGL, Dev-C++) Player character is at location px, py on a 2D plane. Mouse cursor is at location mx, my on that same plane. The player clicks the mouse, and the player character shoots a fireball from px, py to mx, my at some universal velocity. How I'm currently handling it: Start with an initial constant x and y velocity for the fireball which is currently either negative or positive 50. Add the difference from (px, py) - (mx, my). Using this method, the fireball correctly flies towards where the mouse cursor clicked. The problem: The speed of the fireball varies depending on how far mx, my is from px, py. I need the fireball to shoot at a universal speed. The solution: Someone on this messageboard tells me the correct equation/method to calculate the x/y velocity for the fireball so that it flies at a constant speed of my choosing. Har har. Honestly I'd spend an hour Googling this but I wouldn't know what to Google. I also haven't been able to find any programs that use this system AND offer source code.

Share this post


Link to post
Share on other sites
Advertisement
From what I've read about your problem I think what you need to do is this:

1. Calculate the vector difference between the two points: vd = (px, py) - (mx, my). You already do that [smile].

2. Turn this vector into a unit vector by diving by its distance: vd = vd / |vd|. Make sure you check for divide by zero.

3. Multiply the unit vector by the velocity of the fireball to get your speed: vf = k vd, where k = constant velocity of the fireball (50).

Hopefully that will do what you want it to.

Share this post


Link to post
Share on other sites
Quote:
Original post by ScottNCSU
Har har.

Honestly I'd spend an hour Googling this but I wouldn't know what to Google. I also haven't been able to find any programs that use this system AND offer source code.


Did you try looking in a maths book? Vector algebra to be precise. Then you can learn to generalise the issue to other cases.

Alternatively Trapper Zoid offers a solution.

Share this post


Link to post
Share on other sites
If you only want collision approximation you can do that with a quick bounding box class (structure) and 1 function :-)

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!