Giter Club home page Giter Club logo

Comments (6)

shakiba avatar shakiba commented on May 24, 2024 1

@quinton-ashley thanks for your comments and suggestions.

To clarify, the return value of the callback is used to update the initial maxFraction in the raycast input. So 0 would effectively stop the raycast, and negative is ignored as invalid.

Also, I think the parameter is named correctly according to MDN:

A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.

Examples of synchronous callbacks include the callbacks passed to Array.prototype.map(), Array.prototype.forEach(), etc. Examples of asynchronous callbacks include the callbacks passed to setTimeout() and Promise.prototype.then().

from planck.js.

quinton-ashley avatar quinton-ashley commented on May 24, 2024 1

@shakiba you are right! hmm I didn't know those kinds of functions were also called callbacks.

from planck.js.

codingMASTER398 avatar codingMASTER398 commented on May 24, 2024

I inexplicably solved this using the "RayCastClosest" example at https://piqnt.com/planck.js/RayCast .
I just imported the function, and everything seemed to... magically work? No idea what wizardry it does. More helpful documentation on this would be nice.

from planck.js.

shakiba avatar shakiba commented on May 24, 2024

What function did you import that solved the problem?

from planck.js.

quinton-ashley avatar quinton-ashley commented on May 24, 2024

@shakiba In the docs, the use of the term "callback" in WorldRayCastCallback is a bit misleading because planck's world.rayCast is not an async function. "callback" in JavaScript refers to an input function that runs after the called function finishes, almost always because the caller function is async. I think the input function to world.rayCast could more accurately be called a filter.

https://github.com/piqnt/planck.js/blob/master/docs/api/classes/world.md#raycast

Also I think since codingMASTER was only interested in finding the closest callback so he found the solution to his problem in your example's RayCastClosest function.

@codingMASTER398 This may have been added by Shakiba later but the example now notes "do not assume that fixtures are reported in order. However, by clipping, we can always get the closest fixture."

Why is that? It's because the ray cast is not performed in the straight forward way one may expect, by testing points along the ray cast line, which is called Ray Marching.

Ray casting in Box2D is done by looking at nearby fixtures in the Dynamic AABB Tree, a specialized data structure that stores all the fixture's AABBs. The visually closest nodes to the fixture at the starting point may be stored further apart in the dynamic tree. Since planck's raycasting actually works by looping through fixtures in the tree, that's why fixtures may not be reported in the order a ray would hit them. Complicated stuff aye?

This also means that unlike with ray marching, we can't cancel a planck raycast just because for example it hits a wall, because the wall might be behind a target object.

from planck.js.

quinton-ashley avatar quinton-ashley commented on May 24, 2024

In the filter function, returning:

-1 stops the ray casting
greater than 0 limits the ray by a fraction of the distance to the second point
1 maintains the full length of the ray cast

from planck.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.