melonjs / melonJS

a fresh, modern & lightweight HTML5 game engine
https://melonjs.org
MIT License
5.93k stars 643 forks source link

collision not working correctly #1088

Closed dynamo-foundation closed 2 years ago

dynamo-foundation commented 2 years ago

If you are submitting a bug report, please respect the below template :

OS platform / Browser

any

melonJS version

10

Bug description

Object 1: this.body.collisionType = me.collision.types.WORLD_SHAPE; this.body.setStatic(true); this.body.setMaxVelocity(0, 0);

Object 2: this.body.collisionType = me.collision.types.PLAYER_OBJECT; this.body.setFriction(1, 1); this.body.setMaxVelocity(0, 0);

When Object1 touches Object2, Object1 and Object2 both move radomly, sometimes get connected, fly off the screen, etc

Steps to reproduce the bug

Create a level and add two Sprites dynamically with the above settings. Put the sprites next to each other or add a controller to sprite2 so it can be moved by player. Object 2 will not be blocked by Object 1.

Console log Extract

to show possible exception/errors raised by the engine or the browser

How to enable/use the console log
obiot commented 2 years ago

collision is working properly, but what is not fully implemented in melonJS is proper automatic collision response.

each physic entities have to implement the onCollision callback and "specify" the behaviour wrt to other entities : http://melonjs.github.io/melonJS/docs/me.Renderable.html#onCollision

see as well the platformer example : https://github.com/melonjs/examples/blob/2e0adbea99ed53ee2e4211cd350835af2afdbe59/platformer/js/entities/player.js#L137

obiot commented 2 years ago

actually my ultimate goal for version 10 was to replace our own implementation by something like matter, I did not happen (lack of time) but lots of refactoring was done internally to at least help such a transition.

dynamo-foundation commented 2 years ago

What is the purpose of setStatic? If the purpose is to disable all physics (as claimed in the documentation), then that is not working.

My use case involves a player Sprite and an NPC sprite - I would like the Player sprite to not be able to pass the NPC sprite - e.g. they should collide and neither should move - similar to how the level collision boxes work.

obiot commented 2 years ago

https://github.com/melonjs/melonJS/blob/6816d975a88b9b179e4a75de4e08a08a92384db5/src/physics/world.js#L165-L178

funnily melonJS just don't do anything if the physic body is static, I guess what we have here is that the player entity is actually pushing the NPC ? what is your implementation of the player ?

obiot commented 2 years ago

probably we need a similar check here : https://github.com/melonjs/melonJS/blob/6c6fd5f8a5bfa5089e2f10070b40d172d455fee8/src/physics/detector.js#L144-L148

obiot commented 2 years ago

this last commit should fix the static bodies flying over the screen ! :)

I also updated the distribution files, so it's maybe easier to test with the latest version for you ?