Question

I'm beginner in Android programming with Andengine framework. And I learn with tutorials on Internet.I don't know I misunderstand about root position of Scence and Velocity property in PhysicsHandler.

Here: enter image description here

And if my opinion is true. Thus, how to set Velocity property of PhysicsHandler to follow position of Scene?

This is my example: enter image description here

And my code:

scene.setOnSceneTouchListener(new IOnSceneTouchListener() {
            @Override
            public boolean onSceneTouchEvent(Scene pScene,
                    TouchEvent pSceneTouchEvent) {
                if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP) {
                    positionUpX = pSceneTouchEvent.getX();
                    positionUpY = pSceneTouchEvent.getY();

                    ball = new Ball(positionDownX, positionDownY, positionUpX,
                            positionUpY, mFaceTextureRegion,
                            getVertexBufferObjectManager());
                    scene.attachChild(ball);
                    return true;
                }
                if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN) {
                    positionDownX = pSceneTouchEvent.getX();
                    positionDownY = pSceneTouchEvent.getY();

                    return true;
                }
                return false;
            }
        });




private static class Ball extends AnimatedSprite {
        private final PhysicsHandler mPhysicsHandler;

        public Ball(final float pDownX, final float pDownY, //Start 
                final float pUpX,final float pUpY, //End
                final TiledTextureRegion pTextureRegion,
                final VertexBufferObjectManager pVertexBufferObjectManager) {
            super(pDownX, pDownY, pTextureRegion, pVertexBufferObjectManager);
            this.mPhysicsHandler = new PhysicsHandler(this);
            this.registerUpdateHandler(this.mPhysicsHandler);
            this.mPhysicsHandler.setVelocity(pUpX, pUpY);
        }

        @Override
        protected void onManagedUpdate(final float pSecondsElapsed) {
            if (this.mX < 0) {
                this.mPhysicsHandler.setVelocityX(BALL_VELOCITY);
            } else if (this.mX + this.getWidth() > CAMERA_WIDTH) {
                this.mPhysicsHandler.setVelocityX(-BALL_VELOCITY);
            }
            if (this.mY < 0) {
                this.mPhysicsHandler.setVelocityY(BALL_VELOCITY);
            } else if (this.mY + this.getHeight() > CAMERA_HEIGHT) {
                this.mPhysicsHandler.setVelocityY(-BALL_VELOCITY);
            }
            super.onManagedUpdate(pSecondsElapsed);
        }
    }
Was it helpful?

Solution

I have find answer with Physics Box2D and Mouse Joint

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top