<commit_before>#pragma once

#include <math.h>

#include "RigidBody.h"
#include "Collider.h"
#include "Solver.h"
#include <vector>
#include <SFML/Graphics.hpp>

struct PhysSystem
{
  RigidBody *AddBody(Coords2f coords, Vector2f size)
  {
    RigidBody newbie(coords, size, 1e-5f);
    newbie.index = bodies.size();
    bodies.push_back(newbie);
    return &(bodies[bodies.size() - 1]);
  }
  void Update(float dt)
  {
    collisionTime = mergeTime = solveTime = 0;

    sf::Clock clock;
    for (size_t bodyIndex = 0; bodyIndex < bodies.size(); bodyIndex++)
    {
      bodies[bodyIndex].IntegrateVelocity(dt);
    }
    mergeTime += clock.getElapsedTime().asSeconds();
    clock.restart();

    collider.FindCollisions(bodies.data(), bodies.size());
    collisionTime += clock.getElapsedTime().asSeconds();
    clock.restart();

    for (size_t jointIndex = 0; jointIndex < solver.contactJoints.size(); jointIndex++)
    {
      solver.contactJoints[jointIndex].valid = 0;
    }
    for (Collider::ManifoldMap::iterator man = collider.manifolds.begin(); man != collider.manifolds.end(); man++)
    {
      for (int collisionIndex = 0; collisionIndex < man->second.collisionsCount; collisionIndex++)
      {
        Collision &col = man->second.collisions[collisionIndex];
        if (!col.userInfo) continue;

        ContactJoint::Descriptor desc;
        desc.body1 = man->second.body1;
        desc.body2 = man->second.body2;
        desc.collision = &col;
        solver.RefreshContactJoint(desc);
      }
    }
    for (Collider::ManifoldMap::iterator man = collider.manifolds.begin(); man != collider.manifolds.end(); man++)
    {
      for (int collisionIndex = 0; collisionIndex < man->second.collisionsCount; collisionIndex++)
      {
        Collision &col = man->second.collisions[collisionIndex];
        if (col.userInfo) continue;
        ContactJoint::Descriptor desc;
        desc.body1 = man->second.body1;
        desc.body2 = man->second.body2;
        desc.collision = &col;
        solver.contactJoints.push_back(ContactJoint(desc));
      }
    }
    solver.RefreshJoints();
    mergeTime += clock.getElapsedTime().asSeconds();
    clock.restart();

    #if 1
    solver.SolveJoints(500, 15);
    #else
    solver.SolveJointsSoA(&bodies[0], bodies.size(), 500, 15);
    #endif

    solveTime += clock.getElapsedTime().asSeconds();
    clock.restart();
    for (size_t bodyIndex = 0; bodyIndex < bodies.size(); bodyIndex++)
    {
      bodies[bodyIndex].IntegratePosition(dt);
    }
    mergeTime += clock.getElapsedTime().asSeconds();
    clock.restart();
  }
  size_t GetBodiesCount()
  {
    return bodies.size();
  }
  RigidBody *GetBody(int index)
  {
    return &(bodies[index]);
  }
  int GetJointsCount()
  {
    return solver.contactJoints.size();
  }
  Collider *GetCollider()
  {
    return &collider;
  }

  float collisionTime;
  float mergeTime;
  float solveTime;
private:
  std::vector<RigidBody> bodies;
  Collider collider;
  Solver solver;
};<commit_msg>Switch to SoA solver by default<commit_after>#pragma once

#include <math.h>

#include "RigidBody.h"
#include "Collider.h"
#include "Solver.h"
#include <vector>
#include <SFML/Graphics.hpp>

struct PhysSystem
{
  RigidBody *AddBody(Coords2f coords, Vector2f size)
  {
    RigidBody newbie(coords, size, 1e-5f);
    newbie.index = bodies.size();
    bodies.push_back(newbie);
    return &(bodies[bodies.size() - 1]);
  }
  void Update(float dt)
  {
    collisionTime = mergeTime = solveTime = 0;

    sf::Clock clock;
    for (size_t bodyIndex = 0; bodyIndex < bodies.size(); bodyIndex++)
    {
      bodies[bodyIndex].IntegrateVelocity(dt);
    }
    mergeTime += clock.getElapsedTime().asSeconds();
    clock.restart();

    collider.FindCollisions(bodies.data(), bodies.size());
    collisionTime += clock.getElapsedTime().asSeconds();
    clock.restart();

    for (size_t jointIndex = 0; jointIndex < solver.contactJoints.size(); jointIndex++)
    {
      solver.contactJoints[jointIndex].valid = 0;
    }
    for (Collider::ManifoldMap::iterator man = collider.manifolds.begin(); man != collider.manifolds.end(); man++)
    {
      for (int collisionIndex = 0; collisionIndex < man->second.collisionsCount; collisionIndex++)
      {
        Collision &col = man->second.collisions[collisionIndex];
        if (!col.userInfo) continue;

        ContactJoint::Descriptor desc;
        desc.body1 = man->second.body1;
        desc.body2 = man->second.body2;
        desc.collision = &col;
        solver.RefreshContactJoint(desc);
      }
    }
    for (Collider::ManifoldMap::iterator man = collider.manifolds.begin(); man != collider.manifolds.end(); man++)
    {
      for (int collisionIndex = 0; collisionIndex < man->second.collisionsCount; collisionIndex++)
      {
        Collision &col = man->second.collisions[collisionIndex];
        if (col.userInfo) continue;
        ContactJoint::Descriptor desc;
        desc.body1 = man->second.body1;
        desc.body2 = man->second.body2;
        desc.collision = &col;
        solver.contactJoints.push_back(ContactJoint(desc));
      }
    }
    solver.RefreshJoints();
    mergeTime += clock.getElapsedTime().asSeconds();
    clock.restart();

    #if 0
    solver.SolveJoints(500, 15);
    #else
    solver.SolveJointsSoA(&bodies[0], bodies.size(), 500, 15);
    #endif

    solveTime += clock.getElapsedTime().asSeconds();
    clock.restart();
    for (size_t bodyIndex = 0; bodyIndex < bodies.size(); bodyIndex++)
    {
      bodies[bodyIndex].IntegratePosition(dt);
    }
    mergeTime += clock.getElapsedTime().asSeconds();
    clock.restart();
  }
  size_t GetBodiesCount()
  {
    return bodies.size();
  }
  RigidBody *GetBody(int index)
  {
    return &(bodies[index]);
  }
  int GetJointsCount()
  {
    return solver.contactJoints.size();
  }
  Collider *GetCollider()
  {
    return &collider;
  }

  float collisionTime;
  float mergeTime;
  float solveTime;
private:
  std::vector<RigidBody> bodies;
  Collider collider;
  Solver solver;
};
{'fraction_non_alphanumeric': 0.13652425313202698, 'fraction_numerical': 0.01027947317699968, 'mean_word_length': 2.773939393939394, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 27, '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': '6226592', 'n_tokens_mistral': 2116, 'n_tokens_neox': 1990, 'n_words': 367}