[Solving] change angle

Okay, I am officially stuck. I Hope somebody here could help me. I want a simple function added to my game, but I don’t know how to do it. I have now spend one and a halve day. I have made progress but can’t find the final puzzle piece to my solution.

What I want is the following:
If my object hits an object with an angle < 20 degrees, I want it to bounce off that object with an angle of 20 degrees. This should thus prevent the ball from moving in a horizontal direction too much.

What I already have:
A function that, on collision, calculates the angle using the inverse tangent (delta Y/ delta X) . This function is tested and works perfectly.
An if statement that says if (angle < 20 degrees) {
print (“edit the angle a little bit” )

What I still need:
A few lines of code that replace the print statement with some code that changes the outbound vector in such a way that the ball doesn’t move as horizontally.

What I tried:
(Too many things to list them all. Including)

There’s probably a better way to do this, but I just checked if the angle between the velocity vector and Vector2.right (or Vector2.left, depending on the ball’s direction) was less than 10 or greater than 80.

If it was, I decreased the larger of the X and Y velocity by a small amount. Then I recalculated the other velocity component in order to keep the same magnitude. This ends up making the angle more “angled” (for example, from 85 degrees to 70 degrees) while keeping the same overall speed, and it seems to work pretty well for me.

Hope this helps! Let me know if any of that didn’t make any sense.

!!! " I just checked if the angle between the velocity vector and Vector2.right"

You’re a genious! I stored the location of where the ball last hit an object, than compared it to a new location when it hit an object. Calculated Delta X and Delta Y, did an inverse tangent on dX/Dy and got the angle. :slight_smile: I have about 30 lines of code. :blush: I will update that.

I hope I have time today or tomorrow to implement and test your solution but it makes lots of sense and shouldn’t be too difficult. I will update when I am done! Thank you once again!

Privacy & Terms