place an object to top part of the screen

Started by tulsi, August 18, 2020, 07:39:24 AM

Previous topic - Next topic

tulsi

Hello,

I am new to JPCT, currently I am able to load .obj files and apply scale and rotation gesture.

Initially, when the object loads on the screen it appears at the center of screen. But I want it to be at top part of the screen.

Below is the picture of what I have now.



But I want as below.





Thanks. :) :)

AeroShark333

Hey,

Welcome :)

I believe you need to translate either the object upwards or the camera downwards.
I suggest you to try:
carObject3D.translate(x,y,z);
with some value for x,y,z (like 1.0f to try out first)

tulsi

Thanks for your reply.

I applied the translation and it worked but now another problem occurred.

When I zoom in or out its seems like it is moving to another place.

I have attached the video link below.

https://raw.githubusercontent.com/tulsiojha/data/master/video.mp4

AeroShark333

Hello,

That is most likely because the car object isn't centered for the camera.
The camera (using regular implementation) will zoom towards the center of the camera. (which is the exact center of the screen)

Since now the car is off-centered, you'd probably also need to apply either:
- transformation/translation to camera (to compensate for car offset)
- rotation of camera to follow the car (to compensate for car offset as well)
while zooming

Both implementations should work and keep the car above the center line as you wished for.

Suggestions:
camera.moveCamera(Camera.CAMERA_MOVEUP, zoomFloat*0.01f);
camera.rotateZ(zoomFloat*0.01f); // Or it's rotateX or rotateY... not sure
Where zoomFloat is some value passed from your zoom listener implementation.

Good luck :)

tulsi

#4
Thanks for your time.

Sorry.
But it is not working as expected.

Below is the code. May be I am doing something wrong at other place.
        public void onSurfaceChanged(GL10 gl, int w, int h) {
            if (fb != null) {
                fb.dispose();
            }
            fb = new FrameBuffer(gl, w, h);

            if (master == null) {

                world = new World();
                world.setAmbientLight(150, 150, 150);

                sun = new Light(world);
                sun.setIntensity(250, 250, 250);
               
                    for (File f: getFilesDir().listFiles()){

                        if (MimeTypeMap.getFileExtensionFromUrl(f.getName()).equalsIgnoreCase("jpg") || MimeTypeMap.getFileExtensionFromUrl(f.getName()).equalsIgnoreCase("png") || MimeTypeMap.getFileExtensionFromUrl(f.getName()).equalsIgnoreCase("jpeg")){
                            Log.d("Files", f.getName());
                            try {
                                TextureManager.getInstance().addTexture(f.getName(), new Texture(new FileInputStream(new File(getFilesDir(), f.getName()))));
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            }
                        }
                    }

                Object3D[] model = new Object3D[0];
                model = loadModel();

                thing=Object3D.createDummyObj();
                thing.build();
               
                Object3D o3d = new Object3D(0);
                Object3D temp = null;
                for (int i = 0; i < model.length; i++) {
                    temp = model[i];
                    temp.setCenter(SimpleVector.ORIGIN);
                    temp.rotateX((float)( -.5*Math.PI));
                    temp.rotateMesh();
                    temp.setRotationMatrix(new Matrix());
                    temp.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS);
                    temp.setCollisionOptimization(Object3D.COLLISION_DETECTION_OPTIMIZED);
                    temp.build();
                    temp.strip();
                    world.addObject(temp);
                    temp.addParent(thing);
                    temp.addCollisionListener(collisionListener);

                }

                world.getCamera().setPosition(0, 0, -20);
                world.getCamera().lookAt(thing.getTransformedCenter());
                thing.rotateX(180);
                thing.rotateMesh();
                thing.translate(thing.getCenter().x, -10f, thing.getCenter().z);
                thing.translateMesh();

                MemoryHelper.compact();

                if (master == null) {
                    Logger.log("Saving master Activity!");
                    master = MainActivity.this;
                }
            }
        }

public void onDrawFrame(GL10 gl) {
            if (touchTurn != 0) {
                thing.rotateY(touchTurn);
                touchTurn = 0;
            }

            if (touchTurnUp != 0) {
                thing.rotateX(touchTurnUp);
                touchTurnUp = 0;
            }

            world.getCamera().setFOV(2-mScaleFactor);

            fb.clear(back);
            world.renderScene(fb);
            world.draw(fb);

            fb.display();


            if (System.currentTimeMillis() - time >= 1000) {
                Logger.log(fps + "fps");
                fps = 0;
                time = System.currentTimeMillis();
            }
            fps++;
        }


    private Object3D[] loadModel() {
        Object3D[] model = new Object3D[0];
        try {
            model = Loader.loadOBJ(new FileInputStream(new File(getFilesDir(), "hammer.obj")), new FileInputStream(new File(getFilesDir(), "hammer.mtl")), 1f);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return model;
    }


For scaling
  float preV=mScaleFactor, cur=mScaleFactor;
    class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {


        @Override
        public boolean onScale(ScaleGestureDetector detector) {
         mScaleFactor*= detector.getScaleFactor();
            cur=mScaleFactor;
            mScaleFactor = Math.max(0.5f,Math.min(mScaleFactor, 1.5f));

            if (preV>cur){
                Log.d("Scale", "onScale: "+"down");
                if (mScaleFactor >=0.5 && mScaleFactor<=1.5){
                    world.getCamera().moveCamera(Camera.CAMERA_MOVEDOWN, mScaleFactor*1f);
                }
            }else {
                Log.d("Scale", "onScale: "+"up");
                if (mScaleFactor >=0.5 && mScaleFactor<=1.5){
                    world.getCamera().moveCamera(Camera.CAMERA_MOVEUP, mScaleFactor*1f);
                }
            }
            preV = mScaleFactor;
//            cube0.scale();

            Log.d("SCaled", ""+"prev: "+preV+" cur: "+cur);
            return true;
        }

    }


I Hope you don't mind.. :)

tulsi

I think i have solved it.

Below is the video result.
https://raw.githubusercontent.com/tulsiojha/data/master/improved.mp4

I have used object.setScale() method instead of camera.setFOV() method.
Below is the code for it.
class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {


        @Override
        public boolean onScale(ScaleGestureDetector detector) {
         mScaleFactor*= detector.getScaleFactor();
            cur=mScaleFactor;
            mScaleFactor = Math.max(0.5f,Math.min(mScaleFactor, 2f));

               if (thing.getScale() >=0.5 && mScaleFactor >=0.5){
                   thing.setScale(mScaleFactor);
               }
               if (thing.getScale()<0.5)
                   thing.setScale(1);


            Log.d("Scaled", ""+thing.getScale());
            return true;
        }

    }


I don't know if this is the good method to it do or not.
And Thanks for your help.

AeroShark333

Ah yes, you can of course also scale up the Object3D instead of moving the Camera closer.

Well, no problem for the help, I'm glad you got it sorted :)