#### Archived

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

# Collision detection in pong!?

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

## Recommended Posts

How should i do this? Im kinda n00b still.. so, keep it stupid, simple. How should i make the collision between ball - pong thing, and ball - wall ?!

##### Share on other sites
What do you mean "make the collision"? When the ball hits a paddle or wall, just reflect its velocity vector. What are you asking exactly?

##### Share on other sites
What i mean is..
how do i make the code..

hmm.. how should i make ball move..

##### Share on other sites
Thats all there is too pong, the colision detection.

##### Share on other sites
This would probably be better off in the Beginner''s forum

Nevertheless, let me give you a few pointers. The the ball move, you need to give it a velocity. In 2D this would have an x and a y-component. In every cycle of your gameloop, you add this velocity to the current x,y-position in space of the ball.

You also test each loop if the ball has hit a wall (by comparing their coordinates). If it has, you change the velocity. Don''t know the exact formula, I hate physics :D

##### Share on other sites
A normal is a vector with length of 1, for a surface it represents a stick in the surface with an angel of 90° between stick and surface, from all directions.
You need the normal of the surface wich the ball hit, and the edges positions you subtract the position of one edge from the position of the ball, so you got a vector pointing from the edge to the ball, let''s call it ''a''.
To get the distance between the ball and the surface you multiply ''a'' with the normal, the scalar multiplication way.
So if ''n'' is the normal you get
distance = abs(n.x*a.x + n.y*a.y)
if you have z cords
distance = abs(n.x*a.x + n.y*a.y + n.z*a.z)
you take the abs s that you can bounce on a wall from bove sides and to go around some problems.
if the distance is below the radius of the ball you need to reflect the velocity, now called ''v''.
you get the velocity directly toward the surface the same way you got the distance
v_reflect = abs(n.x*v.x + n.y*v.y)
If you would subtract this velocity of the velocity towards the surface the ball would move along the surface and it would just look as if it is just unable to bounce. So you got to take the doueble of it so that the ''v'' towards the surface is actually turned 180°, or reflected.
v_reflect = 2*abs(n.x*v.x + n.y*v.y)
To ensure that the ball will not be in the wall you can
move it away from the surface as much as it is in it.
n*(distance-r) will give you a vector representing how much you have to move the ball out of the wall yo just add it to the position of the ball.
And for the edges you do the same thing only that you get the normal by normalizing the vector ''a'', you know the vector from the edge to the ball, you have to do this, for all edges, if the possibility exists that the ball can hit an edge without hiting a surface.

##### Share on other sites
What zerotacg is trying to say is: go write a text adventure
Seriously, though, you need to work on some kind of basic movement system before you start messin round with collision detection. After that, if the ball hits a horizontal wall, reverse its Y velocity (yVel *= -1), and if it hits a vertical wall, reverse its X velocity (xVel *= -1). Worked for me....

##### Share on other sites
yeah that was what I wanted to say,
I only did not know how to say it.

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
5. 5
A4L
11

• 9
• 12
• 16
• 26
• 10
• ### Forum Statistics

• Total Topics
633768
• Total Posts
3013753
×