Question

I am trying to learn game programming and about angles.

I'm trying to make a game in which you kick a ball, and when it hits a wall it should bounce away from the wall at a certain angle, depending on the speed of the ball when it hits.

After trying to do it for several days now, I don't even really know where to start. I have looked at a lot of other questions, but when I read the code I don't even know what it's supposed to mean. I have always been good in math, but I don't know how to implement it into game programming. I would really appreciate it if you would give me slight push in the right direction.

Here is little picture representing what i'm trying to do.

Was it helpful?

Solution

There is really one main factor that contributes to this.

  1. Your entrance angle

Because you are just a beginner computing the angle with speed takes complex math and will not make your return angle much different. (unless you're traveling at like 500 MPH)

Your entrance angle is found by your offset from the center. In this example I will show you two scenarios. And the general math that you need to implement in your game.

First Scenario: A line

Let's go back to geometry. A line adds up to 180 degrees.

Because it is straight, no matter what your speed is, the angle you enter equals the angle you exit. If your ball enters at 0 then your ball will exit at 0.

As you see in the picture the ball will return and exit at the same angle.

So the math?

if enter angle is 0 then exit angle is 0

What if it doesn't enter in a straight line?

Because your return surface is a straight line, your entrance angle is again going to equal your exit angle.

enter image description here

But what if your return surface is not a straight line?

Well, the angle of your return surface is needed.

By adding the angle of your return surface to your entrance angle you will get your exit angle.

enter image description here

So there are our basic calculations of entrance and exit angles WITHOUT speed. (Again, unless you're traveling at 500MPG your speed will not make a huge difference.)

Based on your picture you will need to have separate line data to calculate your return.

enter image description here

I really hope I gave you a general idea on calculating your return angle. And I hope the pictures gave you a visualization to help you understand more. Good Luck :)

OTHER TIPS

you will have your ball move with a given vector. If you are able to attach colliders or triggers to your objects in the game, you will want to check for a collision with the ball and a wall. If so, say you've collided with the left wall, you can simply multiply the ball's x direction by -1 resulting in a bounce off the wall. This can be done with all walls. If you bounce off a top wall, flip the y.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top