Java Code Examples for com.badlogic.gdx.math.MathUtils#isEqual()

The following examples show how to use com.badlogic.gdx.math.MathUtils#isEqual() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MDQuaternion.java    From Beginner-Level-Android-Studio-Apps with GNU General Public License v3.0 6 votes vote down vote up
public void nor () {
    float w = q[0];
    float x = q[1];
    float y = q[2];
    float z = q[3];

    float len = x * x + y * y + z * z + w * w;
    if (len != 0.f && !MathUtils.isEqual(len, 1f)) {
        len = (float) Math.sqrt(len);
        w /= len;
        x /= len;
        y /= len;
        z /= len;
    }
    set(w, x, y, z);
}
 
Example 2
Source File: TenPatchWidget.java    From skin-composer with MIT License 6 votes vote down vote up
private void drawTiles(Batch batch) {
    if (MathUtils.isEqual(1f, widget.getZoomScale())) {
        style.gridLight.draw(batch, getX(), getY(), getWidth(), getHeight());
    } else {
        var even = (int) (widget.position.y / widget.getZoomScale()) % 2 == 0;
        for (var y = -widget.getZoomScale() + widget.position.y % widget.getZoomScale(); y < getHeight(); y += widget.getZoomScale()) {
            var drawLight = (int) (widget.position.x / widget.getZoomScale()) % 2 == 0 ? even : !even;
            for (var x = -widget.getZoomScale() + widget.position.x % widget.getZoomScale(); x < getWidth(); x += widget.getZoomScale()) {
                var drawable = drawLight ? style.gridLight : style.gridDark;
                drawable.draw(batch, x + getX(), y + getY(), widget.getZoomScale(), widget.getZoomScale());
                drawLight = !drawLight;
            }
            even = !even;
        }
    }
}
 
Example 3
Source File: MDQuaternion.java    From MD360Player4Android with Apache License 2.0 6 votes vote down vote up
public void nor () {
    float w = q[0];
    float x = q[1];
    float y = q[2];
    float z = q[3];

    float len = x * x + y * y + z * z + w * w;
    if (len != 0.f && !MathUtils.isEqual(len, 1f)) {
        len = (float)Math.sqrt(len);
        w /= len;
        x /= len;
        y /= len;
        z /= len;
    }
    set(w, x, y, z);
}
 
Example 4
Source File: ReliablePacketController.java    From riiablo with Apache License 2.0 6 votes vote down vote up
private void updatePacketLoss() {
  int baseSequence = (sentPackets.getSequence() - config.sentPacketBufferSize + 1 + Packet.USHORT_MAX_VALUE) & Packet.USHORT_MAX_VALUE;

  int numDropped = 0;
  int numSamples = config.sentPacketBufferSize / 2;
  for (int i = 0; i < numSamples; i++) {
    int sequence = (baseSequence + i) & Packet.USHORT_MAX_VALUE;
    SentPacketData sentPacketData = sentPackets.find(sequence);
    if (sentPacketData != null && !sentPacketData.acked) numDropped++;
  }

  float packetLoss = numDropped / (float) numSamples;
  if (MathUtils.isEqual(this.packetLoss, packetLoss, TOLERANCE)) {
    this.packetLoss += (packetLoss - this.packetLoss) * config.packetLossSmoothingFactor;
  } else {
    this.packetLoss = packetLoss;
  }
}
 
Example 5
Source File: ReliablePacketController.java    From riiablo with Apache License 2.0 6 votes vote down vote up
private void updateSentBandwidth() {
  int baseSequence = (sentPackets.getSequence() - config.sentPacketBufferSize + 1 + Packet.USHORT_MAX_VALUE) & Packet.USHORT_MAX_VALUE;

  int bytesSent = 0;
  float startTime = Float.MAX_VALUE;
  float finishTime = 0f;
  int numSamples = config.sentPacketBufferSize / 2;
  for (int i = 0; i < numSamples; i++) {
    int sequence = (baseSequence + i) & Packet.USHORT_MAX_VALUE;
    SentPacketData sentPacketData = sentPackets.find(sequence);
    if (sentPacketData == null) continue;
    bytesSent += sentPacketData.packetSize;
    startTime = Math.min(startTime, sentPacketData.time);
    finishTime = Math.max(finishTime, sentPacketData.time);
  }

  if (startTime != Float.MAX_VALUE && finishTime != 0f) {
    float sentBandwidth = bytesSent / (finishTime - startTime) * 8f / 1000f;
    if (MathUtils.isEqual(this.sentBandwidth, sentBandwidth, TOLERANCE)) {
      this.sentBandwidth += (sentBandwidth - this.sentBandwidth) * config.bandwidthSmoothingFactor;
    } else {
      this.sentBandwidth = sentBandwidth;
    }
  }
}
 
Example 6
Source File: ReliablePacketController.java    From riiablo with Apache License 2.0 6 votes vote down vote up
private void updateAckedBandwidth() {
  int baseSequence = (sentPackets.getSequence() - config.sentPacketBufferSize + 1 + Packet.USHORT_MAX_VALUE) & Packet.USHORT_MAX_VALUE;

  int bytesSent = 0;
  float startTime = Float.MAX_VALUE;
  float finishTime = 0f;
  int numSamples = config.sentPacketBufferSize / 2;
  for (int i = 0; i < numSamples; i++) {
    int sequence = (baseSequence + i) & Packet.USHORT_MAX_VALUE;
    SentPacketData sentPacketData = sentPackets.find(sequence);
    if (sentPacketData == null || !sentPacketData.acked) continue;
    bytesSent += sentPacketData.packetSize;
    startTime = Math.min(startTime, sentPacketData.time);
    finishTime = Math.max(finishTime, sentPacketData.time);
  }

  if (startTime != Float.MAX_VALUE && finishTime != 0f) {
    float ackedBandwidth = bytesSent / (finishTime - startTime) * 8f / 1000f;
    if (MathUtils.isEqual(this.ackedBandwidth, ackedBandwidth, TOLERANCE)) {
      this.ackedBandwidth += (ackedBandwidth - this.ackedBandwidth) * config.bandwidthSmoothingFactor;
    } else {
      this.ackedBandwidth = ackedBandwidth;
    }
  }
}
 
Example 7
Source File: Utils.java    From skin-composer with MIT License 5 votes vote down vote up
public static boolean isEqual(float... values) {
    if (values.length > 0) {
        float first = values[0];
        for (int i = 1; i < values.length; i++) {
            float value = values[i];
            if (!MathUtils.isEqual(first, value)) return false;
        }
        return true;
    } else {
        return false;
    }
}
 
Example 8
Source File: ReliablePacketController.java    From riiablo with Apache License 2.0 5 votes vote down vote up
private void updateReceivedBandwidth() {
  synchronized (receivedPackets) {
    int baseSequence = (receivedPackets.getSequence() - config.receivedPacketBufferSize + 1 + Packet.USHORT_MAX_VALUE) & Packet.USHORT_MAX_VALUE;

    int bytesReceived = 0;
    float startTime = Float.MAX_VALUE;
    float finishTime = 0f;
    int numSamples = config.receivedPacketBufferSize / 2;
    for (int i = 0; i < numSamples; i++) {
      int sequence = (baseSequence + i) & Packet.USHORT_MAX_VALUE;
      ReceivedPacketData receivedPacketData = receivedPackets.find(sequence);
      if (receivedPacketData == null) continue;
      bytesReceived += receivedPacketData.packetSize;
      startTime = Math.min(startTime, receivedPacketData.time);
      finishTime = Math.max(finishTime, receivedPacketData.time);
    }

    if (startTime != Float.MAX_VALUE && finishTime != 0f) {
      float receivedBandwidth = bytesReceived / (finishTime - startTime) * 8f / 1000f;
      if (MathUtils.isEqual(this.receivedBandwidth, receivedBandwidth, TOLERANCE)) {
        this.receivedBandwidth += (receivedBandwidth - this.receivedBandwidth) * config.bandwidthSmoothingFactor;
      } else {
        this.receivedBandwidth = receivedBandwidth;
      }
    }
  }
}
 
Example 9
Source File: SystemProfilerGUI.java    From riiablo with Apache License 2.0 5 votes vote down vote up
public void update() {
  // we don't want to update if the change wont affect the representation
  if (!MathUtils.isEqual(lastMax, profiler.getMax(), PRECISION)) {
    lastMax = profiler.getMax();
    max.setText(timingToString(lastMax));
  }
  if (!MathUtils.isEqual(lastLocalMax, profiler.getLocalMax(), PRECISION)) {
    lastLocalMax = profiler.getLocalMax();
    localMax.setText(timingToString(lastLocalMax));
  }
  if (!MathUtils.isEqual(lastAvg, profiler.getMovingAvg(), PRECISION)) {
    lastAvg = profiler.getMovingAvg();
    avg.setText(timingToString(lastAvg));
  }
}
 
Example 10
Source File: ReliableMessageChannel.java    From riiablo with Apache License 2.0 4 votes vote down vote up
private void processSendBuffer(int channelId, DatagramChannel ch) {
//    int numUnacked = 0;
//    for (int seq = oldestUnacked; ReliableUtils.sequenceLessThan(seq, sequence);  seq = (seq + 1) & Packet.USHORT_MAX_VALUE) {
//      numUnacked++;
//    }

    for (int seq = oldestUnacked; ReliableUtils.sequenceLessThan(seq, sequence); seq = (seq + 1) & Packet.USHORT_MAX_VALUE) {
      // never send message ID >= (oldestUnacked + bufferSize)
      if (seq >= (oldestUnacked + 256)) break;

      // for any message that hasn't been sent in the last 0.1 seconds and fits in the available
      // space of our message packer, add it
      BufferedPacket packet = sendBuffer.find(seq);
      if (packet != null && !packet.writeLock) {
        if (MathUtils.isEqual(time, packet.time, 0.1f)) continue;
        boolean packetFits = false;
        int packetSize = packetBuffer.readableBytes() + packet.bb.readableBytes();
        if (packet.bb.readableBytes() < config.fragmentThreshold) {
          packetFits = packetSize <= (config.fragmentThreshold - Packet.MAX_PACKET_HEADER_SIZE);
        } else {
          packetFits = packetSize <= (config.maxPacketSize - Packet.FRAGMENT_HEADER_SIZE - Packet.MAX_PACKET_HEADER_SIZE);
        }

        // if the packet won't fit, flush the message packet
        if (!packetFits) {
          flushPacketBuffer(channelId, ch);
        }

        packet.time = time;
        packetBuffer.writeBytes(packet.bb);
        outgoingMessageIds.add(seq);
        lastMessageSend = time;
      }
    }

    // if it has been 0.1 seconds since the last time we sent a message, send an empty message
    if (time - lastMessageSend >= 0.1f) {
      packetController.sendAck(channelId, ch);
      lastMessageSend = time;
    }

    // flush and remaining messages in the packet buffer
    flushPacketBuffer(channelId, ch);
  }
 
Example 11
Source File: ReliablePacketController.java    From riiablo with Apache License 2.0 4 votes vote down vote up
public void onPacketReceived(ChannelHandlerContext ctx, DatagramPacket packet) {
  if (DEBUG_RECEIVE) Log.debug(TAG, "onPacketReceived " + packet);

  final ByteBuf bb = packet.content();
  final int packetSize = bb.readableBytes();
  if (packetSize > config.maxPacketSize) {
    Log.error(TAG, "packet is too large to receive (%d bytes), max packet size is %d bytes", packetSize, config.maxPacketSize);
    ReliableEndpoint.stats.NUM_PACKETS_TOO_LARGE_TO_RECEIVE++;
    return;
  }

  final byte flags = Packet.getFlags(bb);
  if (!Packet.isFragmented(flags)) {
    // regular packet

    ReliableEndpoint.stats.NUM_PACKETS_RECEIVED++;

    Packet.HeaderData headerData = null;
    try {
      headerData = Packet.obtainData();
      int headerSize = Packet.readPacketHeader(config, bb, headerData);
      if (headerSize == -1) {
        Log.error(TAG, "ignoring invalid packet. could not read packet header");
        ReliableEndpoint.stats.NUM_PACKETS_INVALID++;
        return;
      }

      final boolean isStale;
      final int sequence = headerData.sequence;
      synchronized (receivedPackets) {
        isStale = !receivedPackets.testInsert(sequence);
      }

      if (DEBUG_RECEIVE) Log.debug(TAG, "packet reported sequence as %d", sequence);
      final boolean isAck = Packet.isAck(flags);
      if (!isStale && !isAck) {
        if (DEBUG_RECEIVE) Log.debug(TAG, "processing packet %d", sequence);
        ByteBuf slice = bb.readSlice(bb.readableBytes());
        channel.onPacketProcessed(ctx, packet.sender(), sequence, slice);
        synchronized (receivedPackets) {
          ReceivedPacketData receivedPacketData = receivedPackets.insert(sequence);
          receivedPacketData.time = time;
          receivedPacketData.packetSize =  packetSize;
        }
      }

      if (!isStale || isAck) {
        final int ack = headerData.ack;
        for (int i = 0, ackBits = headerData.ackBits; i < Integer.SIZE && ackBits != 0; i++, ackBits >>>= 1) {
          if ((ackBits & 1) != 0) {
            int ackSequence = (ack - i) & Packet.USHORT_MAX_VALUE;
            SentPacketData sentPacketData = sentPackets.find(ackSequence);
            if (sentPacketData != null && !sentPacketData.acked) {
              if (DEBUG_RECEIVE) Log.debug(TAG, "acked packet %d", ackSequence);
              ReliableEndpoint.stats.NUM_PACKETS_ACKED++;
              sentPacketData.acked = true;
              channel.onAckProcessed(ctx, packet.sender(), ackSequence);

              float rtt = (time - sentPacketData.time) * 1000f;
              if ((this.rtt == 0.0f && rtt > 0.0f) || MathUtils.isEqual(this.rtt, rtt, TOLERANCE)) {
                this.rtt = rtt;
              } else {
                this.rtt += (rtt - this.rtt) * config.rttSmoothingFactor;
              }
            }
          }
        }
      }

      if (isStale) {
        Log.error(TAG, "ignoring stale packet %d", sequence);
        ReliableEndpoint.stats.NUM_PACKETS_STALE++;
        return;
      }
    } finally {
      if (headerData != null) headerData.free();
    }
  } else {
    // fragmented packet

    throw new UnsupportedOperationException();
  }
}
 
Example 12
Source File: Pathfinder.java    From riiablo with Apache License 2.0 4 votes vote down vote up
@Override
protected void process(int entityId) {
  Vector2 position0 = mPosition.get(entityId).position;
  tmpVec2.set(position0);
  Pathfind pathfind = mPathfind.get(entityId);
  Vector2 target = pathfind.target;
  Iterator<Vector2> targets = pathfind.targets;
  if (target.isZero()) return;
  if (tmpVec2.epsilonEquals(target, 0.1f)) { // TODO: tune this appropriately
    if (!targets.hasNext()) {
      findPath(entityId, null);
      return;
    }
  }

  Velocity velocity = mVelocity.get(entityId);
  float speed    = (mRunning.has(entityId) ? velocity.runSpeed : velocity.walkSpeed);
  float distance = speed * world.delta;
  float traveled = 0;
  while (traveled < distance) {
    float targetLen = tmpVec2.dst(target);
    float part = Math.min(distance - traveled, targetLen);
    if (part == 0) break;
    tmpVec2.lerp(target, part / targetLen);
    traveled += part;
    if (MathUtils.isEqual(part, targetLen, 0.1f)) {
      if (targets.hasNext()) {
        target.set(targets.next());
      } else {
        break;
      }
    }
  }

  /**
   * FIXME: there is a lot of jitter here in the direction for shorter movements because of
   *        repathing every frame-- need to create some kind of target component which is a target
   *        entity or target point and if it's down to the last remaining waypoint, set angle to
   *        the actual point or entity.
   */
  tmpVec2.sub(position0);
  mAngle.get(entityId).target.set(tmpVec2).nor();

  velocity.velocity.set(tmpVec2).setLength(speed);
}