10 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.
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.
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.
