9 years ago by SirPereira
Hey guys,
I am building an entity that moves up/down/left/right by having forces applied on it.
Currently, I've got that working. However I'm needing that suddenly when changing the direction, the forces that were being applied to the entity should be almost totally replaced by the new ones.
I'm looking for this kind of movement:
www.haxball.com (takes 3 seconds)
My code looks like it merges the newly applied forces with the already running forces and makes the movement not so smooth as the one in the GIF.
Any idea how can I improve it?
I am building an entity that moves up/down/left/right by having forces applied on it.
Currently, I've got that working. However I'm needing that suddenly when changing the direction, the forces that were being applied to the entity should be almost totally replaced by the new ones.
I'm looking for this kind of movement:
www.haxball.com (takes 3 seconds)
My code looks like it merges the newly applied forces with the already running forces and makes the movement not so smooth as the one in the GIF.
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() ); this.body.SetLinearDamping(0); } else if( ig.input.state('down') ) { var force = new Box2D.Common.Math.b2Vec2( 0, this.speed ); this.body.ApplyForce( force, this.body.GetPosition() ); this.body.SetLinearDamping(0); } // 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() ); this.body.SetLinearDamping(0); } else if( ig.input.state('right') ) { var force = new Box2D.Common.Math.b2Vec2( this.speed, 0 ); this.body.ApplyForce( force, this.body.GetPosition() ); 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(); } }); });
Any idea how can I improve it?