Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kinematic bodies #498

Open
getkey opened this issue Oct 4, 2017 · 13 comments
Open

Kinematic bodies #498

getkey opened this issue Oct 4, 2017 · 13 comments

Comments

@getkey
Copy link

getkey commented Oct 4, 2017

I was wondering if kinematic bodies are supported. From my first look at the documentation it seems they are not but perhaps there is a another way to achieve this?

@liabru
Copy link
Owner

liabru commented Oct 5, 2017

You mean like in box2d? Not currently a feature, but an interesting request. What are the use cases? Platform games?

@getkey
Copy link
Author

getkey commented Oct 5, 2017

Yes, exactly!

@liabru
Copy link
Owner

liabru commented Oct 5, 2017

Cool, for now the closest thing is probably the manipulation demo, check it out.

@getkey
Copy link
Author

getkey commented Oct 5, 2017

Thanks, it seems to solve the issue, just being very slightly more cumbersome to use but I can live with that.

What would happen in the following scenario though, if the dynamic body is falling very fast? Will it go through the pseudo-kinematic body?
what_happens

@liabru
Copy link
Owner

liabru commented Oct 6, 2017

Check out #5 which I think relates to what you mean.

Just a thought, maybe try this for kinematic bodies:

Body.setMass(body, Infinity);
Body.setInertia(body, Infinity);

@getkey
Copy link
Author

getkey commented Oct 6, 2017

Yeah, so as I see in any case it might happen. ^^

Is setMass and setInertia necessary though, given that my body is static.

@TheColorRed
Copy link

If I am not mistaken a kinematic body is one that is not affected by outside forces (and gravity), however it is movable like a moving platform.

In programs such as Unity3d, you can change the x,y position of the kinematic body, for something such as a player and it will still test for collisions of other bodies, so if you run into a wall, you can't pass through it.

Basically it seems as if a kinematic body is something that is only used for collision and trigger detection, everthing else is controlled outside of the body. Would that be a fair assessment?

@lostfictions
Copy link

@TheColorRed that's not quite correct -- a kinematic body in Unity will happily pass through a wall by default, unless you add behaviour for it not to. (For example, check out the somewhat-complicated userland CharacterController2D.) That's the main benefit of kinematic bodies -- you can carefully control their behaviour. (They will collide with and push away dynamic bodies, though.)

@jeroenheijmans
Copy link

Got referenced to this issue from gamedev.stackexchange.com (I'm using MatterJS via Phaser) for a very similar use case (moving a rectangle around as a player, but otherwise treating it as static). Figured I'd link back from here to that Q&A.

@liabru
Copy link
Owner

liabru commented Mar 15, 2023

The manipulation example is still currently the reference for this topic, though there's a new argument updateVelocity for Matter.Body.setPosition which I think can make it a bit easier.

@karolsluszniak
Copy link

karolsluszniak commented Jul 12, 2023

Correct me if I'm wrong but the manipulation example uses isStatic combined with manual position/velocity updates. This indeed makes the body "rock solid" when it comes to collisions (a primary trait of kinematic bodies), but there's one problem that rules it out as a full replacement for the kinematic type - static bodies don't detect collisions with each other. So if I'd like to use this approach for player-controlled characters in a multiplayer game (whose motion is completely scripted and not a subject for collisions) I'm out of luck.

To summarize the permutations of these properties:

Body Type Affects Others Affected by Others Detects Collisions
Dynamic
Static
Sensor
Kinematic (missing)

This shows how currently in some kinematic use cases one has to pick either ability to affect others (by choosing sensor) or ability to detect collisions between such pseudo-kinematic bodies (by choosing static body).

Please correct me if I've missed sth.

@lostfictions
Copy link

lostfictions commented Jul 12, 2023

@karolsluszniak I made a minimal (check the PR's diff) proposal for a first step to resolve this in #877 several years ago with a long write-up for context, but afaict nobody ever looked at it or gave feedback. I'd additionally written up some further notes in #875, which also went ignored.

To be honest it discouraged me from looking at Matter again that these went completely ignored -- I was hoping a better-maintained fork would emerge that resolves some of these years-long major missing pieces. At this point I'd suggest you either patch in fixes you need yourself (via patch-package or your package manager's built-in equivalent functionality)... or simply opt for a different library if you need this kind of thing. It's a shame, because I think Matter is generally well-architected, readable, and easy to use.

@karolsluszniak
Copy link

@lostfictions Thank you for the response. Your description in #877 is very comprehensive and informative, definitely beating my own attempt above in clarity (and volume which is news to me ;P). The problem is definitely out there and it does put a question mark over Matter.js for gamedev.

I'm in the process of transitioning Heyplay.io - my indie IO gamedev platform - to TS/JS scripting and I was considering Matter for 2D physics (currently just viewport querying + collision detection w/ event triggering of many circular bodies across large game area) - something that until now was handled by my own code but I'd like to extend to different body shapes and actual collision feedback. Matter has all that + meets my other requirements: doesn't rely on specific world size (like e.g. box2d does), allows viewport queries and has support for collision masking + exclusive grouping, and the API is very clear. My options now will be hacking Matter the way you did in that PR or looking for sth else (e.g. Rapier2d WASM has caught my attention, I'll very likely be experimenting and checking how the memory usage + CPU usage looks for my case in Matter and other libs). Any suggestions are appreciated. Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants