@CodeHelp do someone here knows how to make an object point to its horizontal and vertical velocity? Both at the same time
Point to the velocity as a position, or point to the velocities when added to its position?
But how do I do that?
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.
Good explanation @Petrichor
@anon92895349, 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)
If X +Vh < X then
Angle = arctan(Vh/Vv) - 180
If Vv > 0 then
Angle = 90
Angle = 270
(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
[Edit] 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
Would you like this topic closed as the issue has been resolved?
Already resolved lol
I realized me myself could code this but I overcomplicated stuff… lol you can close this!
Just add SOLVED to the title and then I’ll close n archive
There! Ready to close