Beispiel #1
0
  void updateServerElements() {
    for (RigidBody rb : rigidBodies) {
      ServerElement element = rigidBodyToServerElement.get(rb);
      if (rb != null && rb.getMotionState() != null) {
        rb.getMotionState().getWorldTransform(tmpTrans);
        element.setTransform(tmpTrans);
      }
      if (!rb.isActive()) {
        rb.activate();
      }
      // log.debug(" "+element.getPosition());
    }

    for (Integer id : serverGameArea.getGameState().getDynamicElements().keySet()) {
      ServerElement elem = serverGameArea.getGameState().getDynamicElements().get(id);
      elem.getCollidees().clear();
    }

    Integer nbManifold = dynamicsWorld.getDispatcher().getNumManifolds();
    for (Integer i = 0; i < nbManifold; i++) {
      PersistentManifold pm = dynamicsWorld.getDispatcher().getManifoldByIndexInternal(i);
      RigidBody rb0 = (RigidBody) pm.getBody0();
      RigidBody rb1 = (RigidBody) pm.getBody1();

      ServerElement elem0 = rigidBodyToServerElement.get(rb0);
      ServerElement elem1 = rigidBodyToServerElement.get(rb1);

      if ((elem0 instanceof ServerBullet) || (elem1 instanceof ServerBullet)) {
        log.info("coll " + elem0 + " " + elem1);
      }

      elem0.addCollidee(elem1);
      elem1.addCollidee(elem0);
    }
  }
  public void updateChunks() {
    ArrayList<Chunk> chunks = CoreRegistry.get(WorldRenderer.class).getChunksInProximity();
    HashSet<RigidBody> newBodies = new HashSet<RigidBody>();

    for (int i = 0; i < 32 && i < chunks.size(); i++) {
      final Chunk chunk = chunks.get(i);

      if (chunk != null) {
        chunk.updateRigidBody();

        RigidBody c = chunk.getRigidBody();

        if (c != null) {
          newBodies.add(c);

          if (!_chunks.contains(c)) {
            _discreteDynamicsWorld.addRigidBody(c);
          }
        }
      }
    }

    for (RigidBody body : _chunks) {
      if (!newBodies.contains(body)) {
        _discreteDynamicsWorld.removeRigidBody(body);
      }
    }

    _chunks = newBodies;
  }
Beispiel #3
0
  void initPhysics() {
    // collision configuration contains default setup for memory, collision
    // setup. Advanced users can create their own configuration.
    CollisionConfiguration collisionConfiguration = new DefaultCollisionConfiguration();

    // use the default collision dispatcher. For parallel processing you
    // can use a diffent dispatcher (see Extras/BulletMultiThreaded)
    CollisionDispatcher dispatcher = new CollisionDispatcher(collisionConfiguration);

    // the maximum size of the collision world. Make sure objects stay
    // within these boundaries
    // Don't make the world AABB size too large, it will harm simulation
    // quality and performance
    Vector3f worldAabbMin = new Vector3f(-100, -100, -100);
    Vector3f worldAabbMax = new Vector3f(100, 100, 100);
    int maxProxies = 1024;
    AxisSweep3 overlappingPairCache = new AxisSweep3(worldAabbMin, worldAabbMax, maxProxies);
    // BroadphaseInterface overlappingPairCache = new SimpleBroadphase(
    //		maxProxies);

    // the default constraint solver. For parallel processing you can use a
    // different solver (see Extras/BulletMultiThreaded)
    SequentialImpulseConstraintSolver solver = new SequentialImpulseConstraintSolver();

    dynamicsWorld =
        new DiscreteDynamicsWorld(dispatcher, overlappingPairCache, solver, collisionConfiguration);

    dynamicsWorld.setGravity(new Vector3f(0, -5, 0));
  }
  private synchronized void addQueuedBodies() {
    while (!_insertionQueue.isEmpty()) {
      RigidBody body = _insertionQueue.poll();

      if (body instanceof BlockRigidBody) _blocks.add((BlockRigidBody) body);

      _discreteDynamicsWorld.addRigidBody(body);
    }
  }
Beispiel #5
0
 void addStaticServerElement(ServerElement elem) {
   RigidBody rb =
       createBody(
           0f, elem.getPosition(), getCollisionShape(elem.getSize()), 0f, elem.getOriantation());
   staticBodies.add(rb);
   // staticServerElements.add(elem);
   elementToRigidBody.put(elem, rb);
   rigidBodyToServerElement.put(rb, elem);
   dynamicsWorld.addRigidBody(rb);
 }
  private void removeTemporaryBlocks() {
    if (_blocks.size() > 0) {
      for (int i = _blocks.size() - 1; i >= 0; i--) {
        if (!_blocks.get(i)._temporary) continue;

        if (!_blocks.get(i).isActive() || _blocks.get(i).calcAgeInMs() > 10000) {
          _discreteDynamicsWorld.removeRigidBody(_blocks.get(i));
          _blocks.remove(i);
        }
      }
    }
  }
  public void update(float delta) {
    addQueuedBodies();

    try {
      _discreteDynamicsWorld.stepSimulation(delta, 3);
    } catch (Exception e) {
      _logger.log(
          Level.WARNING,
          "Somehow Bullet Physics managed to throw an exception again. Go along: " + e.toString());
    }

    updateChunks();
    removeTemporaryBlocks();
    checkForLootedBlocks();
  }
Beispiel #8
0
  public void removeServerElement(ServerElement element) {
    RigidBody rb = elementToRigidBody.get(element);
    if (rb == null) {
      log.error("element " + element + " alredy removed");
    } else {
      rigidBodyToServerElement.remove(rb);
      elementToRigidBody.remove(element);
      rigidBodies.remove(rb);
      // dynamicServerElements.remove(element.getId());
      dynamicsWorld.removeRigidBody(rb);

      if (element instanceof ServerPlayer) {
        ServerPlayer p = (ServerPlayer) element;
        players.remove(p);
      }
    }
  }
 public BulletPhysicsRenderer(WorldRenderer parent) {
   _broadphase = new DbvtBroadphase();
   _defaultCollisionConfiguration = new DefaultCollisionConfiguration();
   _dispatcher = new CollisionDispatcher(_defaultCollisionConfiguration);
   _sequentialImpulseConstraintSolver = new SequentialImpulseConstraintSolver();
   _discreteDynamicsWorld =
       new DiscreteDynamicsWorld(
           _dispatcher,
           _broadphase,
           _sequentialImpulseConstraintSolver,
           _defaultCollisionConfiguration);
   _discreteDynamicsWorld.setGravity(new Vector3f(0f, -10f, 0f));
   _parent = parent;
   _blockItemFactory =
       new BlockItemFactory(
           CoreRegistry.get(EntityManager.class), CoreRegistry.get(PrefabManager.class));
   _timer = CoreRegistry.get(Timer.class);
 }
  public void render() {

    FloatBuffer mBuffer = BufferUtils.createFloatBuffer(16);
    float[] mFloat = new float[16];

    GL11.glPushMatrix();

    Vector3d cameraPosition = _parent.getActiveCamera().getPosition();
    GL11.glTranslated(-cameraPosition.x, -cameraPosition.y, -cameraPosition.z);

    List<CollisionObject> collisionObjects = _discreteDynamicsWorld.getCollisionObjectArray();

    for (CollisionObject co : collisionObjects) {
      if (co.getClass().equals(BlockRigidBody.class)) {
        BlockRigidBody br = (BlockRigidBody) co;
        Block block = BlockManager.getInstance().getBlock(br.getType());

        Transform t = new Transform();
        br.getMotionState().getWorldTransform(t);

        t.getOpenGLMatrix(mFloat);
        mBuffer.put(mFloat);
        mBuffer.flip();

        GL11.glPushMatrix();
        GL11.glMultMatrix(mBuffer);

        if (br.getCollisionShape() == _blockShapeHalf) GL11.glScalef(0.5f, 0.5f, 0.5f);
        else if (br.getCollisionShape() == _blockShapeQuarter) GL11.glScalef(0.25f, 0.25f, 0.25f);

        block.renderWithLightValue(_parent.getRenderingLightValueAt(new Vector3d(t.origin)));

        GL11.glPopMatrix();
      }
    }

    GL11.glPopMatrix();
  }
Beispiel #11
0
  // Vector3f turnVector = new Vector3f(0f,2f,0f);
  // Vector3f turnInvVector = new Vector3f(0f,-2f,0f);
  public synchronized void step(Integer milliseconds) {

    Boolean commanded;
    for (ServerPlayer p : players) {
      commanded = false;
      if (p.getAccel() != 0f) {
        setServerElementAccel(p, p.getAccel());
        p.setAccel(0f);
        commanded = true;
      }

      if (p.getStraf() != 0f) {
        setServerElementStraf(p, p.getStraf());
        p.setStraf(0f);
        commanded = true;
      }

      if (p.getTurn() != 0f) {
        setElementRotation(p, p.getTurn());
        p.setTurn(0f);
        commanded = true;
      }

      if (commanded) {

      } else {
        setServerElementForce(p);
      }
    }

    updateRigidBodys();

    // dynamicsWorld.stepSimulation(1.f / 30.f, 10);
    dynamicsWorld.stepSimulation(milliseconds, 2);

    updateServerElements();
  }
Beispiel #12
0
  public void addDynamicServerElement(ServerElement elem, Float initialVelocity) {
    RigidBody rb =
        createBody(
            elem.getMass(),
            elem.getPosition(),
            getCollisionShape(elem.getSize()),
            initialVelocity,
            elem.getOriantation());
    dynamicsWorld.addRigidBody(rb);

    rigidBodyToServerElement.put(rb, elem);
    elementToRigidBody.put(elem, rb);
    rigidBodies.add(rb);
    // dynamicServerElements.put(elem.getId(),elem);
    if (elem instanceof ServerPlayer) {
      ServerPlayer p = (ServerPlayer) elem;
      players.add(p);
      rb.setAngularFactor(0.3f);
    }

    if (elem instanceof ServerBullet) {
      rb.setGravity(new Vector3f(0, -0.2f, 0));
    }
  }
  private void checkForLootedBlocks() {
    LocalPlayer player = CoreRegistry.get(LocalPlayer.class);

    // to send blocks to minions, some needed classes
    EntityRef playerent = player.getEntity();
    LocalPlayerComponent localPlayerComp = playerent.getComponent(LocalPlayerComponent.class);
    MinionBarComponent inventory = null;
    EntityRef closestminion = null;
    if (localPlayerComp.minionMode) {
      inventory = playerent.getComponent(MinionBarComponent.class);
    }

    for (int i = _blocks.size() - 1; i >= 0; i--) {
      BlockRigidBody b = _blocks.get(i);

      if (b._temporary) continue;

      float closestDist = 99999;
      if (inventory != null) {
        // check for the closest minion
        Iterator<EntityRef> it = inventory.MinionSlots.iterator();
        while (it.hasNext()) {
          EntityRef minion = it.next();
          if (b.distanceToEntity(minion) < closestDist) {
            closestDist = b.distanceToEntity(minion);
            closestminion = minion;
          }
        }
      }

      if (localPlayerComp.minionMode) {
        if (closestDist < 8 && !b._picked) {
          b._picked = true;
        }
      } else {
        closestDist = b.distanceToPlayer();
        // Check if the block is close enough to the player
        if (b.distanceToPlayer() < 8.0f && !b._picked) {
          // Mark it as picked and remove it from the simulation
          b._picked = true;
        }
      }

      // Block was marked as being picked
      if (b._picked && closestDist < 32.0f) {
        // Animate the movement in direction of the player
        if (closestDist > 1.0) {
          Transform t = new Transform();
          b.getMotionState().getWorldTransform(t);

          Matrix4f tMatrix = new Matrix4f();
          t.getMatrix(tMatrix);

          Vector3f blockPlayer = new Vector3f();
          tMatrix.get(blockPlayer);
          if (localPlayerComp.minionMode && closestminion != null) {
            LocationComponent minionloc = closestminion.getComponent(LocationComponent.class);
            if (minionloc != null) blockPlayer.sub(minionloc.getWorldPosition());
            else blockPlayer.sub(new Vector3f());
          } else {
            blockPlayer.sub(new Vector3f(player.getPosition()));
          }
          blockPlayer.normalize();
          blockPlayer.scale(-16000f);

          b.applyCentralImpulse(blockPlayer);
        } else {
          // TODO: Handle full inventories
          // TODO: Loot blocks should be entities
          // Block was looted (and reached the player)
          Block block = BlockManager.getInstance().getBlock(b.getType());
          EntityRef blockItem = _blockItemFactory.newInstance(block.getBlockFamily());

          if (localPlayerComp.minionMode) {
            if (closestminion != null) {
              closestminion.send(new ReceiveItemEvent(blockItem));
            }
          } else {
            playerent.send(new ReceiveItemEvent(blockItem));
          }
          ItemComponent itemComp = blockItem.getComponent(ItemComponent.class);
          if (itemComp != null && !itemComp.container.exists()) {
            blockItem.destroy();
          }
          AudioManager.play(new AssetUri(AssetType.SOUND, "engine:Loot"));

          _blocks.remove(i);
          _discreteDynamicsWorld.removeRigidBody(b);
        }
      }
    }
  }