Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

8 years ago by SirPereira

I am using Box2D modification from @Joncom (thanks!), and I'm looking to a way that would allow me move my entity when I press some key in a smooth way, by slowly accelerating it it and letting it slowly stopping by when I stop pressing anything.

http://answers.unity3d.com/questions/284487/slowly-accelerate-and-slowly-slow-down-while-movin.html

This is a question that basically explains what I am looking for. Does anyone have this snippet in JavaScript or a similar approach?

This is my current code, however my circle entity keeps moving (seems like forever?) after I've stopped pressing any key.

ig.module(
	'game.entities.player'
)
.requires(
	'plugins.joncom.box2d.entity',
	'plugins.joncom.box2d.entities.circle'
)
.defines(function(){

EntityPlayer = EntityCircle.extend({
	
	size: {x: 82, y: 82},

	radius: 41,
	density: 1,

	bounciness: 0.025,

	speed: 1300,
    maxVel: {x: 200, y: 200},

	animSheet: new ig.AnimationSheet( 'media/player.png', 82, 82 ),

	init: function( x, y, settings ) {
		this.parent( x, y, settings );

		this.addAnim( 'idle', 1, [0] );
		this.addAnim( 'shoot', 1, [1] );

		// Set a reference to the player on the game instance
		ig.game.player = this;
	},
	
	update: function(){

        if( ig.input.state('up') ) {

	        var force = new Box2D.Common.Math.b2Vec2( 0, -this.speed );
	        this.body.ApplyForce( force, this.body.GetPosition() );

        } else if( ig.input.state('down') ) {

	        var force = new Box2D.Common.Math.b2Vec2( 0, this.speed );
	        this.body.ApplyForce( force, this.body.GetPosition() );

        }
        
        // Left or right?
        if( ig.input.state('left') ) {

	        var force = new Box2D.Common.Math.b2Vec2( -this.speed, 0 );
	        this.body.ApplyForce( force, this.body.GetPosition() );

        } else if( ig.input.state('right') ) {

	        var force = new Box2D.Common.Math.b2Vec2( this.speed, 0 );
	        this.body.ApplyForce( force, this.body.GetPosition() );

        }

        if ( ig.input.state('shoot') ) {
        	this.currentAnim = this.anims.shoot;
        } else {
        	this.currentAnim = this.anims.idle;        	
        }

		this.parent();

	}

});


});

I'm looking for a similar movement that is implemented in the online game Haxball (www.haxball.com), but I was not been able to achieve it yet with this code.

8 years ago by Joncom

Untested, but maybe try:

ig.game.player.body.SetLinearDamping(5);

Someone suggested it here.

8 years ago by SirPereira

Hey @Joncom, thanks for you suggestion!

It kinda worked, however right now the body does not get any acceleration and it is too slow to move. I kinda needed it to start slow -> gain acceleration as I keep pressing -> lose acceleration when I stop pressing and slowly stops.

Regarding this topic: http://www.box2d.org/forum/viewtopic.php?f=8&t=4517
I guess I should use clearForces() to achieve that (instead of SetLinearDamping).

However, from what I've seen on a previous topic here - http://impactjs.com/forums/help/clearforces-doesnt-exists-on-the-box2d-plugin - ClearForces exists on original ImpactJS Box2D plugin but not in yours. Is that correct?

If so, how may I access ClearForces in your plugin?

And after having access to ClearForces, where am I supposed to implement it?
As update() method runs all the time, if I keep calling it I suspect it would always reset any force I would apply by pressing a key. What would be the correct logic to be used here?

Thanks and congrats for the great work @Joncom!

EDIT: I've found out that for the correct way to call ClearForces (independently of the plugin, guess it works in both) is

ig.world.ClearForces();

However, just as I've thought calling it in update() would make my entity not applying any force at all. So what logic am I missing here?

8 years ago by SirPereira

I guess I've achieved it! Thanks for your precious help @Joncom!

Basically, now I'm just applying the LinearDamping if I'm not pressing any control keys.

However the movement is not perfect, I'm going to create another topic because it's a different issue.

The full code for anyone struggling with the same problem:

ig.module(
	'game.entities.player'
)
.requires(
	'plugins.joncom.box2d.entity',
	'plugins.joncom.box2d.entities.circle'
)
.defines(function(){

EntityPlayer = EntityCircle.extend({
	
	size: {x: 82, y: 82},

	radius: 41,
	density: 1,
	isFixedRotation: true,

	bounciness: 0.025,

	speed: 1300,
    maxVel: {x: 200, y: 200},

	animSheet: new ig.AnimationSheet( 'media/player.png', 82, 82 ),

	init: function( x, y, settings ) {
		this.parent( x, y, settings );

		this.addAnim( 'idle', 1, [0] );
		this.addAnim( 'shoot', 1, [1] );

		// Set a reference to the player on the game instance
		ig.game.player = this;
	},
	
	update: function(){

        if( ig.input.state('up') ) {

	        var force = new Box2D.Common.Math.b2Vec2( 0, -this.speed );
	        this.body.ApplyForce( force, this.body.GetPosition() );

        } else if( ig.input.state('down') ) {

	        var force = new Box2D.Common.Math.b2Vec2( 0, this.speed );
	        this.body.ApplyForce( force, this.body.GetPosition() );

        }

        // Left or right?
        if( ig.input.state('left') ) {

	        var force = new Box2D.Common.Math.b2Vec2( -this.speed, 0 );
	        this.body.ApplyForce( force, this.body.GetPosition() );

        } else if( ig.input.state('right') ) {

	        var force = new Box2D.Common.Math.b2Vec2( this.speed, 0 );
	        this.body.ApplyForce( force, this.body.GetPosition() );

        }

        if ( ig.input.state('up') ||
        	 ig.input.state('down') ||
        	 ig.input.state('left') ||
        	 ig.input.state('right')
    	){
			this.body.SetLinearDamping(0);
        }

        if ( !ig.input.state('up') &&
        	 !ig.input.state('down') &&
        	 !ig.input.state('left') &&
        	 !ig.input.state('right')
    	){
			this.body.SetLinearDamping(3);
        }

        if ( ig.input.state('shoot') ) {
        	this.currentAnim = this.anims.shoot;
        } else {
        	this.currentAnim = this.anims.idle;        	
        }


		this.parent();

	}

});


});

8 years ago by Joncom

Glad you got it sorta figured out. And thanks for sharing your solution!
Page 1 of 1
« first « previous next › last »