Jump to content
• ### What is your GameDev Story?

• Advertisement

#### Archived

This topic is now archived and is closed to further replies.

# Can someone please give me an answer that I can understand ?

This topic is 5840 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

I've tried to get an answer for this in the math forum, but as I am not educated in these matters, the answers are over my head. I am trying to avoid having to learn complex math to solve my problem because my game is very simple. So please do not reply with some abstract looking equation that I can't possibly understand without formal math training. I am pretty sure that there is an easy solution to this because back when programs were very primitive, they did things in a simple way. So you might say that I'm looking for an "Old School" answer to this. When I begin my first 3D program, that is when I will take the time to learn the way that the pros do it. But for now, I'm looking for something easy to understand. I'm doing the collision response for a simple mini golf game that I am making (2D), and I need some math to give the ball a proper re-direction when it hit's a 45 degree angle. Picture this :
                 ___________________
/
/
/      <---------- pretend it's a 45
/
/
/   ^
|    |
|    |
|    |
|    |
|    |
|    O the ball is here and traveling upward.

I need to know a simple formula for telling the ball that when it hits a 45 degree angle, it will redirect and form a right angle. [edited by - LostBoy on January 20, 2003 1:23:03 PM]

#### Share this post

##### Share on other sites
Advertisement
Dunno if you can get any really simple maths for that.

#### Share this post

##### Share on other sites
Hopefully this is what you're looking for...

Now, if your ball is in motion I'm assuming you have some sort of dx/dy variables... these variables are keeping track of how much the ball is moving per (frame/second/whatever) in the X and Y directions. A very simple way to simulate the bouncing of the ball off of a wall is to negate dx if the ball hit a wall that is perfectly vertical, and negate dy if the ball hit a wall that is perfectly horizontal. For example, your (pseudo-)code may look like this:

  if (/* collided with a vertical wall */){  // The ball hit a vertical wall, so it is now moving  // in the opposite horizontal direction  dx = -dx;}else if (/* collided with a horizontal wall */){  // The ball hit a vertical wall, so it is now moving  // in the opposite vertical direction  dy = -dy;}

Good luck!

-Glyph

(edit: 'source' instead of 'code')

[edited by - Glyph on January 20, 2003 1:41:00 PM]

#### Share this post

##### Share on other sites
and when it collide with the 45° wall:

before x y : bx, by
after x y : ax, ay

if you had a coordinate system like this:

^y
|
|
|
----->
x

if not, then just use negative for axis which reversed.

so:
/
/
/
/

ax=by;
ay=bx;

if:

\
\
\

ax=-by;
ay=-bx;

i hope this''ll help

#### Share this post

##### Share on other sites
Try messing around with swaping the dx and dy when the ball collides. You''ll have to negate one too, but which one depends on the wall.

Since dx and dy are kind mathy, I''ll inform you that they''re simply the change in x,y that you apply every frame(or second). And negate means: variable = -variable.

#### Share this post

##### Share on other sites
Here is another (more complicated) way to do this. Just thought I''d add my 2 cents worth.

wall is at 45 degrees. (WA = 45)
ball approaches at 115 degrees (BA = 115)
our reflection angle is called RA

Now imagine that your ''wall'' is always 0 degrees. For the sake of the example we can Just subtract the walls original angle from itself, and from the ''balls'' angle.

NWA = WA - WA
NBA = BA - WA

NWA = 0
NBA = 70

RA = 360 - NBA
RA = 290

Now we can go back to our original wall angle of 45 degrees.
RA = RA + WA

RA = 290 + 45
RA = 335

I know this isn''t a code example, but you get the picture.

good luck,
will

#### Share this post

##### Share on other sites
This might help...

Remember that the angle of incidence (the angle between the wall and the ball''s incoming trajectory) equals the angle of reflection (the angle between the wall and the ball''s outgoing trajectory).

So to compute it...

if the wall is at 45 degrees.

and the ball is incoming at 90 degrees.

the angle between them is 22.5 degrees.

the ball will reflect at 90 + 22.5 degrees or 112.5 degrees.

#### Share this post

##### Share on other sites
Thanks very much guys. I really appreciate you keeping things simple. I can understand you guys.
Now you have given me a new idea. Is there a handy way to translate coordinates (ball X,Y) to degrees so that I can work with degrees? I do have ball X,Y that I am working with. I did the actuall collision detection with very simple math. It looks like this :

    //This is our collision and redirection for the top left 45 degree angle//c3X,c3Y is coordinate for bottom left corner of top left 45 degree angle//c3X,c3Y is our refference point for collisionif (bY >=c3Y)    if (c3X-(bX-.02f) > c3Y-(bY+.02f))		{		    // We have a collison					}

Picture it like this :

                 ___________________                /               /              /      <---------- pretend it's a 45                    /            /                      c3  /   ^      x,y |    |          |    |          |    |    ball = bX,bY          |    |             |    |          |    O the ball is here and traveling upward.

BTW, the ".02f" in my if statement is half of the diameter of my ball. I did it that way so that the ball would "bounce" off of it's edge and not the center.

[edited by - LostBoy on January 20, 2003 3:47:14 PM]

#### Share this post

##### Share on other sites
Ahhh... now I get your original question...

In order to get the angle that your ball is traveling at, you''ll need one more piece of information in addition to its current location; either the ball''s previous location, or how far it traveled to get to its current location. Either way, you''ll need the delta (difference) between the current position (bX, bY) and its previous position... we''ll call the result (dX, dY). The angle (in radians, which is what all the C++ trigonometric functions use) at which the ball is traveling is this:

  double angleInRadians = atan2 (dy, dx);

Hope this helps!

-Glyph

PS- You may want to look into taking a couple of online courses @ http://www.gameinstitute.com... namely https://www.gameinstitute.com/gi/courses/coursedescription.asp?courseID=1 and https://www.gameinstitute.com/gi/courses/coursedescription.asp?courseID=8. The first will teach you all the math you''ll need to know in a very straightforward manner, while the second will teach you how to represent things like the walls of a mini-golf course. Good luck!

#### Share this post

##### Share on other sites
i think if u want to make a good simulation you must improve your math knwonledge in this case (and i assure to you that will fun !! ),

else u can just do it by a simple algo like pong's one !

just inverse the angle-step of your ball when collision :

1-
|  /|a/|/|| |-a|   |    o

2-
|     /|    /__a________|   /||  / ||b/-a||/   |

in this second case, it's the same of the first but here you add to your coordinates b+ angle ! that's all....all about cos and sin deal.

i dont know what kind of simulation are you interest ??

+++
VietCoder

Added [code] tags so that the ascii art would show up properly

[edited by - michalson on January 21, 2003 11:39:14 AM]

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement
• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• ### Popular Now

• 13
• 9
• 15
• 14
• 46
• Advertisement
• ### Forum Statistics

• Total Topics
634067
• Total Posts
3015322
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!