schteppe / cannon.js

A lightweight 3D physics engine written in JavaScript.
http://schteppe.github.com/cannon.js
MIT License
4.67k stars 709 forks source link

Use with ar-frame and ar.js, and world vs local #361

Open labriata opened 6 years ago

labriata commented 6 years ago

Hello, I'm confused about whether coordinates and vectors (forces, speeds, etc.) are treated in world frameworks, or on local frameworks.

More specifically, I'm using two augmented reality markers with ar.js. Each marker carries spheres that are dynamic bodies treated with cannon.js. Now, I want to simulate attractive or repulsive forces between pairs of spheres from different markers. Cannon.js should let me do this with applyForce, I guess.

I can get the world-based coordinates of the spheres so I can compute the components of the vector forces that should act on the spheres, in the world coordinate framework. But then, how do I apply these forces on my spheres? Should I convert them to the local coordinates of the ar.js markers? Or should I apply them in world coordinates, that is without any transforms? I tried both things and multiple variations but none of the results is what I expect.

schteppe commented 6 years ago

Hi, most of the cannon.js api uses world coordinates. The .position, .velocity and .force properties of Body are all in world space. If you use world coords, adding to body.force.x/y/z should just work... And .addForce() is in world space too.

I haven’t worked with AR myself but I understand it must be tricky with multiple markers that can move around dynamically. If you put a physics box on a marker, I guess it will it have to copy the marker world position on every update? And if these world positions move; how should physics objects, say a cannon ball flying through the air, adapt to the markers?

Maybe doing everything in local marker space would make things easier? If you do this, the local marker space would correspond to the world space in cannon.js... makes sense?

labriata commented 6 years ago

Hi !! OK I will keep trying bearing in mind that in principle, cannon is operating on the world coordinates. Thanks for the info!

labriata commented 6 years ago

Hi again! I'm trying (assuming world coordinates) and I'm getting something but not working very nice (and there's a high chance this is because of my bad coding).

So now I go to a more general question, that might actually be of your interest. Would you consider adding something like electrostatic forces between pairs of charged objects?

Ideally, but doesn't need to get so far, something that will then come up automatically with a-frame-physics between pairs of charged particles (so objects should have the new property 'charge', just like they have mass, and the world should have 'dielectric constant', just like it has gravity).

Thinking out loud... Thanks a lot for your help! And I could tell you more if you are interested.