views:

911

answers:

2

Hi All....

I am doing ping pong game where I have to move ball and detect collision with outside boundary and also with moving paddle. I want to implement it using vector and not by trigonometry.

I am very weak in math,so kindly guide me.

+2  A: 

You'll probably need to be more specific about what you need. The key thing to know is that when bouncing the ball, the angle of incidence equals the angle of reflection. However, in pong like games, they usually alter the angle based on the part of the paddle that it hit (as if the paddle were an arc).

For using a vector, it has a start point and a slope. If the start point is the left side of the screen (x == 0), then you are in luck, because the normal slope intercept formula works well

Y = mX + B

Where M is slope and B is the where it crosses the Y-Axis (the value of Y when X is 0). Then increment X some amount and calculate Y. On the return hit, you can do this backwards. m is 0 when the vector is perfectly horizontal. When it hits the paddle, to preserve angle of incidence == angle of reflection -- you just need to use -m (adjusted for the position on the paddle).

Anyway -- if you have any questions, comment on this answer or update your question and I will try to provide more info.

Lou Franco
Hi Lou,Sorry for commenting on your answer too late. But finally I am able to implement 2D vector for moving ball and also it is reflecting from boundary accurately. But now I am facing another problem, as u mention earlier that"In pong like games, they usually alter the angle based on the part of the paddle that it hit (as if the paddle were an arc)" until now I am able to detect the intersection between ball and paddle. But I need your help in calculating angle of reflection which is depends intersect point on paddle. I have moving ball vector angle and it end pt,
Abhijeet
Treat the center of the paddle as 0, and the end of the paddle as some number N. Find a factor between 0 and N depending on the distance from the center. Then take the angle and multiply by (1+factor) -- play around with N and the function that turns the distance into a factor (linear, exponential, etc), until you find something you like.
Lou Franco
+1  A: 

There's a great tutorial on GameDev.net on Collision Detection. It's an OpenGL tutorial, and I don't know if you're using OpenGL, but the math is the same, so I think it'll be useful.

Can Berk Güder
Hi Can, i implemented 2D vector for moving the ball. But i m facing difficulty in finding angle of reflection when ball hit to the paddle. Paddle is arc shape and it's width increases or decreases,i have to write generalized code so plz guide me .
Abhijeet