com.badlogic.gdx.physics.box2d.Body.setUserData()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(11.1k)|赞(0)|评价(0)|浏览(135)

本文整理了Java中com.badlogic.gdx.physics.box2d.Body.setUserData()方法的一些代码示例,展示了Body.setUserData()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Body.setUserData()方法的具体详情如下:
包路径:com.badlogic.gdx.physics.box2d.Body
类名称:Body
方法名:setUserData

Body.setUserData介绍

[英]Set the user data
[中]

代码示例

代码示例来源:origin: libgdx/libgdx

public CirclePlatform (int x, int y, float radius, float da) {
  platform = createCircle(BodyType.KinematicBody, radius, 1);
  platform.setTransform(x, y, 0);
  platform.getFixtureList().get(0).setUserData("p");
  platform.setAngularVelocity(da);
  platform.setUserData(this);
}

代码示例来源:origin: libgdx/libgdx

public MovingPlatform (float x, float y, float width, float height, float dx, float dy, float da, float maxDist) {
  platform = createBox(BodyType.KinematicBody, width, height, 1);
  pos.x = x;
  pos.y = y;
  dir.x = dx;
  dir.y = dy;
  this.maxDist = maxDist;
  platform.setTransform(pos, 0);
  platform.getFixtureList().get(0).setUserData("p");
  platform.setAngularVelocity(da);
  platform.setUserData(this);
}

代码示例来源:origin: libgdx/libgdx

/** Destroy a rigid body given a definition. No reference to the definition is retained. This function is locked during
 * callbacks.
 * @warning This automatically deletes all associated shapes and joints.
 * @warning This function is locked during callbacks. */
public void destroyBody (Body body) {
  Array<JointEdge> jointList = body.getJointList();
  while (jointList.size > 0)
    destroyJoint(body.getJointList().get(0).joint);
  jniDestroyBody(addr, body.addr);
  body.setUserData(null);
  this.bodies.remove(body.addr);
  Array<Fixture> fixtureList = body.getFixtureList();
  while(fixtureList.size > 0) {
    Fixture fixtureToDelete = fixtureList.removeIndex(0);
     this.fixtures.remove(fixtureToDelete.addr).setUserData(null);
     freeFixtures.free(fixtureToDelete);
   }
  
  freeBodies.free(body);
}

代码示例来源:origin: libgdx/libgdx

bd.position.set(xs[j] + x, 0.752f + 1.54f * i);
Body body = world.createBody(bd);
body.setUserData(n);

代码示例来源:origin: stackoverflow.com

//get rid of all bodies  (toBeDeleted is an Array<Body>)
public void sweepDeadBodies() {
  if(!world.isLocked()){ // KEY FACTOR
  Array<Body> wB = getBodies();   
    for (Body body : wB) {         
     if(body!=null && toBeDeleted.contains(body, false)) {
         body.setUserData(null);
         world.destroyBody(body);
         toBeDeleted.removeValue(body, false);
     }

    }
  }          
}

代码示例来源:origin: stackoverflow.com

public class Ball extends Sprite{

final FixtureDef ballFixtureDef = PhysicsFactory.createFixtureDef(1.0f, 0.0f, 0.0f, false, Main.CATEGORYBIT_BALL, Main.MASKBITS_BALL, (short)0);

Body body;
float velocityX, velocityY; 
int type;   

public Ball(float pX, float pY, TextureRegion pTextureRegion, PhysicsWorld pWorld, float velocityX, float velocityY, int type) 
{
  super(pX, pY, pTextureRegion);
  this.type = type;
  this.velocityX = velocityX;
  this.velocityY = velocityY;
  body = PhysicsFactory.createCircleBody(pWorld, this, BodyType.DynamicBody, ballFixtureDef);
  body.setUserData(Ball.this);
  pWorld.registerPhysicsConnector(new PhysicsConnector(this, body, true, true));
}
}

代码示例来源:origin: stackoverflow.com

final AnimatedSprite face;
 final Body body;
 final FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);
   face = new AnimatedSprite(CAMERA_WIDTH/2+200, CAMERA_HEIGHT/2, this.mCircleFaceTextureRegion, this.getVertexBufferObjectManager());
   face.setScale(3);
   body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef);
 this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, body, true, true));
 face.animate(new long[]{200,200}, 0, 1, true);
 face.setUserData(body);
 body.setUserData("player");
 this.mScene.registerTouchArea(face);
 this.mScene.attachChild(face);

代码示例来源:origin: dsaltares/libgdx-cookbook

@Override
public void beginContact(Contact contact) {
  // TODO Auto-generated method stub
  Fixture fixtureA = contact.getFixtureA();
  Fixture fixtureB = contact.getFixtureB();
  
  Body bodyA = fixtureA.getBody();
  Body bodyB = fixtureB.getBody();
  
  if(bodyA == pointerBody && bodyB != groundBody)
    bodyB.setUserData(true);
  else if(bodyB == pointerBody && bodyA != groundBody)
    bodyA.setUserData(true);
}

代码示例来源:origin: narfman0/GDXWorld

@Override public Body createFixture(World world, boolean overrideStatic) {
  BodyType type = overrideStatic ? BodyType.StaticBody : bodyType;
  Body body = PhysicsHelper.createCircle(world, radius, center, type, friction, restitution, density,
      getFilter().maskBits, getFilter().categoryBits, getFilter().groupIndex);
  body.setUserData(name);
  return body;
}

代码示例来源:origin: dsaltares/libgdx-cookbook

public Enemy(Texture texture, float x, float y) {
  this.texture = texture;
  
  enemyBody = createPolygon(BodyType.DynamicBody, x, y, 1f, 0f, 0.8f, ENEMY_WIDTH * 0.5f,
      ENEMY_HEIGHT * 0.5f, CATEGORY_ENEMY, MASK_ENEMY);
  
  //To identify the specific instance when collisions happen 
  enemyBody.setUserData(this);
  
  // Sensor shape to detect other bodies getting close to him
  CircleShape circle = new CircleShape();
  circle.setRadius(1f);
  FixtureDef sensorFD = new FixtureDef();
  sensorFD.isSensor = true;
  sensorFD.shape = circle;
  sensorFD.filter.categoryBits = CATEGORY_SENSOR;
  sensorFD.filter.maskBits = MASK_SENSOR;
  enemyBody.createFixture(sensorFD);
}

代码示例来源:origin: narfman0/GDXWorld

/**
 * @param overrideStatic use BodyType.StaticBody no matter what bodyType is set to
 */
public Body createFixture(World world, boolean overrideStatic){
  FixtureDef fd = new FixtureDef();
  fd.density = density;
  fd.friction = friction;
  fd.restitution = restitution;
  BodyType type = overrideStatic ? BodyType.StaticBody : bodyType;
  Body body = PhysicsHelper.createFixture(world, fd, type, vertices, new PolygonShape(), 
      getFilter().maskBits, getFilter().categoryBits, getFilter().groupIndex);
  if(body != null){
    body.setTransform(center, 0);
    body.setUserData(name);
  }
  return body;
}

代码示例来源:origin: danialgoodwin/dev

public static Body createGround(World world) {
  BodyDef bodyDef = new BodyDef();
  bodyDef.position.set(new Vector2(Constants.GROUND_X, Constants.GROUND_Y));
  Body body = world.createBody(bodyDef);
  PolygonShape shape = new PolygonShape();
  shape.setAsBox(Constants.GROUND_WIDTH / 2, Constants.GROUND_HEIGHT / 2);
  body.createFixture(shape, Constants.GROUND_DENSITY);
  body.setUserData(new GroundUserData(Constants.GROUND_WIDTH, Constants.GROUND_HEIGHT));
  shape.dispose();
  return body;
}

代码示例来源:origin: dsaltares/libgdx-cookbook

private Body createPolygon(int num, BodyType type, float x, float y, float d, float r, float f, float halfwidth, float halfheight) {
  BodyDef bodyDef = new BodyDef();
  bodyDef.type = type;
  bodyDef.position.set(x,y);
  bodyDef.angle=0;
  bodyDef.fixedRotation = true;
  Body square = world.createBody(bodyDef);
  square.setUserData("Box"+num);
  FixtureDef fixtureDef=new FixtureDef();
  fixtureDef.density=d;
  fixtureDef.restitution=r;
  fixtureDef.friction=f;
  fixtureDef.shape=new PolygonShape();
  ((PolygonShape) fixtureDef.shape).setAsBox(halfwidth, halfheight);
  square.createFixture(fixtureDef);
  fixtureDef.shape.dispose();
  return square;
}

代码示例来源:origin: dsaltares/libgdx-cookbook

private Body createPolygon(int num, BodyType type, float x, float y, float d, float r, float f, float halfwidth, float halfheight) {
  BodyDef bodyDef = new BodyDef();
  bodyDef.type = type;
  bodyDef.position.set(x,y);
  bodyDef.angle=0;
  bodyDef.fixedRotation = true;
  Body square = world.createBody(bodyDef);
  square.setUserData("Box"+num);
  FixtureDef fixtureDef=new FixtureDef();
  fixtureDef.density=d;
  fixtureDef.restitution=r;
  fixtureDef.friction=f;
  fixtureDef.shape=new PolygonShape();
  ((PolygonShape) fixtureDef.shape).setAsBox(halfwidth, halfheight);
  square.createFixture(fixtureDef);
  fixtureDef.shape.dispose();
  return square;
}

代码示例来源:origin: dsaltares/libgdx-cookbook

private void createBalloon() {
  float x = MathUtils.random(minWidth, maxWidth);
  float y = MathUtils.random(minHeight, maxHeight);
  // Instantiate the loader with the created JSON data
  BodyEditorLoader loader = new BodyEditorLoader(Gdx.files.internal("data/box2D/balloon.json"));
  // Create the balloon body definition and place it in within the world
  BodyDef bd = new BodyDef();
  bd.type = BodyType.DynamicBody;
  bd.position.set(x, y);
  // Create the balloon body
  Body balloonBody = world.createBody(bd);
  balloonBody.setUserData(false); // Set to true if it must be destroyed, false means active
  // Create balloon fixture
  loader.attachFixture(balloonBody, "balloon", balloonFD, BALLOON_WIDTH);
  
  balloons.add(balloonBody);
}

代码示例来源:origin: danialgoodwin/dev

public static Body createRunner(World world) {
  BodyDef bodyDef = new BodyDef();
  bodyDef.type = BodyDef.BodyType.DynamicBody;
  bodyDef.position.set(new Vector2(Constants.RUNNER_X, Constants.RUNNER_Y));
  PolygonShape shape = new PolygonShape();
  shape.setAsBox(Constants.RUNNER_WIDTH / 2, Constants.RUNNER_HEIGHT / 2);
  Body body = world.createBody(bodyDef);
  body.setGravityScale(Constants.RUNNER_GRAVITY_SCALE);
  body.createFixture(shape, Constants.RUNNER_DENSITY);
  body.resetMassData();
  body.setUserData(new RunnerUserData(Constants.RUNNER_WIDTH, Constants.RUNNER_HEIGHT));
  shape.dispose();
  return body;
}

代码示例来源:origin: com.badlogicgames.gdx/gdx-box2d

/** Destroy a rigid body given a definition. No reference to the definition is retained. This function is locked during
 * callbacks.
 * @warning This automatically deletes all associated shapes and joints.
 * @warning This function is locked during callbacks. */
public void destroyBody (Body body) {
  Array<JointEdge> jointList = body.getJointList();
  while (jointList.size > 0)
    destroyJoint(body.getJointList().get(0).joint);
  jniDestroyBody(addr, body.addr);
  body.setUserData(null);
  this.bodies.remove(body.addr);
  Array<Fixture> fixtureList = body.getFixtureList();
  while(fixtureList.size > 0) {
    Fixture fixtureToDelete = fixtureList.removeIndex(0);
     this.fixtures.remove(fixtureToDelete.addr).setUserData(null);
     freeFixtures.free(fixtureToDelete);
   }
  
  freeBodies.free(body);
}

代码示例来源:origin: dsaltares/libgdx-cookbook

private void createGround() {
  ChainShape chainShape = new ChainShape();
  chainShape.createLoop(new Vector2[] {
      new Vector2(.0f, .0f), 
      new Vector2(SCENE_WIDTH, .0f), 
      new Vector2(SCENE_WIDTH, 7f),
      new Vector2(SCENE_WIDTH-1f, 7f),
      new Vector2(SCENE_WIDTH-1f, 1.5f),
      new Vector2(1f, 1.5f),
      new Vector2(1f, 7f),
      new Vector2(0f, 7f),});
  BodyDef chainBodyDef = new BodyDef();
  chainBodyDef.type = BodyType.StaticBody;
  chainBodyDef.position.set(.0f, .0f);
  groundBody = world.createBody(chainBodyDef);
  FixtureDef groundFD = new FixtureDef();
  groundFD.shape = chainShape;
  groundFD.density = 0;
  groundBody.createFixture(groundFD);
  groundBody.setUserData("Ground Body");
  chainShape.dispose();
}

代码示例来源:origin: dsaltares/libgdx-cookbook

private void createGround() {
  ChainShape chainShape = new ChainShape();
  chainShape.createLoop(new Vector2[] {
      new Vector2(.0f, .0f), 
      new Vector2(SCENE_WIDTH, .0f), 
      new Vector2(SCENE_WIDTH, 7f),
      new Vector2(SCENE_WIDTH-1f, 7f),
      new Vector2(SCENE_WIDTH-1f, 1.5f),
      new Vector2(1f, 1.5f),
      new Vector2(1f, 7f),
      new Vector2(0f, 7f),});
  BodyDef chainBodyDef = new BodyDef();
  chainBodyDef.type = BodyType.StaticBody;
  chainBodyDef.position.set(.0f, .0f);
  groundBody = world.createBody(chainBodyDef);
  FixtureDef groundFD = new FixtureDef();
  groundFD.shape = chainShape;
  groundFD.density = 0;
  groundBody.createFixture(groundFD);
  groundBody.setUserData("Ground Body");
  chainShape.dispose();
}

代码示例来源:origin: danialgoodwin/dev

public static Body createEnemy(World world) {
  EnemyType enemyType = RandomUtils.getRandomEnemyType();
  BodyDef bodyDef = new BodyDef();
  bodyDef.type = BodyDef.BodyType.KinematicBody;
  bodyDef.position.set(new Vector2(enemyType.getX(), enemyType.getY()));
  PolygonShape shape = new PolygonShape();
  shape.setAsBox(enemyType.getWidth() / 2, enemyType.getHeight() / 2);
  Body body = world.createBody(bodyDef);
  body.createFixture(shape, enemyType.getDensity());
  body.resetMassData();
  EnemyUserData userData = new EnemyUserData(enemyType.getWidth(), enemyType.getHeight(), enemyType.getRegions());
  body.setUserData(userData);
  shape.dispose();
  return body;
}

相关文章