Skip to content

Sample Code ( Object Handling )

KimKyuyeon edited this page Mar 14, 2021 · 3 revisions
  1. Move

[1] In the onCreate function, a surfaceview is formed, an object is created when touched, and moved to the tapHelper class.


protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        surfaceView = findViewById(R.id.surfaceview);
        displayRotationHelper = new DisplayRotationHelper(/*context=*/ this);
        mRotationDetector = new RotationGestureDetector(this);
        scaleGestureDetector = new MyScaleGestures(this);
        
        // Set up tap listener.
        tapHelper = new TapHelper(/*context=*/ this);
        surfaceView.setOnTouchListener(tapHelper);
        
        // Set up renderer.
        surfaceView.setPreserveEGLContextOnPause(true);
        surfaceView.setEGLContextClientVersion(2);
        surfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0); // Alpha used for plane blending.
        surfaceView.setRenderer(this);
        surfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
        surfaceView.setWillNotDraw(false);

        installRequested = false;
        calculateUVTransform = true;

        depthSettings.onCreate(this);
        ImageButton settingsButton = findViewById(R.id.settings_button);
        settingsButton.setOnClickListener(this::launchSettingsMenuDialog);
    }

[2] The onScroll method within the TapHelper class uses a counter to calculate the number of touches on the screen. If the number of touches is less than two on the screen, that is, moving with one finger, the object is redrawn at that point.


public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                                if (mPtrCount < 2) {
                                    queuedSingleTaps.offer(e2);
                                    return true;
                                } else {
                                    return false;
                                }
                            }
  1. Rotation

[1]The onTouchEvent method in the RotationGestureDetector class receives the (x, y) coordinate value of the point when touched with two fingers.

[2] Calculates the angle value when handled through the received (x, y) coordinate value.


private float angleBetweenLines(float fX, float fY, float sX, float sY, float nfX, float nfY, float nsX, float nsY) {
        float angle1 = (float) Math.atan2((fY - sY), (fX - sX));
        float angle2 = (float) Math.atan2((nfY - nsY), (nfX - nsX));

        float angle = ((float) Math.toDegrees(angle1 - angle2)) % 360;
        if (angle < -180.f) angle += 360.0f;
        if (angle > 180.f) angle -= 360.0f;
        return angle;
    }

[3]The corresponding angle value is updated with the setRotateM function. (Continuously updating to draw in ObjectRender class)

[4] Rotation rotates on the y-axis.


Matrix.setRotateM(mRotationMatrix, 0, GlobalClass.rotateF, 0.0f, 1.0f, 0.0f);
    Matrix.multiplyMM(mFinalModelViewProjectionMatrix, 0, modelViewProjectionMatrix, 0, mRotationMatrix, 0);
    mFinalModelViewProjectionMatrix = modelViewProjectionMatrix;
    GLES20.glUseProgram(program);
  1. Remove

When the trash can button is pressed, the delete method linked to the button prevents the object from being tracked in the array variable that contains the object when the number of objects is 1 or more, and removes the object using remove().


public void deleteMethod(View v) {
        if (anchors.size() >= 1) {
            anchors.get(anchors.size() - 1).anchor.detach();
            anchors.remove(anchors.size() - 1);
        }
        Toast.makeText(getApplicationContext(), "삭제되었습니다", Toast.LENGTH_SHORT).show();
    }
  1. Scale Modification

1x for 1.0f side and 2x for 2.0f side in scaleFactor variable The updateModelMatrix method on the ObjectRender is the method that sets the scale of the model and multiplies the overall scale by the parameter.


for (ColoredAnchor coloredAnchor : anchors) {
     
                if (coloredAnchor.anchor.getTrackingState() != TrackingState.TRACKING) {
                    continue;
                }
                // Get the current pose of an Anchor in world space. The Anchor pose is updated
                // during calls to session.update() as ARCore refines its estimate of the world.
                coloredAnchor.anchor.getPose().toMatrix(anchorMatrix, 0);
           
                // Update and draw the model and its shadow.
                virtualObject.updateModelMatrix(anchorMatrix, scaleFactor);
                virtualObject.draw(viewmtx, projmtx, colorCorrectionRgba, coloredAnchor.color);
            }

Clone this wiki locally