flame icon indicating copy to clipboard operation
flame copied to clipboard

feat!: Raycasting and raytracing

Open spydon opened this issue 3 years ago • 5 comments

Description

This PR implements raytracing and raycasting for the built-in hitboxes.

Depends on #1787 and #1788

Left to do:

  • [x] Raytracing with result (trivial)
  • [x] Raytracing example
  • [ ] Docs

Checklist

  • [x] The title of my PR starts with a Conventional Commit prefix (fix:, feat:, docs: etc).
  • [x] I have read the Contributor Guide and followed the process outlined for submitting PRs.
  • [x] I have updated/added tests for ALL new/updated/fixed functionality.
  • [ ] I have updated/added relevant documentation in docs and added dartdoc comments with ///.
  • [ ] I have updated/added relevant examples in examples.

Breaking Change

  • [x] Yes, very minor one though

Migration instructions

If you pass in your own collision detection system to the HasCollisionDetection mixin you have to change the signature of that to: CollisionDetection<ShapeHitbox>instead of CollisionDetection<Hitbox>.

Related Issues

spydon avatar Jul 07 '22 18:07 spydon

Perhaps it would make sense to try to split this PR into several smaller ones, so that they are easier to review, and they are better reflected in the change log? For example, the Ray2 class can probably be introduced in a standalone PR; similarly some new methods for the Vector2 class.

Yeah, probably a good idea, split it up in #1787 and #1788.

spydon avatar Jul 08 '22 12:07 spydon

I've tried running the example - and it turned out to be extremely laggy when I move the mouse. After adding print('render') into the render method, and print('casting') into the castRays method, I'm seeing the following output:

render
render
render
render
render
render
render
render
render
render
render
render
render
render
render
render
render
casting
render
render
render
render
render
casting
casting
render
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
render
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
render
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
casting
My guess here is that there are many mouse-move events being generated, and until each of them is processed, the next render is not scheduled. And since each raycast operation is expensive, they end up clogging the pipeline.

Recommendation: accumulate the results of mouse moves in a variable, and then process the them all at once during the update().

st-pasha avatar Jul 08 '22 17:07 st-pasha

I've tried running the example - and it turned out to be extremely laggy when I move the mouse. After adding print('render') into the render method, and print('casting') into the castRays method, I'm seeing the following output: My guess here is that there are many mouse-move events being generated, and until each of them is processed, the next render is not scheduled. And since each raycast operation is expensive, they end up clogging the pipeline.

Recommendation: accumulate the results of mouse moves in a variable, and then process the them all at once during the update().

Fixed that! And broke something else, seems like something is wrong with the broadphase now. 😅

spydon avatar Jul 09 '22 11:07 spydon

Sometimes this happens (I reduce the number of rays to 20 for clarity): Screen Shot 2022-08-03 at 7 28 21 PM

It's kinda super-rare, but in case you might now what might be causing it? As if the point of origin was at the same time inside the rectangle and outside the screen at the same time.

st-pasha avatar Aug 04 '22 02:08 st-pasha

Sometimes this happens (I reduce the number of rays to 20 for clarity): Screen Shot 2022-08-03 at 7 28 21 PM

It's kinda super-rare, but in case you might now what might be causing it? As if the point of origin was at the same time inside the rectangle and outside the screen at the same time.

Very strange, I can't reproduce this. Can you reproduce this from the same ray origin consistently? If so, what origin and direction do you use? It's weird that it happens to all the rays too...

spydon avatar Aug 09 '22 20:08 spydon

The RaycastResult fields are still not calculated lazily, it showed to be quite complex to get that done and at the same time re-use objects, but I have an open branch where I keep on experimenting on this. I want to make the docs more visual Pasha-style, but I'll have to do that later if we want to get this into the release tomorrow.

spydon avatar Aug 18 '22 21:08 spydon