piqnt / planck.js Goto Github PK
View Code? Open in Web Editor NEW2D JavaScript Physics Engine
Home Page: http://piqnt.com/planck.js/
License: MIT License
2D JavaScript Physics Engine
Home Page: http://piqnt.com/planck.js/
License: MIT License
I'd like to reset a world by destroying all of its joints, bodies, etc. Was thinking of this feature. If it makes sense, please consider this email an official request for that.
When I destroy a fixture using body.destroyFixture, I expect the body's fixture list to be updated.
Expectation: Fixture list without the destroyed fixture.
Actual: Fixture list includes the destroyed fixture, with null pointer m_next.
Compared to createFixture function, I do not see m_fixtureList updated in destroyFixture.
I use the WheelJoint to create a car with suspension.
But when the car tilted and fall down, the wheel will not fixed on the WheelJoint axis.
Phaser WheelJoint(https://github.com/flyover/box2d.js):
Thanks for the nice library!
Any advice on how to accurately serialize the state of a planck World? Simply iterating over all bodies/fixtures and copying their positions/velocities/vertices/etc. doesn't work for collisions in progress, etc. There are also a few Function objects in the object hierarchy, hindering more straightforward serialization mechanisms. (FWIW, serialization is something that libraries like matter.js do support out-of-the-box.) Thanks!
Are all the demo setting world.step(1 / 60)
?
I use this in my gravity based program and I find the sprite moves very slow. I am using PIXI.js as the renderer and its ticker rate is 60 FPS I believe. If the world time step is unrelated to the frame rate how do I know what time step to use to get a smooth movement?
Can i fill a body with a image file?
PrismaticJoint.prototype.getReactionForce = function(inv_dt) {
return inv_dt * (this.m_impulse.x * this.m_perp + (this.m_motorImpulse + this.m_impulse.z) * this.m_axis);
};
This returns NaN, because this.m_perp and this.m_axis are vectors not numbers. Can't figure out how to fix this myself! Thanks again for all the work you've done on this engine!
Hey @shakiba - I'm working on an MMO game for the Web, and part of the game design is to use a quad tree on the broadphase for spatial partitioning of events. Effectively a player would only process the physics and network events for near by quads.
I'm still new with working in planck.js, but do you have any suggestions on the right way to connect into the world step to support spatial partitioning in such a way to only process a subset of the world? perhaps bodies remain, but collision detection is reduced to a subset, or perhaps some other ideas for the right approach?
Would you please publish a types file for Typescript?
Hi,
I had a chance to play with Planck.js and Stage.js, but separately.
I'm not js guru. In the last few days I decided to use the library without the testbed.
My first choice was to replace it with Stage.js.
I follow box2d manual hello world example, nothing super ambitious.
I have a bouncing ball on a flat surface/edge. Now I want to use Stage.js to render that on the screen.
It seems like all examples are testbed examples. Even though it supposed to be only a case of calling the world.step(timestep) function, I'm still struggling as I assume there will be something similar needed on the Stage.js side.
Would it be possible to post an example that uses Stage.js? Something really simple.
Thank you.
Chris
In lib/fixture.js:
Fixture.prototype.setDensity = function(density) {
_ASSERT && common.assert(Math.isFinite(density) && density >= 0.0);
this.m_density = density;
}
The function is just isFinite
, not Math.isFinite
.
The definition of SetAsBox
is not found in lib/shape/PolygonShape.js.
as title says. I see the /dist
folder here on GH, but not if I npm install.
A question more than an issue.
If you were to create a game with this lib, how would generate a fixture for text representation, using an SVG path (of said text string as an outline) as input?
PS: this project seems to get a lot more attention from its author/maintainer than all others out there; thanks a lot for that. This is much appreciated, given how not easy physics engines are.
I have a feeling this is not a bug, yet I remember this not happening in my (looooong ago) tests with Box2D.
When a dynamic body perfectly overlaps another, static one -- their world center coords are same -- and both are joined by a revolute joint (with default jointdef values), it seems that changing world gravity has no effect on the dynamic one; it stays in place, not rotating.
If however I offset one from the other such that they no longer overlap, the dynamic one now responds fluidly to gravity changes.
See this example .
Does this seem normal to you? Joint friction perhaps? If not, I can test this on another testbed and see what comes up.
Hello :) When you have time, could you please report your last fixes into the generated js files (into the dist/ folder) please ? thanks ^^
Hi, i´ve been trying add images on planks.js, but i colud not make it. i´d like load images through Stage.js and planck-with-testbed to can see rendering images on my canvas. this repository have no examples with images loaders and textures. ¿is it possible, load images and textures on plank.js viewer?
Thanks. your api is amazing.
clearly, the code at https://github.com/shakiba/planck.js/blob/0024d26/example/RayCast.js#L123 should be
return def;
Does it support radial gravity? Or have you got an implementation to show for it? Thanks.
I am porting the Top-down car demo and computing a projection of one vector on another one is quite a common operation there. Hence I implemented a projectOn
method for the Vec2
class and the issue is that I am unable to run a testbed so that it picks up the new version of the Vec2
class.
While I am able to rebuild dist/planck.js
using gulp so that it includes the new method it is still not picked up by the testbed. Naturally, I am wondering what goes wrong.
I am new to js so I apologize if the issue is trivial.
I was wondering whether normalize
should return a reference to this
rather than the old length so that it makes it possible to use it in function chaining. Or alternatively create a normal()
that is a wrapper around normalize
and just returns this
.
See the example below:
const myVec = vec.clone()
.sub(vec2)
.normalize()
.mul(0.5)
// and so on
Really cool project by the way! My only complaint is the minimal documentation which makes it harder as I don't have an understanding of where everything is in the library yet.
I'm trying to build a revolute joint that stays at the lower limit if no button is pressed, and rotates to the upper limit when I press a button. The problem is that when the joint is on the limits, it "sticks" to them, and takes a while to start rotating.
I have built this example on JS Bin
In the example, the 'up' arrow makes the joint rotate. It responds immediately if it's between the limits, but when it's at the limits it has a delay before it starts moving.
I'm not sure if it's a problem with the library, or if there's a way to code this better so that the issue stops happening.
Thank you
just like the table tennis game.
the ball has linearDamping and angularDamping ,is forced apply by one velocity, and collision with the wall ,then moves to anthor angle
how could i know the trajectories in advance before the ball is forced apply?
Planck looks cool! I'm thinking about using it to prototype a simulation, where characters may drive large vehicles around in a large, top-down, 2D environment. I'd like to have characters that can walk around inside of a vehicle while it's being moved.
I'm wondering how I might model this problem with planck. 3 initial strategies that come to mind:
Any feedback/ideas would be much appreciated!
Hi Shakiba.
I need know, if exist some library, tool or thecnique that allow me to make optimization with canvas on Cordova. I actually be making a game to android, but my canvas is working so slow. also, i´d want know if this implementation is viable, ¿create a game using plank.js and Stage.js?
Thank´s.
Uncaught TypeError: testbed.play is not a function. Got exception when resuming from pause state in testbed.
To reproduce, go to example and press 'p' to pause and the again to resume.
In testbed function, togglePause() play() should be resume().
In the documentation there are a few references to the variable hz
// in each frame call word.step(hz) with fixed frequency
world.step(1 / 60);
And that this variable is the 'frequency'. However, below this we have 1/60. This is not frequency (which is 60 Hz, not 1/60), this is the time step. The example ReadMe should be updated to reflect this.
Also from the source docs:
680 /**
681 * Take a time step. This performs collision detection, integration, and
682 * constraint solution.
683 *
684 * Broad-phase, narrow-phase, solve and solve time of impacts.
685 *
686 * @param {float} ts Time step, this should not vary.
687 * @param {float} dt Elapsed time, since last call.
688 */
689 World.prototype.step = function(ts, dt) {
It seems like the upperAngle and lowerAngle values don't work: when setting them along with enableLimit the joint won't work anymore.
(Great work on the library, btw!)
@shakiba congrats on this amazing work and project. I believe this is very needed in the community and it's exactly what I was looking for months ago. I ended up settling on p2.js, which also looks inspired by box2d, so it'll be easy for me to port my game to planck.js
p2.js was very good, but I'm glad to see someone crafting a clean and even refactored box2d port to JS - so thank you very much.
Of course there are many architectures when writing a game - in my case I used an ECS design to break everything into synchronous and discrete set of steps in time. However with listeners, it opens up to an asynchronous state which maybe or maybe not is compatible with ECS - what do you think?
also, what is the point of the events versus other state testing functions in a physics engine, and having an API that is friendly to an ECS or other game architectures?
Planck currently calls a function Abs() which is not defined.
My results are:
10 passing (58ms)
1 failing
There is also a missing dependency on the Mocha test framework.
PS- This looks like great port of box2d to javascript, congrats.
More a question than an issue.
I have 3 fixtures:
I have 2 wheel joints:
Now, I turn on the wheel joint motors. The car moves.
I want to draw each wheel joint's anchor in realtime, as the car moves. To do that, I need to obtain the current world coordinates of body B. Must I keep a ref to B and ask for its world point at each tick, or is there a way to obtain that info with the joint alone (I didn't see a getBodyB function for wheel joints)?
Hallo,
I'm using a DistanceJoint
to attract a DynamicBody
to a static center Body
. The original Box2D implementation allows setting the length of the DistanceJoint
through DistanceJointDef
. In planck-js the length is automatically calculated as the distance between the two anchors:
this.m_length = Vec2.distance(anchorB, anchorA);
Currently I'm overwriting m_length
after creating the joint. It would be nice if the length could be specified in the definition.
Where can I find the API documentation? Like the list of main classes and their methods.
Is there any way to integrate planckjs in Phaser 2? I couldn't find anything related to this.
Phaser 2 has box2d integration, so i don't think it would be hard to integrate, but some hints/help wouldn't hurt :)
Also is planckjs deterministic?
Thanks!
I need the objects of my world to react as another 'special' object passes by and touches them. For technical reasons, I am setting the position of this special object programmatically (setting the theBody.c_position.c.x
and theBody.c_position.c.y
properties).
Suppose I position the special object obj
at a certain point (x,y) around which there other objects. obj
is located properly (I can see it moving) but it does not seem to affect the surrounding objects when touching them. Instead, it just overlaps them and is just drawn on top of them.
What am I missing here? I suspect the problem has to do with motion, but the only thing I have at hand is the desired location of the object I need to move programmatically.
Working on an application in PIXI.js, and the functionality of Mat22 would facilitate some of the translation between the two libraries ( scaling for pixel => meter ).
add the following code to index.js, and define the types in index.d.js
Code:
exports.Mat22 = require('./common/Mat22');
Type:
declare let Mat22: {
new ( a: number, b: number, c: number, d: number ): Mat22;
( a: number, b: number, c: number, d: number ): Mat22;
new (a: { x: number; y: number }, b: { x: number; y: number }): Mat22;
(a: { x: number; y: number }, b: { x: number; y: number }): Mat22;
new (): Mat22;
(): Mat22;
mul(mx: Mat22, my: Mat22): Mat22;
mul(mx: Mat22, v: Vec2): Vec2;
mulT(mx: Mat22, my: Mat22): Mat22;
mulT(mx: Mat22, v: Vec2): Vec2;
abs(mx): Mat22;
add(mx1, mx2): Mat22;
}
I am not familiar with Box2D but I know there is some difference between each version. Are the algorithms all based on the latest version of Box2D?
In most projects , the dir namelib
means dependencies (the 3rd lib) , src
means the source code of current project.
So , I hope you could rename lib
to src
Should I always use a testbed? It renders always debug layer. Any example how to draw a basic texture?
Hey,
Trying to get the reaction force and torque out of a joint to determine if I should remove the joint (creating "breakable" joints).
I'm not sure if this code in Planck is correct:
RevoluteJoint.prototype.getReactionForce = function(inv_dt) {
var P = Vec2.neo(this.m_impulse.x, this.m_impulse.y);
return inv_dt * P;
};
P is an object with x and y properties, and then the object is multiplied by a number? This returns NaN. Should it be p.mul(inv_dt) instead so the multiplication works correctly?
Thanks!
I'm finding that a gear joint between two revolute joints works fine, and a gear joint between a revolute and a prismatic work fine* but a gear joint between two prismatic joints causes this:
Uncaught ReferenceError: Dot is not defined
at GearJoint.solvePositionConstraints (planck.js:9663)
at Solver.solveIsland (planck.js:3533)
at Solver.solveWorld (planck.js:3384)
at World.step (planck.js:4639)
*Only if jointA is the revolute and jointB is the prismatic, not the other way around
Should be simple to reproduce. Thanks for making planck!
Not an issue, but just to let you know that I am working on externs for Haxe (not shure of the best practice to post this) :
https://github.com/NicolasVereenoghe/git.vega.pixi4/tree/master/haxe/lib/planck%2Cjs
It's a first try, and for now the externs are hosted into my framework repository.
I'm going to deal with serious testing the coming days. I hope it will make the job ^^
If you're interested, try it or tell ... I may create another repository for the Haxe externs, for now I'm not sure :)
in README your install instructions are
#### NPM
npm install stage-js --save
#### Bower
bower install stage-js --save
I think you meant to have planck-js
instead of stage-js
Hi guys, Just to let you know typo on the landing page
"Providing a JavaScrip-friendly API"
Good job otherwise :)
Hi,
First thanks for this tremendous work! Awsome. I've tried Matter js, but I like physics in this one far better.
Anyway, I don't know if this is the place to ask (please point me somewere if it isn't), but is it possible to pause simulation, and save everything (bodies, forces, broadband, collision pairs,...). I would then continue simulation but in anytime I could "roll-back" everything to exact state as it was in moment of saving?
Thank you very much in advance!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.