1

I want to limit the area to move the sprite object only on this area (for example a area dimensions 200x200).

I would to create a box2D 200x200 where the sprites can moved only on this area

How do you do that please?

@Override
public Scene onCreateScene() {
    this.mEngine.registerUpdateHandler(new FPSLogger());

    final Scene scene = new Scene();
    scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));

    final float centerX = (CAMERA_WIDTH - this.mFaceTextureRegionLetterOne
            .getWidth()) / 2;
    final float centerY = (CAMERA_HEIGHT - this.mFaceTextureRegionLetterOne
            .getHeight()) / 2;
    final Sprite letterOne = new Sprite(centerX - centerX / 2, centerY
            - centerY / 2, this.mFaceTextureRegionLetterOne,
            this.getVertexBufferObjectManager()) {
        @Override
        public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
                final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
            this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2,
                    pSceneTouchEvent.getY() - this.getHeight() / 2);
            return true;
        }


    };

    final Sprite letterTwo = new Sprite(centerX - centerX / 2, centerY,
            this.mFaceTextureRegionLetterTwo,
            this.getVertexBufferObjectManager()) {
        @Override
        public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
                final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
            this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2,
                    pSceneTouchEvent.getY() - this.getHeight() / 2);
            //int count = scene.getChildCount();
            //for(int i = 0; i < count; i++) {

            IEntity entity = scene.getChildByIndex(1);
            if (entity instanceof Sprite) {
                if (entity.getUserData().equals("sprite"))
                    if (((Sprite) entity).collidesWith(letterOne))
                        Log.v("colission", "face_box is collised on google plus -> letterTwo on letterOne");
            }
            //}
            return true;
        }
    };
    letterTwo.setUserData("sprite");

    final Sprite boxArea = new Sprite(centerX, centerY,
            this.mFaceTextureRegionBox, this.getVertexBufferObjectManager());
    letterOne.setScale(2);
    scene.attachChild(letterOne);
    scene.registerTouchArea(letterOne);


    letterTwo.setScale(2);
    scene.attachChild(letterTwo);

    scene.registerTouchArea(letterTwo);

    boxArea.setScale(2);
    scene.attachChild(boxArea);

    scene.setTouchAreaBindingOnActionDownEnabled(true);

    return scene;
}

Thank you.

Alexey
  • 714
  • 8
  • 21
user1400390
  • 77
  • 1
  • 5

3 Answers3

0

I don't know box2D, but normally you would check the edges of the sprite on each movement, if they do not overlap the edges of the area.

If your sprite is represented by a Rectangle, and the area where you can move also represented by a Rectangle, this could be done easy.

But first, check the box2D API, maybe it has already some helper methods to ease this task, something like:

obect.overlaps(object)
Andy Res
  • 15,963
  • 5
  • 60
  • 96
0

You should create a Rectangle of 200X200 and then you should use create a body of the rectangle using the Box2D Physics. And for Fixture definition for rectangle you can set density, Elasticity and Friction so that your sprites will be treated accordingly when they collide with the boundary of the Rectangle.

To create a Rectangle you can refer the Examples of the Andengine.

Dharmendra
  • 33,296
  • 22
  • 86
  • 129
0

With your code, you seem to try to do collision checking with your sprites ?!

It would be complicated and not nice when you try to do things without physics bodies. So lets use the physics bodies with your sprites.

But note that, DO NOT create a solid box body for your area (containing your sprites), lets use 4 separated body walls (left, top, right, bottom) to form a closed box; because game engine can only check collision with solid shapes.

The following is the code for reference:

/**
 * @param pScene
 *      Sence of the game, get from class member
 * @param pWorld
 *      physics world of the game, get from class member
 */
public void CreateSprites(final Scene pScene, final PhysicsWorld pWorld)
{
    final FixtureDef mFixtureDef = PhysicsFactory.createFixtureDef(10, 1.1f, 0.0f);//should be placed as member of class
    final Sprite letterTwo = new Sprite(centerX - centerX / 2, centerY, 
            this.mFaceTextureRegionLetterTwo,
            this.getVertexBufferObjectManager());

    final Body letterTwoBody = PhysicsFactory.createBoxBody(pWorld, letterTwo, BodyType.DynamicBody, mFixtureDef);
    letterTwo.setUserData(letterTwoBody); // for later sprite-body attachment access

    pScene.attachChild(letterTwo);
    pWorld.registerPhysicsConnector(new PhysicsConnector(letterTwo, letterTwoBody, true, true));
}

/** Create the walls, in these boudaries sprites will move */
public void InitBoxWalls(Scene pScene, PhysicsWorld pWorld)
{
    final float WALL_MARGIN_WIDTH = 5f;
    final float WALL_MARGIN_HEIGHT = 10f;
    final VertexBufferObjectManager vertexBufferObjectManager = this.getVertexBufferObjectManager();

    mWallGround = new Rectangle(-WALL_MARGIN_WIDTH, mCameraHeight + WALL_MARGIN_HEIGHT - 2, mCameraWidth + 2*WALL_MARGIN_WIDTH, 2, vertexBufferObjectManager);
    mWallRoof = new Rectangle(-WALL_MARGIN_WIDTH, -WALL_MARGIN_HEIGHT, mCameraWidth + 2*WALL_MARGIN_WIDTH, 2, vertexBufferObjectManager);
    mWallLeft = new Rectangle(-WALL_MARGIN_WIDTH, -WALL_MARGIN_HEIGHT, 2, mCameraHeight + 2*WALL_MARGIN_HEIGHT, vertexBufferObjectManager);
    mWallRight = new Rectangle(mCameraWidth + WALL_MARGIN_WIDTH - 2, -WALL_MARGIN_HEIGHT, 2, mCameraHeight + 2*WALL_MARGIN_HEIGHT, vertexBufferObjectManager);

    PhysicsFactory.createBoxBody(pWorld, mWallGround, BodyType.StaticBody, mWallFixtureDef);
    PhysicsFactory.createBoxBody(pWorld, mWallRoof, BodyType.StaticBody, mWallFixtureDef);
    PhysicsFactory.createBoxBody(pWorld, mWallLeft, BodyType.StaticBody, mWallFixtureDef);
    PhysicsFactory.createBoxBody(pWorld, mWallRight, BodyType.StaticBody, mWallFixtureDef);

    pScene.attachChild(mWallGround);
    pScene.attachChild(mWallRoof);
    pScene.attachChild(mWallLeft);
    pScene.attachChild(mWallRight);
}

And the last thing you should do is to look up example of Physics/MouseJoint in AndEngine examples.

Kelvin Trinh
  • 1,248
  • 7
  • 15