Comments (11)
Seems like I've got it working too! Thanks a lot!
from box2d.js.
After a day of messing around with this, I was finally able to successfully build Box2D with the b2RayCastInput and b2RayCastOutput bindings. As a web developer it's quite humbling to see the complexity that goes into something like this. I followed the suggestions in Issue #3 and added simple constructors to the missing structures. The method I was after was b2Fixture.RayCast and not b2World.RayCast which led to more confusion with the customizeVTable.
Anyway, I cleared it up and it's running great.
For anyone interested this is what I changed in b2Collision.h
/// Ray-cast input data. The ray extends from p1 to p1 + maxFraction * (p2 - p1).
struct b2RayCastInput
{
b2RayCastInput() {}
b2Vec2 p1;
b2Vec2 p2;
float32 maxFraction;
};
/// Ray-cast output data. The ray hits at p1 + fraction * (p2 - p1), where p1 and p2
/// come from b2RayCastInput.
struct b2RayCastOutput
{
b2RayCastOutput() {}
b2Vec2 normal;
float32 fraction;
};
from box2d.js.
Without compiling myself, how can I get basic raycasts to work?
from box2d.js.
Even when compiling yourself, do you use a JSQueryCallback to use raycasts? @shannon -- if you're still looking at this issue, just wondering what JS code interfaces with your changes. Thanks!
from box2d.js.
@cwindolf It's been a really long time but looking over my code from back then it's something like this:
var rayCastInput = new Box2D.b2RayCastInput();
var rayCastOutput = new Box2D.b2RayCastOutput();
function raycast(b2Fixture, p1, p2, maxFraction){
rayCastInput.set_p1(p1);
rayCastInput.set_p2(p2);
rayCastInput.set_maxFraction(maxFraction);
if(b2Fixture.RayCast(rayCastOutput, rayCastInput)){
return {
normal: rayCastOutput.get_normal(),
fraction: rayCastOutput.get_fraction()
}
}
return false;
}
from box2d.js.
Whoa, fast reply, was barely expecting one! Looks promising, thanks very much @shannon!
from box2d.js.
While looking fix history and change logs, seems like b2RayCastInput/b2RayCastOutput has been already added to b2Collision.h. But still, I'm getting exception while trying to create new instance of those objects: "cannot construct a b2RayCastInput, no constructor in IDL". Tried booth builded versions 2.3.1_min.js/2.2.1_min.js
from box2d.js.
@IljaK I'm having the same issue. I tried doing raycasts through the JSQueryCallback but they don't work for me. Any leads on how to fix this?
from box2d.js.
I did get this to work eventually. The build I'm using doesn't take JSQueryCallback
, b2RayCastInput
or b2RayCastOutput
. It takes a JSRayCastCallback
and two points. The JSRayCastCallback
must then implement the ReportFixture
function, which takes a fixture, a point, a normal and a fraction in that order.
from box2d.js.
Could you paste some code example?
from box2d.js.
Sure. In this code, box2d
is my instantiated box2D. This code is not finished at all, but it logs the correct fraction of the ray because ReportFixture
is called.
const _callback = new box2d.JSRayCastCallback();
const configureCallback = () => {
_callback.ReportFixture = function(fixture, point, normal, fraction) {
const f = box2d.wrapPointer(fixture, box2d.b2Fixture);
_callback.m_fixture = fixture;
if (f)
console.log(fraction);
return fraction;
};
};
/**
* Get the length of the ray.
* @returns {Number} The portion of the ray that could be cast in the range [0, 1].
*/
this.getLength = () => {
let from = body._getBody().GetWorldPoint(new box2d.b2Vec2(x, y));
world.RayCast(
_callback,
new box2d.b2Vec2(from.get_x(), from.get_y()),
new box2d.b2Vec2(from.get_x() + ray.x, from.get_y() + ray.y));
return 0.5;
};
configureCallback();
from box2d.js.
Related Issues (20)
- b2Vec2 won't free HOT 2
- Unable to build latest on macOS Mojave with [email protected] HOT 5
- High Gravity jitter HOT 1
- missing runtime methods for allocating arrays HOT 4
- box2d.wasm.html test faild with [email protected] build HOT 2
- is there typescript declaration file (d.ts) for this project? HOT 2
- Box2d 2.4.0
- Box2D 2.4.0 HOT 8
- Node server side implementation
- How does this work with webpack? HOT 1
- May I ask why the calculation results are different on the ios and android platforms HOT 2
- vue how to use it
- vue hao to use it
- why is not show?
- why now show debugdraw
- could i have more simple demos about the box2djs οΌand where can i see the API about box2djs
- How to build? I want to build the debug version of javascript file
- Position not getting what is set
- Uncaught (in promise) cannot construct a b2Draw, no constructor in IDL
- Cannot SetTarget of b2MouseJoint
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 box2d.js.