Hopson97 / open-builder

Open "Minecraft-like" game with multiplayer support and Lua scripting support for the both client and server
https://github.com/Hopson97/open-builder/projects/3
GNU General Public License v3.0
700 stars 80 forks source link

Finding blocks to check for intersections #24

Closed OperationDarkside closed 4 years ago

OperationDarkside commented 4 years ago

Hi,

I recently started a similar project and wrote a different algorithm for finding removal/placing blocks than you in your one-week challenge. If you want you can take this code:

bool compare(float one, float two, bool less) {
 if (less) {
  return one < two;
 } else {
  return two < one;
 }
}

// Get Position
 glm::vec3 &pos = camera.Position;

 // Get Direction
 glm::vec3 &direction = camera.Front;

 Ray ray(pos, direction);

 // Get current chunkblock
 float posX = pos.x;
 float posY = pos.y;
 float posZ = pos.z;

 // Get potential penetrating blocks
 // Get end point
 glm::vec3 endPoint = pos + (direction * RAY_LEN);

 bool xDirPositive = direction.x > 0;
 bool yDirPositive = direction.y > 0;
 bool zDirPositive = direction.z > 0;

 float xIncr = xDirPositive ? 1 : -1;
 float yIncr = yDirPositive ? 1 : -1;
 float zIncr = zDirPositive ? 1 : -1;

 std::vector<std::array<glm::vec3, 2>> boxesToCheck;

 for (float x = posX; compare(x, endPoint.x, xDirPositive); x += xIncr) {
  for (float y = posY; compare(y, endPoint.y, yDirPositive); y += yIncr) {
   for (float z = posZ; compare(z, endPoint.z, zDirPositive); z += zIncr) {

    int intX = static_cast<int>(x);
    int intY = static_cast<int>(y);
    int intZ = static_cast<int>(z);
    glm::vec3 intVec { intX, intY, intZ };

    int intCeilX = intX + (x < 0 ? -1 : 1);
    int intCeilY = intY + (y < 0 ? -1 : 1);
    int intCeilZ = intZ + (z < 0 ? -1 : 1);
    glm::vec3 intCeilVec { intCeilX, intCeilY, intCeilZ };

    boxesToCheck.push_back( { intVec, intCeilVec });
   }
  }
 }

// Your AABB intersection checks
cedricschwyter commented 4 years ago

I think for this to be useful, we need some things explained from you:

Thanks for the initiative!

Hopson97 commented 4 years ago

Thanks for the suggestion, however rolled my own:

    //Create a "ray"
    Ray ray(mp_player->position, mp_player->rotation);

    //Step the ray until it hits a block/ reaches maximum length
    for (; ray.getLength() < 8; ray.step()) {
        auto rayBlockPosition = toBlockPosition(ray.getEndpoint());
        if (m_chunks.manager.getBlock(rayBlockPosition) == 1) {
            BlockUpdate blockUpdate;
            blockUpdate.block = button == sf::Mouse::Left ? 0 : 1;
            blockUpdate.position = button == sf::Mouse::Left
                                       ? rayBlockPosition
                                       : toBlockPosition(ray.getLastPoint());
            m_chunks.blockUpdates.push_back(blockUpdate);
        }
    }

Thanks for contributing to the project anyways :)