I am trying to make a pong game but I wanted to use dot products to do the collisions with the paddles, however whenever I make a dot product objects it never changes much from .9 this is my code to make vectors

vector = {
        return [object.x + object.width/2,object.y + object.height/2]
        var length = Math.sqrt(v[0] * v[0] + v[1] * v[1])
        v[0] = v[0]/length
        v[1] = v[1]/length
        return v
        return v1[0] * v2[0] + v1[1] * v2[1]

and this is where I am calculating the dot in my code

vector1 = vector.normalize(vector.make(ball))
vector2 = vector.normalize(vector.make(object))
dot = vector.dot(vector1,vector2)

Here is a JsFiddle of my code currently the paddles don't move.

Any help would be greatly appreciated

  • 4
    \$\begingroup\$ This isn't the problem, but you should add semicolons to the end of each statement (eg v[0] = v[0]/length should be v[0] = v[0]/length;). Leaving semicolons off isn't good style in JavaScript because you could inadvertently introduce bugs that way \$\endgroup\$
    – bobobobo
    Commented Jun 30, 2012 at 21:17
  • \$\begingroup\$ I suggest you review this Douglas Crockford talk, slides here for more on JavaScript \$\endgroup\$
    – bobobobo
    Commented Jun 30, 2012 at 21:23
  • 1
    \$\begingroup\$ You are confusing points with vectors. You seem to be using the center position as the vector - but remember, a vector is a direction (with a magnitude). You need to set vector1 to the direction of your ball (I suppose [bouncex, bouncey]?) and vector2 to the surface normal of your paddle. For a simple block paddle behavior, the normal should be [1,0] for the first paddle and a normal of [-1,0] for the second paddle. See wikipedia for "vector" and then "surface normal". Hope this helps. \$\endgroup\$
    – j-a
    Commented Jun 30, 2012 at 23:14

1 Answer 1


Your make() is creating a directional vector from an object's current position and constant physical attribute? object.x + object.width/2 Of course that's always going to not change very much and isn't what you really need.

What you want to do is make the directional vector for the moving ball to use in the dot product from the ball's current position - last position to get a sense of what direction it is travelling.

The directional vector for the paddle will always be the same, based on the facing and nothing to do with current position and constants like width. Example would be a paddle on the left edge, the 2D vector would be [1,0] and on the right edge [-1,0].

Even with that all fixed up all you're going to be able to tell with a dot product is which side of the paddle the ball is on and not whether it hit.

I may be missing something, what purpose did you envision for dot products here?

  • \$\begingroup\$ to tell what side of the paddle the ball is on, I already have collision detection, what I want to know is where it hit on the paddle so that I can make the ball bounce right. I know there are easier ways to tell what side it is on but I wanted to learn how to do dot products. \$\endgroup\$ Commented Jun 30, 2012 at 23:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .