Author Topic: Touch to 3d point on plane  (Read 1360 times)

Offline zammbi

  • float
  • ****
  • Posts: 360
    • View Profile
Touch to 3d point on plane
« on: May 11, 2012, 12:49:18 am »
I'm porting some Objective C code over to android and a little stuck on this piece of code:

Code: [Select]
CC3Vector4 intsectionPoint = [self.activeCamera unprojectPoint:CGPointMake(x, y) ontoPlane:CC3PlaneMake(0, 1, 0, 0)];
Which basically you pass the x, y of the touch and the method converts it to a point on the plane in the world. Any method like unprojectPoint?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11747
    • View Profile
    • http://www.jpct.net
Re: Touch to 3d point on plane
« Reply #1 on: May 11, 2012, 08:20:32 am »
Interact2D has some methods to project/unproject....i'm not 100% sure if they do exactly what you need here though.

Offline zammbi

  • float
  • ****
  • Posts: 360
    • View Profile
Re: Touch to 3d point on plane
« Reply #2 on: May 11, 2012, 01:57:57 pm »

Offline zammbi

  • float
  • ****
  • Posts: 360
    • View Profile
Re: Touch to 3d point on plane
« Reply #3 on: May 14, 2012, 05:21:44 am »
Having some trouble. My camera spins around a point. If I don't have the camera near a certain position the points are all wrong.

This is the code I'm using:

   
Code: [Select]
private static int Z_PLANE = 0;

public SimpleVector getTouchVector(int xPos,int yPos){
final SimpleVector dir = Interact2D.reproject2D3DWS(world.getCamera(), frameBuffer, xPos, yPos).normalize();
final SimpleVector pos = world.getCamera().getPosition();

final float a = (Z_PLANE - pos.z) / dir.z;
final float xn = pos.x + a * dir.x;
final float yn = pos.y + a * dir.y;
return SimpleVector.create(xn, yn,(float) Z_PLANE);
}


Correct output(camera is is that certain spot):

Touch-- x:117 y:138 (top left)
Point in world space-- x:-26.990156 y:-24.963928 z:0.0

Wrong output (camera is spinned around 90 degrees):
Touch-- x:113 y:126 (top left)
point-- x:-35.060413 y:-65.494 z:0.0


« Last Edit: May 14, 2012, 05:23:26 am by zammbi »

Offline zammbi

  • float
  • ****
  • Posts: 360
    • View Profile
Re: Touch to 3d point on plane
« Reply #4 on: May 14, 2012, 06:47:43 am »
Ah got it:

Code: [Select]
public SimpleVector getTouchVector(int xPos,int yPos){
final SimpleVector dir = Interact2D.reproject2D3DWS(world.getCamera(), frameBuffer, xPos, yPos).normalize();
final SimpleVector pos = world.getCamera().getPosition();

final float a = (Z_PLANE - pos.y) / dir.y;
final float xn = pos.x + a * dir.x;
final float yn = pos.z + a * dir.z;

return SimpleVector.create(xn,(float) Z_PLANE, yn);
}