Physics Moving Platform / Match velocity does not work well

Hi there,

First-time poster, first-time Phaser user here.

I have a ‘boat’, which is a physics enabled (Arcade) container that contains a “platform” physics-enabled rectangle. Both container and the platform within it have Y gravity at 0 and are immovable (so that the player does not push them).

The boat just sits in the middle of the screen until the player wishes to move it by modifying a ‘power’ variable (done via a button press) - it moves towards the right; like so;

Boat.Update() {

this.body.acceleration.x += this.power;

// "Friction".
this.body.velocity.x *= .7;

// No reversing please!
if(this.body.velocity.x < 0)
	this.body.velocity.x = 0;
}

}

I also have a ‘player’, physics enabled. Platformer style movement pretty much ripped straight from the Phaser3 platform tutorial.

When the player collides with the boat, a flag is set (player ‘Z’ is set to zero when colliding with the boat).

Everything works well, until I start adding acceleration to the boat. No matter what I do, the player tends to slide along the boat’s surface (as the boat moves beneath them).

I’ve tried setting body friction to 1 on both the boat and the player. No effect.

I’ve tried " player.body.velocity.x = boat.body.velocity.x " in an Update method. This almost works; it reduces the slide but nevertheless the slide is still there.
I’ve tried doing that same thing in “Pre/Post” Update methods (after having registered them). No change.

The player movement is handled by the following method, called in Scene Update when appropriate key is pressed.

Player.Move(_dir) {

if(_dir == -1)
	this.body.velocity.x = -160;
else if(_dir == 1)
	this.body.velocity.x = 160;
else
	this.body.velocity.x = this.z == 0 ? this.boat.body.velocity.x : 0;

}

You can see that I’m attempting to set the velocity depending on the flag in that method too. Doesn’t seem to make a difference whether I do that ternary check or just set 0 whenever _dir == 0.

I’ve even tried setting the player’s gravity really high when they’re colliding with the platform;

Player.CollisionVsPlatform(_player, _platform) {

if(_platform.body.moves && _platform.body.touching.up && _player.body.touching.down)
	_player.body.gravity.y = 10000;

}

The method is called but does not appear to do anything.

I’ve uploaded the entire thing here: http://www.whoshotdk.co.uk/phaser/

The ‘boat’, ‘player’ and other scripts are here: http://www.whoshotdk.co.uk/phaser/Asset/GameScene/

The main scene file resides there too, it’s called GameScene.scene.

The game: http://www.whoshotdk.co.uk/phaser/game.html

In order to make the boat move, jump the player up onto it (up arrow), walk into the red box, then left-click the ‘+’ button that appears. You’ll see the boat move and the player slide. Note; if you cant jump high enough, make the window smaller and refresh. I haven’t got round to handling different screen sizes yet!

I’d appreciate any advice on this!

Thanks
Dave

:wave:

I can’t reach the boat. :frowning:

All I can think of is try to do it without containers.

Thanks Samme,

Yes as this is my literal first day with Phaser, I have yet to work out how to handle screen scaling as regards to forces and positions in the game :slight_smile:

If you make the window less tall, you’ll find you can reach it.

No containers eh? I’m only using it so I can organise my entities into some sort of logical ecapsulation - I’d rather not stuff everything into the scene.

Nevertheless I suppose I can give that a go. Thanks!

Edit; I would appreciate an explanation of why not using a container would help, if you have the inclination to do so and the time.

Perhaps this topic can help?

Thanks @Milton!

Unfortuantely for me, I have already seen that article - I’m not actually using tweens but having said that I have tried code that is very similar.

It turns out there is an issue with my use of a container - I’ve just finished a very quick demo with just a couple of sprites - I didn’t need to do anything to make the player follow the moving platform; the Arcade physics just worked. I think I read somewhere that’s expected :smiley:

I’m probably going to remove the container and programatically ‘parent’ my boat entities ( collider rectangle, boat sprite and red box) together instead.

As I’m new I can’t even blame the behaviour on a bug (no one would believe me, including me, hah) but I do intend to test this container behaviour some more soon.

Thanks again!