<commit_before>var Box = require("./math").box
var math = require("./math")
var Vector3 = math.vec3
var Plane = math.plane
var Triangle = math.triangle

var defaultSphereShape = new Vector3(1, 1, 1)

// vel is unnormalized
function getSweptCollision(spherePos, vel, tris, sphereShape, stick) {
  if (vel.lengthSq() < Number.EPSILON) return { position: spherePos, collision: false }
  sphereShape = sphereShape instanceof Vector3 ? sphereShape : defaultSphereShape

  var newPos = new Vector3().copy(spherePos).divide(sphereShape)
    , newVel = new Vector3().copy(vel).divide(sphereShape)
    , touchPoint = new Vector3(0, 0, 0)
    , touchSpherePoint = new Vector3(0, 0, 0)
    , temp = new Vector3()
    , slidePlane = new Plane()
    , endPos = new Vector3()
    , maxIterations = 10
    , epsilon = Number.EPSILON * 2
    , collision = false
    , hit

  for (var i = 0; i < maxIterations; i++) {
    endPos.addVectors(newPos, newVel)

    var info = getCollision(newPos, newVel, tris, sphereShape)

    if (!isFinite(info.t)) {
      hit = { position: endPos, collision: collision }
      break
    }

    collision = true
    touchPoint.copy(info.collisionPoint)
    // adding this check seems to fix some of the glitches
    if (info.t > epsilon)
      touchSpherePoint.addVectors(newPos, temp.copy(newVel).multiplyScalar(info.t))
    else
      touchSpherePoint.copy(newPos)

    var slideNormal = new Vector3().subVectors(touchSpherePoint, touchPoint).normalize()
    slidePlane.setFromNormalAndCoplanarPoint(slideNormal, touchPoint)
    if (stick) {
      hit = {
        position: newPos.copy(touchSpherePoint).add(slideNormal.multiplyScalar(epsilon)),
        collision: true
      }
      break
    }
    // Projected end point
    var endTouchPoint = new Vector3().addVectors(endPos, new Vector3().copy(slidePlane.normal).multiplyScalar(-slidePlane.distanceToPoint(endPos)))

    // Can modify
    newPos.copy(touchSpherePoint).add(slideNormal.multiplyScalar(epsilon))
    newVel.subVectors(endTouchPoint, touchPoint)
  }

  if (!hit) {
    hit = {
      position: touchSpherePoint.addVectors(newPos, newVel),
      collision: collision
    }
  }
<commit_msg>add a little more epsilon for floating point inaccuracies<commit_after>var Box = require("./math").box
var math = require("./math")
var Vector3 = math.vec3
var Plane = math.plane
var Triangle = math.triangle

var defaultSphereShape = new Vector3(1, 1, 1)

// vel is unnormalized
function getSweptCollision(spherePos, vel, tris, sphereShape, stick) {
  if (vel.lengthSq() < Number.EPSILON) return { position: spherePos, collision: false }
  sphereShape = sphereShape instanceof Vector3 ? sphereShape : defaultSphereShape

  var newPos = new Vector3().copy(spherePos).divide(sphereShape)
    , newVel = new Vector3().copy(vel).divide(sphereShape)
    , touchPoint = new Vector3(0, 0, 0)
    , touchSpherePoint = new Vector3(0, 0, 0)
    , temp = new Vector3()
    , slidePlane = new Plane()
    , endPos = new Vector3()
    , maxIterations = 10
    , epsilon = Number.EPSILON * 2
    , collision = false
    , hit

  for (var i = 0; i < maxIterations; i++) {
    endPos.addVectors(newPos, newVel)

    var info = getCollision(newPos, newVel, tris, sphereShape)

    if (!isFinite(info.t)) {
      hit = { position: endPos, collision: collision }
      break
    }

    collision = true
    touchPoint.copy(info.collisionPoint)
    // adding this check seems to fix some of the glitches
    if (info.t > epsilon)
      touchSpherePoint.addVectors(newPos, temp.copy(newVel).multiplyScalar(info.t))
    else
      touchSpherePoint.copy(newPos)

    var slideNormal = new Vector3().subVectors(touchSpherePoint, touchPoint).normalize()
    slidePlane.setFromNormalAndCoplanarPoint(slideNormal, touchPoint)
    if (stick) {
      hit = {
        position: newPos.copy(touchSpherePoint).add(slideNormal.multiplyScalar(epsilon)),
        collision: true
      }
      break
    }
    // Projected end point
    var endTouchPoint = new Vector3().addVectors(endPos, new Vector3().copy(slidePlane.normal).multiplyScalar(-slidePlane.distanceToPoint(endPos)))

    // Can modify
    newPos.copy(touchSpherePoint).add(slideNormal.multiplyScalar(epsilon*2))
    newVel.subVectors(endTouchPoint, touchPoint)
  }

  if (!hit) {
    hit = {
      position: touchSpherePoint.addVectors(newPos, newVel),
      collision: collision
    }
  }

{'fraction_non_alphanumeric': 0.11361566484517305, 'fraction_numerical': 0.012067395264116576, 'mean_word_length': 3.496417604912999, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7355304', 'n_tokens_mistral': 1466, 'n_tokens_neox': 1424, 'n_words': 324}