Hi, I have the same problem and I can’t figure how to solve it.
I tried @dabingnn suggestion but I can’t figure how to get the normal of the plane and d.
I think that the original normal is (0, 0, 1) but as I rotated the sprite by -35 degrees in X I don’t know how to transform the normal.
Thanks!
The original normal is (0,0,-1)instead of (0,0,1) because we are look to the negative Z.
For that you using rotation only, you can use this formula to translate normal:
Vec4 newNormal = worldToNodeMatrix * Vec4(0,0,-1,0)
@dabingnn thanks for your quickly answer.
I tried with the new normal but is not working. I’m a bit confused with the directions, If we are looking to the negative Z the ray should have a negative z but the normal a positive one.
This is my current code, may I have some other mistake.
Mat4 toNodeMatrix = _boardNode->getWorldToNodeTransform();
Vec4 start = toNodeMatrix * Vec4(point.x, point.y, 0, 1);
Vec4 end = toNodeMatrix * Vec4(point.x, point.y, -1, 1);
Vec4 rayDir = end - start;
rayDir.normalize();
Vec4 normal = toNodeMatrix * Vec4(0, 0, -1, 0);
normal.normalize();
float rayDirDotNorm = rayDir.dot(normal);
float P0DotNorm = start.dot(normal);
float t = 0;
if (rayDirDotNorm != 0) {
t = -P0DotNorm / rayDirDotNorm;
}
Vec4 result = (rayDir * t) + start;
@diegof29
Sorry, it is my mistake, I used to think about the direction of ray, the director is (0,0,-1), the normal is (0,0,1).
Given that you have transform the ray to node space, you do not need to transform normal, because it is (0,0,1) in node space, just use (0,0,1) to compute the intersection point.
I think the other code is correct if ZERO point (0,0,0) is on the plane of the quad.
@dabingnn Still doesn’t work 
Sorry, but I don’t know how to check this part:
Thanks in advance!
@dabingnn I found the problem.
After a lot of research and refreshing algebra I realized that the problem is in the calculation of the ray.
You can’t just use the worldToNodeMatrix to make the ray.
What we have here is a projected point (screen point) and we need to unproject the point to the node space to make the ray.
To unproject the point I’ve implemented the gluUnproject algorithm in cocos2dx.
Here is my code if anyone want to use it:
Point transformPoint(Point point)
{
Vec4 start = unProjectPoint(Vec3(point.x, point.y, 0));
Vec4 end = unProjectPoint(Vec3(point.x, point.y, -1));
Vec4 rayDir = end - start;
rayDir.normalize();
Vec4 normal = Vec4(0, 0, 1, 0);
float rayDirDotNorm = rayDir.dot(normal);
float P0DotNorm = start.dot(normal);
float t = 0;
if (rayDirDotNorm != 0) {
t = -P0DotNorm / rayDirDotNorm;
}
Vec4 result = (rayDir * t) + start;
return Point(result.x, result.y);
}
Vec4 unProjectPoint(Vec3 point)
{
ScreenManager *sm = ScreenManager::getInstance();
Size screenSize = sm->getScreenSize();
Rect viewPort = Rect(0, 0, screenSize.width, screenSize.height);
Director *d = Director::getInstance();
Mat4 projectionMatrix = d->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
Mat4 modelView = _modelViewTransform;
Mat4 finalMatrix = projectionMatrix * _modelViewTransform;
assert(finalMatrix.inverse());
Vec4 in = Vec4(point.x, point.y, point.z, 1);
in.x = (in.x - viewPort.origin.x) / viewPort.size.width;
in.y = (in.y - viewPort.origin.y) / viewPort.size.height;
in.x = in.x * 2 -1;
in.y = in.y * 2 -1;
in.z = in.z * 2 -1;
Vec4 out = finalMatrix * in;
assert(out.w != 0);
out.x /= out.w;
out.y /= out.w;
out.z /= out.w;
out.w /= out.w;
return out;
}
hi @diegof29, what is the definition of _modelViewTransform in " Mat4 modelView = _modelViewTransform;".
thanks!
@leonliu is the model view transform of the Node. If your class inherits from Node you should access to this matrix as _modelViewTransform
assert(finalMatrix.inverse());
this function will change finalMatrix’s value, but can’t run in release mode.