【问题标题】:Libgdx Make Lights Ignore BodiesLibgdx 使灯光忽略实体
【发布时间】:2013-08-31 16:13:40
【问题描述】:

我正在开发一个使用 Box2d 和 Box2d Lights 的 libgdx 游戏。

我的问题是如何让 Bodys / Bodydefs 忽略来自 Box2d Light 的光源而不投射阴影?

谢谢。

  PolygonShape groundBox = new PolygonShape();
  groundBox.setAsBox(2f, 2f);

  FixtureDef gridBox = new FixtureDef();
  gridBox.shape = groundBox;
  gridBox.density = 1.0f;

  gridBox.filter.groupIndex = GRID_GROUP;
  gridBox.filter.categoryBits = GRID_CAT;
  gridBox.filter.maskBits = GRID_MASK;

  bodyDefs[i][j] = new BodyDef();       
  bodyDefs[i][j].position.set(j*factor + factor, i*factor + factor);
  bodyDefs[i][j].fixedRotation = true;
  bodyDefs[i][j].type = BodyDef.BodyType.DynamicBody;

  bodies[i][j] = world.createBody(bodyDefs[i][j]);
  bodies[i][j].createFixture(gridBox);

  handler = new RayHandler(world);
  handler.setCombinedMatrix(camera.combined);
  Filter filter = new Filter();
  filter.groupIndex = GRID_GROUP; // GRID_GROUP = 1
  filter.categoryBits = GRID_CAT; // GRID_CAT = 0x0001;
  filter.maskBits = GRID_MASK;    // GRID_MASK = 1;

  new PointLight(handler, 1000, Color.RED, 2000, 0, height);

  PointLight.setContactFilter(filter);

【问题讨论】:

  • 您是否成功地做到了这一点?我有同样的问题。你能分享你的解决方案或提示吗?

标签: java box2d libgdx


【解决方案1】:

您可以使用以下方法:

cone.setContactFilter(categoryBits, groupIndex, ma​​skBits);

其中cone 是ConeLight 类型的对象。现在这盏灯将忽略那些拥有 指定 categoryBits、groupIndex 和 maskBits。

你可以通过body的fixture来设置categoryBits、groupIndex和maskBits 以下方式-

fixture.filter.categoryBits = 你的价值;

(categoryBits : 碰撞类别位。通常你只需设置一位。)

fixture.filter.groupIndex = 你的价值;

( groupIndex: 碰撞组允许某组物体永不碰撞(负数)或总是碰撞(正数)。零表示 没有碰撞组。非零组过滤总是胜过掩码位。)

fixture.filter.maskBits = 你的价值

(maskBits:碰撞掩码位。这说明了此形状将接受碰撞的类别。)

如果您希望某些机构忽略所有灯光。那么您可以使用以下内容 灯光.setContactFilter(categoryBits, groupIndex, maskBits)

【讨论】:

  • 我认为这是正确的方法,但我在实现它时遇到了问题。我已经更新了我的问题,以帮助清除空气。
【解决方案2】:

我相信接触过滤器只能使用 Box2D Lights 应用于 ALL LIGHTS。我尝试使用 aug13 引用的 setContactFilter 方法设置它们,但这是从 Light 继承的静态方法,不能从对象调用。我不知道它是否可以在过去的某个时候。

来自文档:

public static void setContactFilter(short categoryBits,
                    short groupIndex,
                    short maskBits)

使用给定参数为所有灯创建新的联系人过滤器

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-30
    • 1970-01-01
    • 2016-03-26
    • 1970-01-01
    • 2014-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多