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

feat!: Raycasting and raytracing #1785

Merged
merged 60 commits into from
Aug 19, 2022
Merged

feat!: Raycasting and raytracing #1785

merged 60 commits into from
Aug 19, 2022

Conversation

spydon
Copy link
Member

@spydon spydon commented Jul 7, 2022

Description

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

Depends on #1787 and #1788

Left to do:

  • Raytracing with result (trivial)
  • Raytracing example
  • Docs

Checklist

  • The title of my PR starts with a Conventional Commit prefix (fix:, feat:, docs: etc).
  • I have read the Contributor Guide and followed the process outlined for submitting PRs.
  • 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

  • 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

packages/flame/lib/src/collisions/collision_detection.dart Outdated Show resolved Hide resolved
packages/flame/lib/src/collisions/collision_detection.dart Outdated Show resolved Hide resolved
packages/flame/lib/src/extensions/aabb2.dart Outdated Show resolved Hide resolved
packages/flame/lib/src/extensions/vector2.dart Outdated Show resolved Hide resolved
packages/flame/lib/src/geometry/ray2.dart Outdated Show resolved Hide resolved
packages/flame/lib/src/geometry/ray2.dart Outdated Show resolved Hide resolved
Copy link
Contributor

@st-pasha st-pasha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@spydon
Copy link
Member Author

spydon commented Jul 8, 2022

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 spydon changed the title feat: Raycasting and raytracing feat!: Raycasting and raytracing Jul 8, 2022
@st-pasha
Copy link
Contributor

st-pasha commented Jul 8, 2022

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().

@spydon
Copy link
Member Author

spydon commented Jul 9, 2022

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 spydon changed the base branch from main to spydon/ray2 July 25, 2022 12:36
Base automatically changed from spydon/ray2 to main July 27, 2022 15:51
@spydon
Copy link
Member Author

spydon commented Aug 9, 2022

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
Copy link
Member Author

spydon commented Aug 18, 2022

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 spydon requested a review from st-pasha August 18, 2022 22:27
doc/flame/collision_detection.md Outdated Show resolved Hide resolved
doc/flame/collision_detection.md Outdated Show resolved Hide resolved
doc/flame/collision_detection.md Outdated Show resolved Hide resolved
doc/flame/collision_detection.md Outdated Show resolved Hide resolved
doc/flame/collision_detection.md Outdated Show resolved Hide resolved
doc/flame/collision_detection.md Outdated Show resolved Hide resolved
doc/flame/collision_detection.md Outdated Show resolved Hide resolved
spydon and others added 2 commits August 19, 2022 21:38
Co-authored-by: Pasha Stetsenko <stpasha@google.com>
@spydon spydon merged commit ed452dd into main Aug 19, 2022
@spydon spydon deleted the spydon/raycasting branch August 19, 2022 20:44
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

Successfully merging this pull request may close these issues.

3 participants