www.jpct.net

jPCT-AE - a 3d engine for Android => Support => Topic started by: Babu on November 23, 2011, 04:22:35 pm

Title: Translating an Object3D to the touch point
Post by: Babu on November 23, 2011, 04:22:35 pm
Hi Raft,
I am back  ;D

I am just trying a simple thing to make an Object3D follow my touch points i.e. the Object3D moves to whatever point that I touch on the screen.  But I am unsuccessful  :(

I used the basic cube example of the Hello World AE.  I changed the onTouchEvent() as below:
Code: [Select]
public boolean onTouchEvent(MotionEvent me)
{
if (me.getAction() == MotionEvent.ACTION_DOWN)
{
xpos = me.getX();
ypos = me.getY();

mTouchVector = Interact2D.reproject2D3DWS(world.getCamera(), fb,
(int)xpos, (int)ypos);
return true;
}

try
{
Thread.sleep(15);
}
catch (Exception e)
{
// No need for this...
}
return super.onTouchEvent(me);
}

Then I changed the onDrawFrame as below:
Code: [Select]
public void onDrawFrame(GL10 gl)
{
try
{
fb.clear(back);

if(mTouchVector != null)
{
cube.clearTranslation();
cube.translate(mTouchVector);
}

world.renderScene(fb);
world.draw(fb);
fb.display();
}
catch (Exception e)
{
Logger.log(e, Logger.MESSAGE);
}
}

I see that the cube is moving, but very little!!  Can you please let me know if I am missing something  :-[
Title: Re: Translating an Object3D to the touch point
Post by: raft on November 23, 2011, 04:43:44 pm
I'm not quite sure (quite a time passed last I used this stuff). it seemed ok to me.

maybe it's a sampling issue? what is your screen resolution? what does FrameBuffer.getHeight() and getWidth() return?

Note: Moved topic as this is not Bones related
Title: Re: Translating an Object3D to the touch point
Post by: Babu on November 23, 2011, 06:04:48 pm
Following are values

11-23 22:33:42.735: D/HelloJPCT(679): frame-buffer-width = 320  frame-buffer-height = 480
11-23 22:33:42.735: D/HelloJPCT(679): TouchEvent-xpos = 98.30721  TouchEvent-ypos = 362.75574
11-23 22:33:42.735: D/HelloJPCT(679): mTouchVector.x = -0.2421875  mTouchVector.y = 0.4765625  mTouchVector.z = 1.0

Title: Re: Translating an Object3D to the touch point
Post by: raft on November 23, 2011, 06:22:54 pm
these results seemed as they are in camera space to me. are you sure you are actually using Interact2D.reproject2D3DWS instead of Interact2D.reproject2D3D? former returns result in world space, later in camera space.

if so, there may be a bug. Egon will clarify either way
Title: Re: Translating an Object3D to the touch point
Post by: Babu on November 23, 2011, 07:06:52 pm
The code snippet I provided was a copy-paste from my source file...

"mTouchVector = Interact2D.reproject2D3DWS(world.getCamera(), fb,
                  (int)xpos, (int)ypos);"
Title: Re: Translating an Object3D to the touch point
Post by: raft on November 23, 2011, 07:25:35 pm
well, i don't know then ??? Egon should hopefully help..
Title: Re: Translating an Object3D to the touch point
Post by: EgonOlsen on November 23, 2011, 08:10:59 pm
The coordinates look as if you've not moved the camera at all, which is why they look like camera space. That method projects the 2D coordinate to a 3D point on the view plane, i.e. on z=1 in camera space. This point will then be transformed into world space. If the camera hasn't moved, both spaces are equal.
However, it's unlikely that you want to move in view plane.
Like i did in the other thread that is currently dealing with this issue, i'll link to a recent thread for desktop jPCT that offers a little example at the bottom of the thread: http://www.jpct.net/forum2/index.php/topic,2413.0.html (http://www.jpct.net/forum2/index.php/topic,2413.0.html)
The basic idea is to calculate the position in world space on a given plane (in world space, not in camera space) based on the result from reproject2D3DWS. You can do it that way or by doing a collision detection based on this vector. I'm mentioning that in the thread too.
Hope this helps.
Title: Re: Translating an Object3D to the touch point - Resolved
Post by: Babu on November 24, 2011, 02:35:19 pm
Thanks a lot, Egon.  Your code in that thread works perfect!!  :D

I am attaching the working Android code...

[attachment deleted by admin]
Title: Re: Translating an Object3D to the touch point
Post by: gourabf9systems on March 05, 2013, 11:57:33 am
Can anyone post , working example of this topic. that solved by anyone.
Title: Re: Translating an Object3D to the touch point
Post by: EgonOlsen on March 05, 2013, 08:01:14 pm
Can anyone post , working example of this topic. that solved by anyone.
The post right above yours has a working Activity attached.
Title: Re: Translating an Object3D to the touch point
Post by: gourabf9systems on March 06, 2013, 11:17:52 am
Hi EgonOlsen,

I use example made by Babu. I modify that code for my self. I attach that with this post.Can you peace check it. I just wnat to make the Object3D smaller that present when I move this from +y axis to -y axis.   Can you explain bellow lines


int Z_PLANE  = 0;
float a = (Z_PLANE - camPos.z) / mTouchVector.z;
         float x = camPos.x +  a * mTouchVector.x;
         float y = camPos.y + a * mTouchVector.y;

[attachment deleted by admin]
Title: Re: Translating an Object3D to the touch point
Post by: EgonOlsen on March 06, 2013, 08:21:29 pm
If you want to make an object smaller or bigger, just use Object3D.scale() or Object3D.setScale(). And i don't see how this relates to the lines of code that you've posted... ???

Anyway, these lines are a pretty simple system of linear equations to find the point on a given z-plane. It's basic math.
Title: Re: Translating an Object3D to the touch point
Post by: gourabf9systems on March 07, 2013, 08:04:48 am
Dear EgonOlsen,

I search lot's in google to get a sample code of game using jpct ae. But I have not get yet. If you post source code of game develop by you using JPCT ae. Then I able to learn some thing from that. If u post a video tutorial for this then I am very happy and become muster to use it.

Thanks
Gourab Singha
Title: Re: Translating an Object3D to the touch point
Post by: EgonOlsen on March 07, 2013, 08:55:21 am
You might want to look at the Alien Runner sources. There have been written for the G1 class of devices, so there's a lot of micro-optimizing in them that might not be needed on current devices. Keep that in mind just in case you are wondering why some things have been done in the way that they are. Here you go: http://www.jpct.net/forum2/index.php/topic,1629.msg14207.html#msg14207 (http://www.jpct.net/forum2/index.php/topic,1629.msg14207.html#msg14207)