# What is wrong with my Dot Product?

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 = {
make:function(object){
return [object.x + object.width/2,object.y + object.height/2]
},
normalize:function(v){
var length = Math.sqrt(v[0] * v[0] + v[1] * v[1])
v[0] = v[0]/length
v[1] = v[1]/length
return v
},
dot:function(v1,v2){
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

• 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 Commented Jun 30, 2012 at 21:17
• I suggest you review this Douglas Crockford talk, slides here for more on JavaScript Commented Jun 30, 2012 at 21:23
• 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.
– j-a
Commented Jun 30, 2012 at 23:14

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.