Hi, i create games with SDL2 but now it doesn't matter.
In my game there is the player and the enemy.
Enemy can rotate itself watching to the player and can shot some balls that can kill the player.
So balls should move from a point to another o from the enemy to the player.
Ok. Then i don't know how to calculate that direction. Someone told me to use trigonometry but i don't know it.
I tried calculating the difference x and y of two points and i tried with calculating the "hypotenuse" but i don't think it will be useful.
Please can you explain me how to do it?
P.S. The velocity of the ball is the sameand never change during animation
Hey Manuel, your friend is not wrong, you can get the function of the line to run at a constant speed if you know how to deal with polar equations and know how to get your x and y positions out of it again by knowing the angle and such, but in reality vectors are the easier method to understand. Vectors are actually how we deal with the problem that velocity has both speed and a direction over a period of time.
Knowing how to deal with vectors (or parametric equations) will also allow you to easily deal with other real world issues like wind shear and gravity, and will very quickly lead into equations for 3d games whereas your friend's option is kind of a one-trick-pony if I'm interpreting it correctly.
I'm not sure about the vids that Marcus mentions, but if they are teaching how to use vectors then that's a good place to start.
I thought I should just come back and mention that the C++ class <vector> has nothing really in common with the mathematical meaning for vectors. When I first started coding I hadn't taken enough math classes and the mix-up in terms really had me confused why people kept talking about how vectors can help in figuring out this sort of thing if they were just kind of glorified strings. They aren't.
In case you don't have a strong math background, a mathematical vector gives you the option of setting up functions for x and functions for y, and even (in 3d space) functions for z, all which can change based off of an independent variable; often t which stands for time.
V = <x, y>
V(t) = <t, t^2> // both x and y are related to t
R(t) = <3, t> // x is a constant at 3 , so this goes only up and down at x=3 as t changes
P(t) = <3t, 0, 4t> // a three dimensional vector in which y is a constant at 0, but x and z respond to changes in t
Using vectors means that your x and y values form a continuous line as t changes, but y does not have to depend on the value of x anymore.
You can even do some fun stuff with the line (path of the projectile) like this;
V(t) = <2t+sin(11t), t+cos(11t)> // which will cause the projectile to do little loops as it heads to the target