Okay, I struggled a bit choosing wheter to put this on StackOverflow or here, Math. I concluded that it was actual math my problem and not "exactly" programming.
You see, I am making a 2d game, top-view where your character moves around and you can use your mouse as aim to shoot.
My problem is that I can't figure out the correct formula to get the "amount of pixels to move per frame". Let me explain:
The projectile being shot will not "stop" at the point your mouse clicked. Instead it will continuously move. Every frame, my projectile is supposed to "change X-position by {pixels}" and "change Y-position by {pixels}". I don't know how to figure out the {pixels} amount I need for both X and Y movements.
Here is some code. It is quite self-explanatory:
class PortalBall def initialize(sx,sy,dx,dy) # sx,sy source points. dx,dy destination points @speed = 60 x_per_frame = dx / @speed y_per_frame = dy / @speed end def update @sprite.x += x_per_frame @sprite.y += y_per_frame end end
As you can see, my formula to figure out the amount of x pixels to move per frame is "destination X divided by speed". Speed is always 60 actually...
But this clearly isn't working properly. Can someone help me fixing my formula?
NOTE In this language, the higher on the screen means less Y position and the lower screen is more Y position. The top of the screen is Y 0, and the bottom is Y 600.