English 中文(简体)
roid星和eng星的碰撞
原标题:Sprite collisions in android andengine

I m developing a simple game by andengine. I have 10 balls which are moving randomly on screen.i m importing the balls as picture in sprites.if they move at the same coordinate , they pass though their own insides.but i want: if they move at the same coodirnates ,they should change their directions.so they cannot pass through their insides.how can i do that?

   private Runnable mStartCircle = new Runnable() {
      public void run() {
        int i = circleNumber++;
        Scene scene = Level1Activity.this.mEngine.getScene();
        float startY = -64.0f;
        float startX = randomNumber.nextFloat()*(CAMERA_WIDTH-70.0f);
        float a= randomNumber.nextFloat()*(CAMERA_WIDTH-70.0f);
        circles[i] = new Sprite(startX, startY, textRegCircle[i]);
        circles[i].registerEntityModifier(
                (IEntityModifier) new SequenceEntityModifier (
                            new MoveModifier(10.0f, circles[i].getX(),  a, 
                                    circles[i].getY(),CAMERA_HEIGHT+64.0f)));
        }
        scene.getLastChild().attachChild(circles[i]);
        if (circleNumber < 10){
            mHandler.postDelayed(mStartCircle,1000);
        }
    }
 };
问题回答

Each object(ball) requires a bounding box, or in your case a bounding circle, which is equal to the size of your sprite.

当游戏更新和任何球场变化时,你必须测试碰撞。

Circle to circle collision testing is the simplest type to do. if distance between (ball1.pos + ball2.pos) is less than (ball1.radius + ball2.radius) = collision.

然后,你处理碰撞问题,扭转这种局面或计算新的动力或东西。 (你们也需要把物体分开,以便不再相互串通)

Just apply a physical connector between balls:

因此,它会串通一气,背后。

final FixtureDef boxFixtureDef = PhysicsFactory.createFixtureDef(0.1f, 0.5f, 0.5f);
final Body ballBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, circles[i],BodyType.DynamicBody, boxFixtureDef);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(circles[i], ballBody, true, true));
this.mScene.attachChild(circles[i]);




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签