How to place object at specific location of world?

Started by jaychang0917, March 24, 2017, 04:37:34 AM

Previous topic - Next topic

jaychang0917

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?


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

EgonOlsen

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...

jaychang0917

arrr... i can blit it, it is much more efficient and easy way. Thanks your suggestion.

jaychang0917

Quote from: 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...
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?

AeroShark333

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 :)

jaychang0917

#5
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,

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,

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!

AeroShark333

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.

jaychang0917

#7
Quote from: 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.

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,

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
....

AeroShark333

Oh my bad...

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

I hope this will work...

jaychang0917

Although the direction of movement is correct now, but the proportion of movement seems less than before.

AeroShark333

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 :)

jaychang0917

Quote from: 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 :)

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.

AeroShark333

Uhm... I don't quite understand what you mean... Sorry

jaychang0917

Quote from: AeroShark333 on April 01, 2017, 12:46:32 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.

jaychang0917

I fixed by using model.getRoot().clearTranslation(); :)