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

Optimizing raycasting operations #6

Open
IllusionTheDev opened this issue Sep 16, 2022 · 6 comments
Open

Optimizing raycasting operations #6

IllusionTheDev opened this issue Sep 16, 2022 · 6 comments
Labels

Comments

@IllusionTheDev
Copy link

IllusionTheDev commented Sep 16, 2022

Feature description

Hey,

I've been experimenting with making my own map rendering engine, and I'd like to leave some tips for just better overall performance:

There are 3 ways you can approach raycasting, it seems like you're doing method 1:

Method 1:

  • Grab the start location, and add a very tiny miniscule offset, increment by that offset until you reach a frame. (It takes about 5 * 1/offset * distance operations, if your offset is 1/128, you're doing 640 iterations, measures at about 3ms / cast on my machine

Method 2 (1+):

  • Grab the start location, and increment by the normalized offset, until there's a frame at the current block. Then, do a finer cast through method one (1-5 + (1/offset iterations, if there is a frame)) - About 5x better, measures at about 0.1ms / cast on my machine

Method 3:

  • Grab the start location, and increment by the normalized offset. If there is a frame at the current block, estimate the frame's bounding box by grabbing the frame's center location, and shifting in the opposite direction of where the frame is looking at, you'll always end up with ceil(distance) iterations, instead of <DISTANCE * STEPS_PER_BLOCK>, measures at about 0.015ms / cast on my machine

Further optimizations:

  • Cache the converted colors of your ColorMap, from what I've measured, mapping colors takes about 90 microseconds per color, but marginally less if cached.

Relevant issues

No response

@IllusionTheDev IllusionTheDev added the enhancement New feature or request label Sep 16, 2022
@cerus
Copy link
Owner

cerus commented Sep 16, 2022

Thank you for opening an issue! I didn't think much about performance when I implemented raycasting stuff to be honest, 3ms seemed "good enough" to me. If you want to contribute you can optimize the raycasting stuff yourself, just let me know :D (If you want to participate in Hacktoberfest you can also wait until October)


Cache the converted colors of your ColorMap, from what I've measured, mapping colors takes about 90 microseconds per color, but marginally less if cached.

The ColorCache class is responsible for caching colors, I think that's what you're looking for.

@IllusionTheDev
Copy link
Author

IllusionTheDev commented Sep 16, 2022

when I implemented raycasting stuff to be honest, 3ms seemed "good enough" to me.

Given that 50mspt is your upper limit, anything above 1ms can cause tons of issues, specially with multiple players interacting at the same time.

Possible issue with the color cache: Constant mapping if the the color is transparent (id 0 according to minecraft's color mappings)

@cerus
Copy link
Owner

cerus commented Sep 16, 2022

Possible issue with the color cache: Constant mapping if the the color is transparent (id 0 according to minecraft's color mappings)

Good catch, that should be a simple fix. I'll open an issue for that.

@cerus
Copy link
Owner

cerus commented Sep 16, 2022

Possible issue with the color cache: Constant mapping if the the color is transparent (id 0 according to minecraft's color mappings)

Good catch, that should be a simple fix. I'll open an issue for that.

I just remembered that it's impossible to feed transparent colors to the ColorCache class since you're only able to provide (r, g, b), so this shouldn't be an issue

@IllusionTheDev
Copy link
Author

Possible issue with the color cache: Constant mapping if the the color is transparent (id 0 according to minecraft's color mappings)

Good catch, that should be a simple fix. I'll open an issue for that.

I just remembered that it's impossible to feed transparent colors to the ColorCache class since you're only able to provide (r, g, b), so this shouldn't be an issue

This leads to another issue then - How are you gonna handle transparent images?

@cerus
Copy link
Owner

cerus commented Sep 16, 2022

This leads to another issue then - How are you gonna handle transparent images?

When converting an image you simply have to check if the pixel you're converting has an alpha value of zero (or whatever threshold you'd like for transparency). maps doesn't have any built-in image converters iirc.

Pseudo code, should be roughly the same for BufferedImages:

Image img = ...;
bool hasAlpha = img.getColorModel().hasAlpha(); // Works for BufferedImage
MapGraphics<?, ?> graphics = MapGraphics.standalone(img.width, img.height);
for (x = 0; x < img.width; x++) {
  for (y = 0; y < img.height; y++) {
    int pixel = img.getRGB(x, y);
    java.awt.Color col = new java.awt.Color(pixel, hasAlpha);
    if(col.alpha == 0) {
      graphics.setPixel(x, y, (byte) 0);
    } else {
      graphics.setPixel(x, y, ColorCache.rgbToMap(col.r, col.g, col.b));
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants