Author Topic: Interact2D issue after camera transformation  (Read 1454 times)

Offline kkl

  • float
  • ****
  • Posts: 291
    • View Profile
Interact2D issue after camera transformation
« on: December 29, 2012, 08:30:23 am »
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:
Code: [Select]
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:
Code: [Select]
Camera cam = renderer.getWorld().getCamera();
cam.moveCamera(cam.getXAxis(), 1);

Creating objects and world:
Code: [Select]
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());
« Last Edit: December 29, 2012, 08:41:17 am by kkl »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11748
    • View Profile
    • http://www.jpct.net
Re: Interact2D issue after camera transformation
« Reply #1 on: December 29, 2012, 09:56:58 am »
Just use the method with the WS-suffix to get coordinates in world space instead.

Offline kkl

  • float
  • ****
  • Posts: 291
    • View Profile
Re: Interact2D issue after camera transformation
« Reply #2 on: December 29, 2012, 03:32:03 pm »
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.
Code: [Select]
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);
}
« Last Edit: December 29, 2012, 04:04:40 pm by kkl »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11748
    • View Profile
    • http://www.jpct.net
Re: Interact2D issue after camera transformation
« Reply #3 on: December 29, 2012, 05:09:49 pm »
...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.

Offline kkl

  • float
  • ****
  • Posts: 291
    • View Profile
Re: Interact2D issue after camera transformation
« Reply #4 on: January 01, 2013, 09:22:04 am »
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:
Code: [Select]
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());

Code: [Select]
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.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11748
    • View Profile
    • http://www.jpct.net
Re: Interact2D issue after camera transformation
« Reply #5 on: January 01, 2013, 11:08:04 am »
That's expected behaviour if all your transformation does is a translation, because (as the docs state), what this method returns is:

Quote
the 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... ;)

Offline kkl

  • float
  • ****
  • Posts: 291
    • View Profile
Re: Interact2D issue after camera transformation
« Reply #6 on: January 01, 2013, 11:16:31 am »
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 ;)