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 enpu

Im trying to make Box2D platformer game, but im not sure how i can check when player can jump or not (when its touching ground).

method im using now, is that when players velocity.y is 0, then it can jump.
but its not working very well.

any better ideas?

1 decade ago by MobileMark

check the collision on the y axis and put a boolean toggle in it. If the bool is 1 (hes on the ground) let him jump, if not he can't.

If that's too vague let me know, i'll add sample code

1 decade ago by enpu

please add some sample code :)

1 decade ago by MobileMark

Add var jumpbool=1; in a script tag in the index.html (assuming he can jump initially)

then in the update function add a check with an if function that makes sure the jumpbool is 1 when the button to jump is pressed

 if( ig.input.pressed('jump') && jumpbool==1 ) 
          { the code you call to jump

              jumpbool=0;

                      // this will turn off jumpbool so the player
                    // can't jump again until it  has a Y collision
           }

handleMovementTrace: function( res ) {
		this.parent( res );
		
		// collision with the floor, can jump again
		if( res.collision.y ) { 
                jumpbool=1;
			
		}
	}, 

You'll also have to put in some code to check to see if the Y collision is on the top or bottom of the entity, but hopefully this will get you started.

1 decade ago by enpu

hmm i think thats not working with box2d?

1 decade ago by MobileMark

How are you calling the jump function?

1 decade ago by enpu

i mean isn't the collision detection different when using box2d?

in player.js there is a line:
collides: ig.Entity.COLLIDES.NEVER, // Collision is already handled by Box2D!

1 decade ago by MobileMark

Well thats a good point hah. Sorry, I forgot box2D completely takes out impacts collision. I'll be working with box2D more tonight and i'll let you know if I find a solution.
Sorry about that

1 decade ago by enpu

heh thats okey! i hope you find a solution :)

1 decade ago by enpu

http://www.emanueleferonato.com/2009/07/31/platform-engine-using-box2d-step-3/

There is a box2d platform engine written in ActionScript 3.
It uses some kind of ground sensor, but im not sure how to make that
work on Impact.

1 decade ago by MobileMark

have you tried turning player.js
ig.Entity.COLLIDES.NEVER,

to passive and checking the handler then? Not sure if that'd work or break everything in a glorious code malfunction

1 decade ago by MobileMark

Have you looked into b2ContactListener? It seems to catch contact or collision instances.

you can read more here: http://www.box2dflash.org/docs/2.0.2/manual#Contact_Listener

1 decade ago by enpu

COLLIDES.PASSIVE is not working.

And im not sure how to use that b2ContactListener on JavaScript/Impact.

1 decade ago by dominic

If you use Box2D, you should completely disable Impact&039;s own collision system with #ig.Entity.COLLIDES.NEVER, otherwise Box2D and Impact will interfere with each other.


I haven&039;t tried it, but I think you can create your own #b2ContactListener in JavaScript like so:

var MyContactListener = new b2.ContactListener();
MyContactListener.prototype.Add = function( point ) {
    /* do something */
};

MyContactListener.prototype.Persist = function( point ) {
    /* do something */
};

…

1 decade ago by enpu

i tried that but got error:

Uncaught TypeError: Cannot set property 'Add' of undefined

1 decade ago by dominic

Whops, sorry. Try
var MyContactListener = function(){};
MyContactListener.prototype = new b2.ContactListener();

1 decade ago by enpu

i cant get it to work :/

1 decade ago by MobileMark

I thought of a sort of lame hack. You could set a trigger function to reset the boolean value so the player can jump ( jumpbool ), then place that trigger along the ground (or any entity that the main character can touch and regain the ability to jump)

I've been busy the last couple of days so I haven't got to play with what Dominic suggested, but I'll let you know if I have any breakthrough.

1 decade ago by MobileMark

I've played around with this for about 2 hours and I still can't get it to work -_-

b2.ContactListener has to be the answer but so far even using Doms snippet I get a lot of Undefined errors. I'll keep working with it though

1 decade ago by maryrosecook

I got it working. There's an extra step. As Dominic said, you do:

var MyContactListener = function(){};
MyContactListener.prototype = new b2.ContactListener();
MyContactListener.prototype.Add = function( point ) {
  console.log("contact started")
};
// other contact event callbacks could be added as well

But, then, you need to create a contact listener and set it on the world:

ig.world.SetContactListener(new MyContactListener());

(I did this last step after loading the level, though there is no particular reason it needs to go there.)

1 decade ago by Ashkettle

This is pretty cool. My next game after the one I'm currently on will be a physics platformer.
Page 1 of 1
« first « previous next › last »