Jump to content

Collision, slopes and velocity


psy_
 Share

Recommended Posts

Hello everybody !

I'm trying to build some slope and non-regular tiles but i have a lot of problem with collision. The main problem is that when I use moveRight()/moveLeft() on a slope (in this case, just a simple body) and I stop at the middle of the tile, my player just jump like that : 

out.gif.3ee0620ad3bc8cb691844e6a043468b9

On the other case, when I from the top of the tile to the left, my player just fly in the air and slowly land. I tried to tweak a bit the material by setting friction and removed restitution but it don't really change. Maybe it could work if I put a huge amount of gravity but then it will influence other part of the game (like jumping). I have also tried to add a sensor (simple circle) just below the sprite so I could set the x/y velocity at zero when the sensor is in contact with the ground. This is working well but it's not working at all when i'm trying to go down the slope. Basically it moves one px left, set velocity to 0, fall one px (cause of the gravity). move one px left and etc...

This is the code if you want to reproduce the problem (I'm stuck with this for many days D:) :

create() {
      this.playerMaterial = this.game.physics.p2.createMaterial()
      this.tileMaterial = this.game.physics.p2.createMaterial()

      this.game.physics.startSystem(Phaser.Physics.P2JS);
      this.game.physics.p2.gravity.y = 400

      this.player = this.game.add.sprite(40,40, 'player', 1)
      this.tile = this.game.add.sprite(80, 272, 'player')
      this.tile.visible = false
      this.game.physics.p2.enable([this.player, this.tile])

      this.player.body.clearShapes()
      this.player.body.addRectangle(0, 40, 0, 0)
      this.player.body.debug = true
      this.player.body.fixedRotation = true
      this.player.body.setMaterial(this.playerMaterial)

      this.player.body.mass = 1.5

      this.tile.body.clearShapes()
      this.tile.body.addPolygon( {} , 0,29,  0,40  ,  50, 40  ,  50, 0  )
      this.tile.body.debug = true
      this.tile.body.static = true
      this.tile.body.setMaterial(this.tileMaterial)

      this.cursors = this.game.input.keyboard.createCursorKeys()
      this.jumpButton = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR)

      let contactMaterial = this.game.physics.p2.createContactMaterial(this.playerMaterial, this.tileMaterial)
      contactMaterial.friction = 1
      contactMaterial.restitution = 0
      contactMaterial.frictionRelaxation = 3;  
      contactMaterial.surfaceVelocity = 0
      contactMaterial.frictionStiffness = 1e7; 
      contactMaterial.stiffness = 1e7;
}

update() {
      if (this.cursors.left.isDown) {
         this.player.body.moveLeft(200);
      } else if (this.cursors.right.isDown) {
         this.player.body.moveRight(200);
      } else {
         this.player.body.velocity.x = 0;
      }
       
      if (this.jumpButton.isDown) {
         this.player.body.moveUp(100);
      }

}

So, any idea how can I resolve this problem ? I'm a bit lost.

Thanks !
 

Link to comment
Share on other sites

12 minutes ago, psy_ said:

Thanks for your non-help.

I help devs by buying their games actually. It's far better imo.

Anyway, I'm not here to start a discussion, your post has nothing to do here.

You are welcome, any time.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...