Friday, November 23, 2018

javascript - Cocos2d JS - Getting vector reflection



I'm working on a small Breakout clone using Cocos2D-JS, without the use of a physics engine.


One of the things that baffles me was how the ball bounces. My friend came up with this:



inputVector - 2(normalVector * inputVector)



I have no idea how to translate that in code. So I tried with this:


g_Ball.position = this.velocityComputer(cc.p(g_Ball.x, g_Ball.y), cc.p(this.x, this.y));

velocityComputer: function(inVector, thisNVector) {
var prodVec = cc.pDot(inVector, thisNVector);

var retVec = inVector - (2 * (prodVec * inVector));
cc.log(retVec);
// cc.log(blockNVec);
return retVec;
},

But g_Ball doesn't change directions, and retVec simply logs NaN.


Any suggestions?


Thanks.




No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...