Reflection vector?

TestRayCast.zip (643.2 KB)
The examples https://github.com/2youyou2/physics-example4 shows doing a reflection for a normal.

I’m trying to take it one step further by adding a reflection to the sample. The screenshot shows the reflection but its incorrect. I’ve implemented this following a well known math formula but its not right.

This function is added:
rayCastReflect: function (intersectionPoint, inVector, NVector) {

        var prodVec = cc.pDot(inVector, NVector);

        //calculate the reflection vector: r = v - 2(d . n)n

        var retVec = inVector.sub(cc.pMult( cc.pCompMult( prodVec, NVector ), 2 ));

        var finalpos = cc.Vec2();

        //update the vector position 
        finalpos = retVec.add(intersectionPoint);
        
        cc.log('final reflect pos: ' + finalpos);

       this.drawCircle(finalpos);
       this.ctx.moveTo(intersectionPoint.x, intersectionPoint.y);
       this.ctx.lineTo(finalpos.x, finalpos.y);
       this.ctx.stroke();

    },

Please see my sample project. What is it that I m doing wrong ?

Hello, anyone has some advice on what to do about drawing the reflection vector?

Fixed it by changing this line:

//r=d−2(d⋅n)n
    //calculate the reflection vector
    var retVec = inVector.sub(cc.pMult( cc.pMult(NVector, prodVec), 2 ));