Touch to 3d point on plane

Started by zammbi, May 11, 2012, 12:49:18 AM

Previous topic - Next topic

zammbi

I'm porting some Objective C code over to android and a little stuck on this piece of code:

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?

EgonOlsen

Interact2D has some methods to project/unproject....i'm not 100% sure if they do exactly what you need here though.


zammbi

#3
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:

   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



zammbi

Ah got it:

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);
}