Interact2D issue after camera transformation

Started by kkl, December 29, 2012, 08:30:23 AM

Previous topic - Next topic

kkl

Hi I'm working on object following finger touch by Interact2D in android. The reproject2D3D gave unexpected value after I translated the camera. I know the reproject2D3D doesn't take account of camera transformation. Is there any way to get correct location after camera transformation?

Here's the code

Happens when finger touching:

public void onTouch(MotionEvent e) {
Camera cam = world.getCamera();
SimpleVector rayTemp = Interact2D.reproject2D3D(cam, frame, (int)e.getX(), (int)e.getY(), 50f);
rayTemp.z = 0;
plane.setOrigin(rayTemp);
}


Move the camera along X axis:

Camera cam = renderer.getWorld().getCamera();
cam.moveCamera(cam.getXAxis(), 1);


Creating objects and world:

frame = new FrameBuffer(gl, width, height);
world = new World();
world.setAmbientLight(10, 10, 10);

sun = new Light(world);
sun.setIntensity(255, 255, 255);

Texture texture = new Texture(BitmapHelper.convert(context.getResources().getDrawable(R.drawable.test3)), true);
TextureManager.getInstance().addTexture("texture", texture);
texture.setClamping(true);

plane = Primitives.getPlane(20, 2);
//cube.calcTextureWrapSpherical();
plane.setTexture("texture");
plane.setTransparency(0);
plane.build();

world.addObject(plane);

Camera cam = world.getCamera();
cam.moveCamera(Camera.CAMERA_MOVEOUT, 50);
cam.lookAt(plane.getTransformedCenter());

EgonOlsen

Just use the method with the WS-suffix to get coordinates in world space instead.

kkl

#2
I tried the reproject2D3DWS, it gives the same values as reproject2D3D does. Since we get a vector from reproject2D3DWS, do we have to calculate the coordinates along that vector by a given Z-value by ourself instead of treating the vector as actual coordinates?

I also tried this way and it got the correct values, but I don't think it's the proper way to do it.

public void onTouch(MotionEvent e) {
Camera cam = world.getCamera();
SimpleVector rayTemp = Interact2D.reproject2D3D(cam, frame, (int)e.getX(), (int)e.getY(), 50f);
SimpleVector camPos = cam.getPosition();
rayTemp.x += camPos.x;
rayTemp.y += camPos.y;
rayTemp.z = 0;
plane.setOrigin(rayTemp);
}

EgonOlsen

...WS can't give you the same values if you have transformed the camera at least once...it takes the camera into account while the other method doesn't.

BTW: Modifying an object directly in onTouch is asking for trouble, because it will interfere with the rendering thread and that can cause all kinds of strange behaviour.

kkl

hi thanks for reply.

I tried again with reproject2D3DWS.

The results are as followed:

No camera movement yet
touch = (45, 340), vector result = (-0.5078125, -0.057291668, 0.0)

After camera.moveCamera(camera.getXAxis(), -0.3f)
touch = (45, 340), vector result = (-0.5078125, -0.057291668, 0.0)

I got both the same values at 2 cases. It should be different since the camera has moved. Did I miss anything yet?

Here's the new code:

frame = new FrameBuffer(gl, width, height);
world = new World();
world.setAmbientLight(10, 10, 10);

sun = new Light(world);
sun.setIntensity(255, 255, 255);

Texture texture = new Texture(BitmapHelper.convert(context.getResources().getDrawable(R.drawable.test3)), true);
TextureManager.getInstance().addTexture("texture", texture);
texture.setClamping(true);

plane = Primitives.getPlane(1, 0.08f);
plane.calcTextureWrapSpherical();
plane.setTexture("texture");
plane.setTransparency(0);
plane.build();

world.addObject(plane);

Camera cam = world.getCamera();
cam.moveCamera(Camera.CAMERA_MOVEOUT, 1f);
cam.lookAt(plane.getTransformedCenter());



public void onTouch(MotionEvent e) {
Camera cam = world.getCamera();
SimpleVector rayTemp = Interact2D.reproject2D3DWS(cam, frame, (int)e.getX(), (int)e.getY());
rayTemp.z = plane.getOrigin().z;
Log.v("kkl", (int)e.getX() + ", " + (int)e.getY() + ": " + rayTemp.toString());
plane.setOrigin(rayTemp);
}


Btw, I'm using RENDERMODE_WHEN_DIRTY for rendering mode in GLSurfaceView. It only renders after onTouch() function. I reckon it's quite safe to do so since rendering is called after modifying objects.

EgonOlsen

That's expected behaviour if all your transformation does is a translation, because (as the docs state), what this method returns is:

Quotethe direction vector from the camera to the position in world space

The DIRECTION vector is independent from the camera's position. So if you need the actual POSITION in world space, you have to add the camera's position to that vector. Sorry for the lack of clarity, but we where talking about slightly different things here... ;)

kkl

Haha... Yaya... We were talking in different context... In the first reply you were saying about returning a coordinate, I took it as actual position in world space. Now I got what you mean. Thank alot for the help ;)