Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finding blocks to check for intersections #24

Closed
OperationDarkside opened this issue Dec 21, 2019 · 2 comments
Closed

Finding blocks to check for intersections #24

OperationDarkside opened this issue Dec 21, 2019 · 2 comments

Comments

@OperationDarkside
Copy link

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
@D3PSI
Copy link
Contributor

D3PSI commented Jan 5, 2020

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

  • What exactly does the algorithm do?
  • How does it work?
  • What makes it better than other solutions and why is it suitable for the problem at hand?

Thanks for the initiative!

@Hopson97
Copy link
Owner

Hopson97 commented Jan 5, 2020

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 :)

@Hopson97 Hopson97 closed this as completed Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants