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

1 decade ago by Jon123

Hi, I have been trying to implement a simple jump action with:

if( ig.input.pressed('jump') ) {
				this.body.ApplyImpulse( new b2.Vec2(0,-100), this.body.GetPosition() );
}

but the entity still jumps if it is in the air, say falling to the ground. How can I tell if the entity is on a surface and therefore able to jump?

Thanks

1 decade ago by ryananger

If my understanding is correct, this.standing (boolean) determines if the entity is on the ground or not.

1 decade ago by Jon123

Thanks, I saw that but simply surrounding the jump code with "if(this.standing)" always returned false :/

1 decade ago by Krisjet

Since you're using box2d Impact's own this.standing code breaks, so you have to roll your own. This is what I did when I was working on my box2d platforming game. Include this in whatever entitiy needs to check if it's standing and call this somewhere in it's update, or right before you check this.standing.

setStanding: function(){
			this.standing = false;
			for( var edge = this.body.m_contactList; edge; edge = edge.next){
				var normal = edge.contact.m_manifold.normal;
				if(normal.y === -1){
					this.standing = true;
				}
			}
			
		},

1 decade ago by Joncom

Alternatively, if you use this Box2D plugin, then
if(ig.input.pressed('jump') && this.standing) { 
    /* then jump */
}

will work just fine.

You could even simplify your code a little bit and just do this:

if(ig.input.pressed('jump') && this.standing) { 
    this.vel.y = -200;
}

1 decade ago by Jon123

@Joncom - Simply using this.standing wasn't working with box2d, @Krisjet had the right idea, works perfectly! Thanks all.

1 decade ago by Joncom

Quote from Jon123
@Joncom - Simply using this.standing wasn&039;t working with box2d, @Krisjet had the right idea, works perfectly! Thanks all.
It's not the plugin you think it is and #this.standing would indeed work.

1 decade ago by Jon123

Oh OK my mistake sorry. I'm new to all of this, but I'll check out your plugin in more detail.
Page 1 of 1
« first « previous next › last »