www.jpct.net

jPCT-AE - a 3d engine for Android => Support => Topic started by: jaychang0917 on March 24, 2017, 04:37:34 am

Title: How to place object at specific location of world?
Post by: jaychang0917 on March 24, 2017, 04:37:34 am
I wanna place an object at the bottom right corner of world with the following code, it doesn't work.

How to place object at specific location of world?

Code: [Select]
Texture waterMarkTexture = new Texture(BitmapHelper.rescale(BitmapHelper.convert(context.getResources().getDrawable(R.drawable.logo_camino_white)), 128, 128));
      boolean isWaterMarkTextureAdded = TextureManager.getInstance().containsTexture("watermark");
      if (!isWaterMarkTextureAdded) {
        TextureManager.getInstance().addTexture("watermark", waterMarkTexture);
      }
      Object3D waterMarkModel = Primitives.getPlane(1, AppUtils.dp2px(context, 4));
      waterMarkModel.setTexture("watermark");
      int screenX = AppUtils.getScreenWidthPixels(context) - 20;
      SimpleVector position = Interact2D.reproject2D3DWS(world.getCamera(), frameBuffer, screenX, screenX);
      waterMarkModel.setOrigin(position);
      waterMarkModel.build();
      world.addObject(waterMarkModel);
Title: Re: How to place object at specific location of world?
Post by: EgonOlsen on March 24, 2017, 11:07:33 am
Just a quick question: Why don't you just blit the watermark instead of creating an object for it? Or you could use an Overlay instead...if your manual GL calls don't break its functionality...
Title: Re: How to place object at specific location of world?
Post by: jaychang0917 on March 24, 2017, 03:29:20 pm
arrr... i can blit it, it is much more efficient and easy way. Thanks your suggestion.
Title: Re: How to place object at specific location of world?
Post by: jaychang0917 on March 30, 2017, 05:48:41 pm
Just a quick question: Why don't you just blit the watermark instead of creating an object for it? Or you could use an Overlay instead...if your manual GL calls don't break its functionality...
Hi, i still want to know how can I move an object. I call translate() in the onDraw() loop, the model will keep moving instead of move to that position. Is there any method that can move the object to an absolute position and it stops at there?
Title: Re: How to place object at specific location of world?
Post by: AeroShark333 on March 30, 2017, 11:37:19 pm
I guess it's best to do the translation when initializing your objects. (So it's just executed once rather than nonstop)
If you really need to do absolute translation during draw calls I guess this could work:

Private variable somewhere in your renderer class:
private SimpleVector absCoords = new SimpleVector (3.3f,3.3f,3.3f);

In your OnDraw loop:
yourObject.translate(absCoords.x -yourObject.getTransformedCenter().x,
absCoords.y -yourObject.getTransformedCenter().y,
absCoords.z -yourObject.getTransformedCenter().z);

This should pretty much keep your Object3D at the absCoords location. Even when you change absCoords during runtime it should still work :)
Title: Re: How to place object at specific location of world?
Post by: jaychang0917 on March 31, 2017, 08:39:39 am
Thanks AeroShark333, it works.

Let's say the model is rendered on a 1440*1440 screen, and I have a 2d point like (x, y), i want to place the model base on this point, so i try to translate this point to world space point and do translation with the following code,
Code: [Select]
SimpleVector targetPosition = new SimpleVector();

Interact2D.reproject2D3DWS(world.getCamera(), frameBuffer,
      x, y, world.getCamera().getPosition().z, targetPosition);

model.getRoot().translate(
      targetPosition.x -model.getRoot().getTransformedCenter().x,
      targetPosition.y -model.getRoot().getTransformedCenter().y,
      0);

but i found that the targetPosition.x will be decrease if x increase, I log the result as follow,
Code: [Select]
2d: PointF(518.5, 746.5) => 3d:(14.027778,-1.8055556,-80.0)
2d: PointF(677.16003, 587.04706)  => 3d:(2.9861112,9.236111,-80.0)

So my model will be moved oppositely. Have any idea? Thanks!
Title: Re: How to place object at specific location of world?
Post by: AeroShark333 on March 31, 2017, 08:57:06 am
Oh hmmm, well you could try to swap this:
model.getRoot().translate(
      targetPosition.x -model.getRoot().getTransformedCenter().x,
      targetPosition.y -model.getRoot().getTransformedCenter().y,
      0);

To:
model.getRoot().translate(
      -targetPosition.x +model.getRoot().getTransformedCenter().x,
      -targetPosition.y +model.getRoot().getTransformedCenter().y,
      0);

Which should swap the translation direction into the opposite X and Y direction.
Title: Re: How to place object at specific location of world?
Post by: jaychang0917 on March 31, 2017, 09:22:39 am
Oh hmmm, well you could try to swap this:
model.getRoot().translate(
      targetPosition.x -model.getRoot().getTransformedCenter().x,
      targetPosition.y -model.getRoot().getTransformedCenter().y,
      0);

To:
model.getRoot().translate(
      -targetPosition.x +model.getRoot().getTransformedCenter().x,
      -targetPosition.y +model.getRoot().getTransformedCenter().y,
      0);

Which should swap the translation direction into the opposite X and Y direction.

I tried your code, the model will continuously move to left side instead stop at a position, and i log the parameters x and y as follows,
Code: [Select]
x: -7.5 y: 7.4305553
x: -19.51389 y: 9.097222
x: -39.305557 y: 15.000001
x: -83.61111 y: 26.180557
x: -167.63887 y: 50.555557
....
Title: Re: How to place object at specific location of world?
Post by: AeroShark333 on March 31, 2017, 11:18:20 am
Oh my bad...

model.getRoot().translate(
      -targetPosition.x -model.getRoot().getTransformedCenter().x,
      -targetPosition.y -model.getRoot().getTransformedCenter().y,
      0);

I hope this will work...
Title: Re: How to place object at specific location of world?
Post by: jaychang0917 on March 31, 2017, 12:45:56 pm
Although the direction of movement is correct now, but the proportion of movement seems less than before.
Title: Re: How to place object at specific location of world?
Post by: AeroShark333 on March 31, 2017, 03:18:54 pm
Hmmm you could add some scalar to the translation...
model.getRoot().translate(
      -1.5f * targetPosition.x -model.getRoot().getTransformedCenter().x,
      -1.5f * targetPosition.y -model.getRoot().getTransformedCenter().y,
      0);

With 1.5f being your scalar... Which could be any value up to you :)
Title: Re: How to place object at specific location of world?
Post by: jaychang0917 on March 31, 2017, 07:04:24 pm
Hmmm you could add some scalar to the translation...
model.getRoot().translate(
      -1.5f * targetPosition.x -model.getRoot().getTransformedCenter().x,
      -1.5f * targetPosition.y -model.getRoot().getTransformedCenter().y,
      0);

With 1.5f being your scalar... Which could be any value up to you :)

This did come across my mind, but the issue is that the model will be shifted by 1.5 times at the first rendering. You will see that the model is not at the center at first time.
Title: Re: How to place object at specific location of world?
Post by: AeroShark333 on April 01, 2017, 12:46:32 am
Uhm... I don't quite understand what you mean... Sorry
Title: Re: How to place object at specific location of world?
Post by: jaychang0917 on April 01, 2017, 05:08:57 am
Uhm... I don't quite understand what you mean... Sorry

The application i am developing is to draw points of detected face using front camera and place a face model on top of those points. so if i use 1.5f scalar, the model will be shifted right a little.
Title: Re: How to place object at specific location of world?
Post by: jaychang0917 on April 01, 2017, 09:03:15 am
I fixed by using model.getRoot().clearTranslation(); :)