简体   繁体   English

AndEngine GLES1:在碰撞过程中识别对象

[英]AndEngine GLES1: Identifying Objects during Collision

I am creating a 'Chain Reaction' type game. 我正在创建“连锁反应”类型的游戏。

In onLoadResources I create 4 static bodies (ground, roof, left border and right border) to act as boundaries. 在onLoadResources中,我创建了4个静态实体(地面,屋顶,左边界和右边界)作为边界。 I then create 30 dynamic bodies (which are balls that bounce around). 然后,我创建30个动态物体(它们是反弹的球)。

My question is, how can I detect which ball has just collided with a boundary? 我的问题是,如何检测哪个球刚与边界碰撞?

The way I have it right now, I believe the last 'particle_body' that is created, is the only one that is ever modified during a collision. 我现在所拥有的方式,我相信最后创建的“ particle_body”是碰撞期间唯一被修改的方式。

Any suggestions?? 有什么建议么??

Here is my code: 这是我的代码:

this.mPhysicsWorld = new FixedStepPhysicsWorld(60, new Vector2(0, 0), false); // Gravity (x, y) scene.registerUpdateHandler(mPhysicsWorld);

  // Create the ground groundFixture = PhysicsFactory.createFixtureDef(1, 1.0f, 0.5f); ground = new Rectangle(0, CAMERA_HEIGHT-1, CAMERA_WIDTH, CAMERA_HEIGHT); ground.setColor(0.0f, 0.0f, 0.0f); ground_body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, this.ground, BodyType.StaticBody, groundFixture); ground_body.setUserData("ground"); mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(ground, ground_body, true, false)); scene.attachChild(ground); // Create the roof roofFixture = PhysicsFactory.createFixtureDef(1, 1.0f, 0.5f); roof = new Rectangle(0, 0, CAMERA_WIDTH, 1); roof.setColor(0.0f, 0.0f, 0.0f); roof_body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, this.roof, BodyType.StaticBody, roofFixture); roof_body.setUserData("roof"); mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(roof, roof_body, true, false)); scene.attachChild(roof); // Create the left border leftBorderFixture = PhysicsFactory.createFixtureDef(1, 1.0f, 0.5f); leftBorder = new Rectangle(0, 0, 1, CAMERA_HEIGHT); leftBorder.setColor(0.0f, 0.0f, 0.0f); leftBorder_body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, this.leftBorder, BodyType.StaticBody, leftBorderFixture); leftBorder_body.setUserData("leftBorder"); mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(leftBorder, leftBorder_body, true, false)); scene.attachChild(leftBorder); // Create the right border rightBorderFixture = PhysicsFactory.createFixtureDef(1, 1.0f, 0.5f); rightBorder = new Rectangle(CAMERA_WIDTH-1, 0, CAMERA_WIDTH, CAMERA_HEIGHT); rightBorder.setColor(0.0f, 0.0f, 0.0f); rightBorder_body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, this.rightBorder, BodyType.StaticBody, rightBorderFixture); rightBorder_body.setUserData("rightBorder"); mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(rightBorder, rightBorder_body, true, false)); scene.attachChild(rightBorder); // Prepare a random number generator Random rand = new Random(); // Create Particles for(int i=0; i < 30; i++) { int x = rand.nextInt(CAMERA_WIDTH) - 1; int y = rand.nextInt(CAMERA_HEIGHT) - 1; this.particle = new Particle(x, y, this.mPurpleParticleTextureRegion, 100); particle.setScale(0.3f); scene.attachChild(particle); particleFixture = PhysicsFactory.createFixtureDef(10, 0.9f, 0.1f); particle_body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, this.particle, BodyType.DynamicBody, particleFixture); particle_body.setUserData("particle_" + i); mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(this.particle, particle_body, true, false)); // Assign a random velocity to the particle x = rand.nextInt(20) + 3; y = rand.nextInt(20) + 3; particle_body.setLinearVelocity(new Vector2(x, y)); // x, y impulse mParticleList.add(particle); mBodyList.add(particle_body); mFixtureList.add(particleFixture); particleNumAR[i] = i; } // Create the Physics Collision Detection listener mPhysicsWorld.setContactListener(new ContactListener() { public void endContact(Contact contact) { } public void beginContact(Contact contact) { if (contact.getFixtureA().getBody().getUserData() != null && contact.getFixtureB().getBody().getUserData() != null) { final String objA = (String)contact.getFixtureA().getBody().getUserData(); final String objB = (String)contact.getFixtureB().getBody().getUserData(); // Particle hits ground if (objA.equals("ground") || objB.equals("ground")) { Random rand2 = new Random(); int xx = rand2.nextInt(20) + 3; int yy = rand2.nextInt(10) + 3; particle_body.setLinearVelocity(new Vector2(xx, yy)); // x, y impulse } // Particle hits roof if (objA.equals("roof") || objB.equals("roof")) { Random rand2 = new Random(); int xx = rand2.nextInt(20) + 3; int yy = rand2.nextInt(10) + 3; particle_body.setLinearVelocity(new Vector2(xx, yy)); // x, y impulse } // Particle hits left border if (objA.equals("leftBorder") || objB.equals("leftBorder")) { Random rand2 = new Random(); int xx = rand2.nextInt(10) + 3; int yy = rand2.nextInt(20) + 3; particle_body.setLinearVelocity(new Vector2(xx, yy)); // x, y impulse } // Particle hits right border if (objA.equals("rightBorder") || objB.equals("rightBorder")) { Random rand2 = new Random(); int xx = rand2.nextInt(10) + 3; int yy = rand2.nextInt(20) + 3; particle_body.setLinearVelocity(new Vector2(xx, yy)); // x, y impulse } } } @Override public void preSolve(Contact contact, Manifold oldManifold) { } @Override public void postSolve(Contact contact, ContactImpulse impulse) { } }); 

You can use the other fixture of the contact. 您可以使用触点的其他固定装置。 One of the fixtures will be one of your walls. 固定装置之一将是您的墙壁之一。 The other fixture will be your particle that collided. 另一个固定装置将是碰撞的粒子。

So you could do something like this (untested pseudo code): 因此,您可以执行以下操作(未经测试的伪代码):

// I don't know what class the Box2D body is in this case (b2Body maybe? or just Body?)
// so please insert appropriate type

final String objA = (String)contact.getFixtureA().getBody().getUserData();
Body bodyA = contact.getFixtureA().getBody();
final String objB = (String)contact.getFixtureB().getBody().getUserData();
Body bodyB = contact.getFixtureB().getBody();


Body particleBody = objA.startsWith("particle_") ? bodyA : bodyB; 

...

// then use particleBody for setting the velocity in your different if cases

particleBody.setLinearVelocity(new Vector2(x,y));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM