Comments (6)
@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.
@shakiba you are right! hmm I didn't know those kinds of functions were also called callbacks.
from planck.js.
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.
What function did you import that solved the problem?
from planck.js.
@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.
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)
- When will version 1 be released? HOT 5
- Feature Request: Implementation of b2Draw, SetDebugDraw and DebugDraw HOT 7
- Changing a circle collider's friction doesn't make it roll slower HOT 1
- Feature Request: Add the ESM button to jsDelivr
- How to change a collider's center? HOT 5
- Why does displacement cause multiple begin and end contact events? HOT 6
- Typo in the Wiki HOT 4
- setActive(true) throwing error in v1 beta 8
- ERROR changing a collider's mass cancels its linearVelocity HOT 5
- planck's npm package is quite large HOT 8
- World step error HOT 12
- possible testOverlap bug HOT 2
- Undefined Reading "x" error HOT 5
- clarification on the determinism limitation from docs HOT 1
- pointers on how to network planck HOT 8
- inconsistent Vec2 construction throughout examples
- DistanceJoint `getReactionForce` function not working HOT 4
- Script tag: Uncaught TypeError: t is null HOT 2
- Cannot read properties of undefined (reading "x") HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from planck.js.