Please! Calculate direction of a ball shooted

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
Here you go -> https://www.youtube.com/watch?v=sKCF8A3XGxQ&list=PLW3Zl3wyJwWOpdhYedlD-yCB7WQoHf-My&index=1

You'll get your answer in the first 3-5 videos.
Not what i'm searching for.
Th videos tell about vectors but not about velocity of x and y.
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.
Last edited on
Vectors are indeed the answer.

Lets say the player sits at (px,py). The enemy is at (ex,ey).
Those are points/positions, but also vectors from (0,0).

vector P = (px-0, py-0) = (px, py)
vector E = (ex-0, ey-0) = (ex, ey)

Let vector EP = P - E = (px-ex, py-ey)

What is E + EP?
(ex+px-ex, ey+py-ey) = (px, py) = P

The EP contains both direction and distance.


What is the distance between E and P?
Dep = sqrt( (px-ex)² + (py-ey)² )

Vector EP / Dep has length == 1. (What if E == P?)

How long distance do we want the ball to move in one timestep?
Dt

Where is the ball after T timesteps?
B = E + T * Dt / Dep * EP


You want the enemy to rotate. That means that the enemy has a "facing direction" F. That should be a length=1 vector too.

One can compute the (cosine of) angle between F and EP/Dep. (That is where trigonometry can be kind of used.) One can rotate F towards EP/Dep.
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
Last edited on
Solved with proportions:
a = enemy.x-player.x;
b = enemy.y-player.y;
v = sqrt((a*a)+(b*b));
speed=6;
velocity.x=a*speed/d;
velocity.y=b*speed/d;

It works
Topic archived. No new replies allowed.