# How to point to a velocity (SOLVED)

#1

@CodeHelp do someone here knows how to make an object point to its horizontal and vertical velocity? Both at the same time

5 Likes
#2

Point to the velocity as a position, or point to the velocities when added to its position?

2 Likes
#3

But how do I do that?

1 Like
#4

I will make a thing for pointing to the velocities added to the current position, just donâ€™t add the current position if you want to point to the velocities as a point.
While I was making the screenshots for this I realized that I was making this over complicated.

While I was making the simpler explanation, I realized that I was still making it worse than it needed to be.

To point to any point (with a greater x value than the object), you can use arctan(distance y/distance x). Usually you would find the differences in y and x by using absolute value, but here you already know the differences. Since the y values of the two points are current y position and current y position + vertical velocity, the distance between them is the absolute value of y-y+vertical velocity. The y-y is equal to zero, meaning that the distance is vertical velocity. So what you need is arctan of vertical velocity divided by horizontal velocity.

(Google inverse tangent for an explanation of it)
But thatâ€™s only allows it to point somewhere if horizontal velocity is greater than zero.
Otherwise it points in the exact opposite direction.
To fix this, put the set angle block in a check if else and check if horizontal velocity is greater than 0. (I did mine differently, but it does the same thing.)
In the else, copy and paste the set angle block but add a -180 to it.

5 Likes
#5

Good explanation @Petrichor

@Rodrik834, the only time that method fails is when the horizontal velocity is 0 (the object is moving either straight up or down) because the Tangent function goes to +/- infinity at 90 & 270 deg. Depending on the project, that might never happen so it can be ignored. Otherwise, you have to add an additional check

If X + Vh > X then
Angle = arctan(Vh/Vv)
Else
If X +Vh < X then
Angle = arctan(Vh/Vv) - 180
Else
If Vv > 0 then
Angle = 90
Else
Angle = 270
End if
End if
End if

(X/Y are positions, Vh/Vv are velocities)

Alternatively, we can use Sine or Cosine instead of Tangent. The formula is a little longer because you have to calculate the hypotenuse, but it gets rid of the need to check for straight up or down. Then we can also add a little trick onto the end so we donâ€™t have to check and subtract 180 either.

Angle = arccos[ Vh / Sqrt(Vx^2 + Vy^2) ] * [ Vv/abs(Vv) + .5) / abs(Vv/abs(Vv) + .5)]

One equation that always works

 I fixed the equation just above. The â€śtrickâ€ť at end is more complicated than I originally had it. Previously it failed if the angle was 270.

* [ Vv/abs(Vv) + .5) / abs(Vv/abs(Vv) + .5)]

that part of the equation multiplies by
1 for any positive value of Vv
1 if Vv is 0
-1 for any negative value of Vv

â€¦whether this is better than just using an IF conditional to add 180 if Vv is less than 0 is probably questionable, but up to the individual user

5 Likes
#6

Done :))

3 Likes
#7

Ohh thanks! @petrichor @thinbuffalo & @fearlessphoenix

1 Like
#8

No problem

Would you like this topic closed as the issue has been resolved?

1 Like
#9

1 Like
#10

I realized me myself could code this but I overcomplicated stuffâ€¦ lol you can close this!

1 Like
#11

Lol yeah.
Just add SOLVED to the title and then Iâ€™ll close n archive

1 Like
#12