schteppe / cannon.js

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

Can cannon.js be used in LayaBox3D? #391

Open Jasonwky opened 6 years ago

Jasonwky commented 6 years ago

I want to use in LayaBox3D,Can officials give an example?

swift502 commented 6 years ago

I can't even find out what LayaBox3D is. Is it a game engine? Is it a rendering library? A Unity addon? You probably won't find official examples with this. I've certainly never heard of it.

Jasonwky commented 6 years ago

This is website:https://www.layabox.com/, a chinese game engin.

swift502 commented 6 years ago

Looks very cool! Again there probably aren't many examples, but it should be doable if you know how to work with this layabox engine.

Cannon really is an independent system. If you look at the current examples, it usually goes something like:

  1. Cannon.js init
  2. Three.js init
  3. Synchronize three and cannon

You keep the first step. Initialize Cannon scene, add objects and step the world. All of that is really independent and you can copy all of that code. Just ignore anything three.js related, which again, shouldn't be difficult, as the two systems are pretty well separated.

As for synchronization, you now need to go through your layabox objects inside your layabox Update loop and say: layaBoxBody.position = cannonBody.position; layaBoxBody.rotation = cannonBody.rotation;

So the Cannon part really is the same everywhere. If you need help with layabox's object transformation or scene management, you'll need to look into layabox's documentation. Then just build a bridge between the two and you're done.

Might be a lot more difficult than I imagine it to be but I wish you good luck.

jwdunn1 commented 6 years ago

Also take a look at the demo (http://layaair.ldc.layabox.com/demo/?2d&Physics&Slingshot) that uses matter.js (2D physics). Integrating cannon.js for 3D physics should be similar. (Source on GitHub here: https://github.com/layabox/layaair/blob/master/samples/js/2d/samples/Physics_Slingshot.js)

Jasonwky commented 6 years ago

Thank you,I'll try it.

Jasonwky commented 6 years ago

I cannot find rotation in cannonBody,Can you tell me in which class I can find it?

swift502 commented 6 years ago

Oh sorry, rotation is stored as a quaternion in a Body object. (a 4D vector, it prevents gimbal lock) http://schteppe.github.io/cannon.js/docs/classes/Body.html image

Does layabox support quaternions? Might not if it's mainly a 2D engine. Converting quaternions can be tricky.