Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question
4  
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 –  bobobobo Jun 30 '12 at 21:17
    
I suggest you review this Douglas Crockford talk, slides here for more on JavaScript –  bobobobo Jun 30 '12 at 21:23
    
I know how to write good javascript I just was too lazy to do so. –  Clay Ellis Murray Jun 30 '12 at 22:42
1  
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 Jun 30 '12 at 23:14
add comment

1 Answer

up vote 1 down vote accepted

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?

share|improve this answer
    
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. –  Clay Ellis Murray Jun 30 '12 at 23:54
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.