www.jpct.net

jPCT-AE - a 3d engine for Android => Support => Topic started by: zammbi on May 11, 2012, 12:49:18 am

Title: Touch to 3d point on plane
Post by: zammbi 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?
Title: Re: Touch to 3d point on plane
Post by: EgonOlsen 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.
Title: Re: Touch to 3d point on plane
Post by: zammbi on May 11, 2012, 01:57:57 pm
I found this thread helped me:
http://www.jpct.net/forum2/index.php/topic,2418.msg17730.html

Thanks.
Title: Re: Touch to 3d point on plane
Post by: zammbi 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


Title: Re: Touch to 3d point on plane
Post by: zammbi 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);
}