Frage

perspectiveCamera = new PerspectiveCamera(90, 80, 48);
perspectiveCamera.position.set(0,0, 10f);
perspectiveCamera.lookAt(0,0,0);
perspectiveCamera.near = .01f;
perspectiveCamera.far = 300f;

My ScreenWidth x ScreenHeight are 800 x 480;

pCamera.unproject(mytouchPoint) shall suppose to give results between

x = 0 to 80 y = 0 to 48

but I m getting 0.000xyz for both x and y axis

War es hilfreich?

Lösung

Don't use such a small value for your camera's near member, it will cause floating point errors and/or z-fighting.

The width and height values you provided to PerspectiveCamera constructor, are used to calculate the aspect ratio. There is no single 2D resolution (the size of the screen-plane in world coordinates) in a 3D perspective.

You cannot simply unproject a 2D screen coordinate to a single 3D coordinate. For each 2D screen coordinate, there are an "infinite" amount of 3D coordinates possible. Therefor the unproject method of the camera will use the z-coordinate of the provided screen coordinate to decide which of those 3D coordinates to return. If z is zero, it will give the coordinate on the near-plane. If z is one, it will give the coordinate on the far-plane.

Assuming you used z=0 for myTouchPoint and given you have a very small near-plane (since you near value is very small), the unprojected value will be vary small and therefor (almost) equal to zero.

For more information, you might want to have a look at: http://blog.xoppa.com/interacting-with-3d-objects/

Andere Tipps

I found a way to easily do it. Its fast too.

Just create a plane at required depth z and find intersection of ray on it.

float zDepth=-10;//your decision or and object z position

public boolean touchDown(int screenX, int screenY, int pointer, int button) {

    Ray ray = camera.getPickRay(screenX,screenY);

    Plane plane=new Plane();
    plane.set(0,0,1,0);// the xy plane with direction z facing screen

    plane.d=zDepth;//***** the depth in 3d for the coordinates

    Vector3 yourVector3Position=new Vector3();
    Intersector.intersectRayPlane(ray, plane, yourVector3Position);

}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top