Recently I got a problem. For example, the design resolution in our app is 640x960, and I used setRotation3D(Vec3(-20, 0, 0)) to rotate the layer, and tried to get the touches point coordinate in the rotated layer.
Void HelloworldScene::onTouchesBegan(touches, event)
{
Point p = touches.at(0)->getLocation(); //Got (90, 780)
P = this->convertToNodeSpace(touches.at(0)->getLocation()); //Got (90, 733)
P = this->convertTouchToNodeSpace(touches.at(0)); //Got (90, 733)
}
None of these is what I expectted!! I touched on the (90, 780) on the screen, it’s supposed to be converted to (0, 960) on the layer. Please check the screenshot.
Yes. I rotated the layer as the “ground” of my 2.5D game, and touched on the screen. I need to know what the converted coordinates are on the roated layer. I think this is called "screen coordinate & world coordinates conversion in 3D games.
If I dont rotate the layer, all the coordinates are correct.
If you have used setRotation3D(), pick concept in 3d is needed for touch handling. Unfortunately, we have not implement this. We will add it on version 3.3 or version 3.4.
However, you can do it manually now.
First, you need a ray in world space. For orthogonal projection in 3d, the start point of the ray could be (x,y, 0), the direction is (0,0,-1).
Second, convert the ray to node space.
ray start = worldToNodeMatrix * (x, y, 0,1)
ray end = worldToNodeMatrix * ((x,y,0,1) + (0,0,-1,0))
ray direct = ray end - ray start
Third, test the intersect point of the ray and the sprite.
@mobileharry
In your post, you want to use ray plane intersection to test it.
Here a plane could be represent using a formula P dot N + d = 0.
Here N means the normal of the plane. P dot N equals to (P - O) dot N where O is the zero point.
so d equal -(P - O) dot N.V` is the direction of the ray.