<commit_before>import _ from 'underscore';
import b2 from 'box2d';
import BallBody from 'shared/bodies/ball';
import config from 'shared/config';

var BallMesh = config.node ? null : require('client/meshes/ball');
var THREE = config.node ? null : require('three');

var UP = config.node ? null : new THREE.Vector3(0, 0, 1);

export var preStep = function (user) {
  var acceleration = user.acceleration;
  var velocity = user.body.GetLinearVelocity();
  var speed = velocity.Length();
  var nextVelocity = new b2.b2Vec2(
    velocity.get_x() + (acceleration.get_x() * config.game.acceleration),
    velocity.get_y() + (acceleration.get_y() * config.game.acceleration)
  );
  var nextSpeed = nextVelocity.Length();
  b2.destroy(nextVelocity);
  var maxSpeed = Math.max(config.game.maxSpeed, speed);
  var power = Math.min(maxSpeed - nextSpeed, config.game.acceleration);
  var force = new b2.b2Vec2(
    acceleration.get_x() * power,
    acceleration.get_y() * power
  );
  user.body.ApplyLinearImpulse(force);
  b2.destroy(force);
};

export var updateMesh = function (user) {
  var position = user.body.GetPosition();
  var x = position.get_x();
  var y = position.get_y();
  var mesh = user.mesh;
  if (!mesh.prev) mesh.prev = new THREE.Vector3();
  mesh.prev.copy(mesh.position);
  mesh.position.set(x, y, mesh.position.z);
  var v3 = new THREE.Vector3(mesh.prev.x - x, mesh.prev.y - y, 0);
  var theta = v3.length() / config.game.ballRadius;
  var axis = v3.cross(UP).normalize();
  mesh.matrix =
    (new THREE.Matrix4()).makeRotationAxis(axis, theta).multiply(mesh.matrix);
  mesh.rotation.copy((new THREE.Euler()).setFromRotationMatrix(mesh.matrix));
};

export var create = function (options) {
  options = _.extend({x: 8, y: 8}, options);
  return {
    type: 'user',
    id: options.id,
    game: options.game,
    body: BallBody.create(options),
    mesh: config.node ? null : BallMesh.create(options),
    acceleration: new b2.b2Vec2(0, 0),
    lastBroadcast: 0,
    needsBroadcast: 0
  };
};

export var destroy = function (user) {
  b2.destroy(user.acceleration);
  user.game.world.destroy(user.body);
  if (!config.node) user.game.scene.remove(ball.mesh);
};<commit_msg>No need to persist prev<commit_after>import _ from 'underscore';
import b2 from 'box2d';
import BallBody from 'shared/bodies/ball';
import config from 'shared/config';

var BallMesh = config.node ? null : require('client/meshes/ball');
var THREE = config.node ? null : require('three');

var UP = config.node ? null : new THREE.Vector3(0, 0, 1);

export var preStep = function (user) {
  var acceleration = user.acceleration;
  var velocity = user.body.GetLinearVelocity();
  var speed = velocity.Length();
  var nextVelocity = new b2.b2Vec2(
    velocity.get_x() + (acceleration.get_x() * config.game.acceleration),
    velocity.get_y() + (acceleration.get_y() * config.game.acceleration)
  );
  var nextSpeed = nextVelocity.Length();
  b2.destroy(nextVelocity);
  var maxSpeed = Math.max(config.game.maxSpeed, speed);
  var power = Math.min(maxSpeed - nextSpeed, config.game.acceleration);
  var force = new b2.b2Vec2(
    acceleration.get_x() * power,
    acceleration.get_y() * power
  );
  user.body.ApplyLinearImpulse(force);
  b2.destroy(force);
};

export var updateMesh = function (user) {
  var position = user.body.GetPosition();
  var x = position.get_x();
  var y = position.get_y();
  var mesh = user.mesh;
  var delta = mesh.position.clone();
  mesh.position.set(x, y, mesh.position.z);
  delta.sub(mesh.position);
  var theta = delta.length() / config.game.ballRadius;
  var axis = delta.cross(UP).normalize();
  mesh.matrix =
    (new THREE.Matrix4()).makeRotationAxis(axis, theta).multiply(mesh.matrix);
  mesh.rotation.copy((new THREE.Euler()).setFromRotationMatrix(mesh.matrix));
};

export var create = function (options) {
  options = _.extend({x: 8, y: 8}, options);
  return {
    type: 'user',
    id: options.id,
    game: options.game,
    body: BallBody.create(options),
    mesh: config.node ? null : BallMesh.create(options),
    acceleration: new b2.b2Vec2(0, 0),
    lastBroadcast: 0,
    needsBroadcast: 0
  };
};

export var destroy = function (user) {
  b2.destroy(user.acceleration);
  user.game.world.destroy(user.body);
  if (!config.node) user.game.scene.remove(ball.mesh);
};
{'fraction_non_alphanumeric': 0.15255813953488373, 'fraction_numerical': 0.013023255813953489, 'mean_word_length': 4.030409356725146, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '320911', 'n_tokens_mistral': 1524, 'n_tokens_neox': 1459, 'n_words': 332}