-1

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.

Kevin Panko
  • 7,844
  • 19
  • 46
  • 58
dHoja
  • 206
  • 2
  • 10
  • How much of an angle do you want or do you want it to be depended on the speed of the ball – washcloth Feb 13 '14 at 15:19
  • 1
    For a proper programming question you should show what code you've written so far and what part exactly is your problem. – t0mppa Feb 13 '14 at 15:21
  • @ExcelledProducts Yes, i want it to be depended on the speed. – dHoja Feb 13 '14 at 15:26
  • @t0mppa I have all the graphics figured out, i know how to move the ball, but i don't even know where to start with angles so my code count involving angles is zero. – dHoja Feb 13 '14 at 15:26
  • 1
    `i don't even know where to start` Generally StackOverflow isn't the best place to start, *but* I hope you find the information you're looking for. I recommend you take a look at this - http://stackoverflow.com/help/how-to-ask - Good luck! – admdrew Feb 13 '14 at 15:31
  • for starter you can implement simple reflection, for vertical walls change ball vector of movement fro (x,y) to (-x,y) and for horizontal (x,y) -> (x,-y) – user902383 Feb 13 '14 at 15:41
  • I am writing an answer that will cover your question quite clearly – washcloth Feb 13 '14 at 15:42
  • You should provide an [MVCE](http://stackoverflow.com/help/mcve) which will show us that you have working code already, and it will help us to understand what pieces you are missing. – Kevin Panko Feb 13 '14 at 16:06

2 Answers2

2

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 :)

Kevin Panko
  • 7,844
  • 19
  • 46
  • 58
washcloth
  • 2,322
  • 13
  • 27
0

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.

Elliott
  • 147
  • 2
  • 11