rickbross Posted March 21, 2015 Share Posted March 21, 2015 So I am just working on a demo for a game that I still be starting soon. I just found out about Box2D today and I was wondering if anyone knows how I would jump a sprite. Currently, I can press the up key and I can infinitely soar into the sky. I have gravity working fine. I have seen people use a "jump timer". I understand the concept, but do not understand how to code this up. Also, I have failed at all attempts to check in my "man" is touching the ground. What is the best way to do this? Here is my update code: function update() { man.body.setZeroVelocity(); man.body.fixedRotation = true; if (cursors.left.isDown) { man.scale.x = -.2; man.body.moveLeft(400); man.animations.play('run', 10, true); } else if (cursors.right.isDown) { man.scale.x = .2; man.body.moveRight(400); man.animations.play('run', 10, true); } if (cursors.up.isDown) { man.body.velocity.y = -750; } else if (cursors.down.isDown) { man.body.moveDown(400); } if (!cursors.left.isDown && !cursors.right.isDown) { man.animations.stop(); man.frameName = "shooting1.png"; }; }And Here is what it looks like: Link to comment Share on other sites More sharing options...
rickbross Posted March 22, 2015 Author Share Posted March 22, 2015 bump Link to comment Share on other sites More sharing options...
GBeebe Posted March 23, 2015 Share Posted March 23, 2015 maybe this will help? http://www.iforce2d.net/b2dtut/forces I found that doing this:phyPlayer.ApplyImpulse( {'x' : 0, 'y' : -2}, phyPlayer.GetWorldCenter() );Works pretty well. phyPlayer is the body returned by world.CreateBody(myBodyDef); Now, could you tell me how you made your lines? smatthews1999 1 Link to comment Share on other sites More sharing options...
rickbross Posted March 23, 2015 Author Share Posted March 23, 2015 So instead of:man.body.velocity.y = -750;I should do:man.ApplyImpulse( {'x' : 0, 'y' : -2}, phyPlayer.GetWorldCenter() );?? Link to comment Share on other sites More sharing options...
rickbross Posted March 23, 2015 Author Share Posted March 23, 2015 How can I determine whether or not the player is currently touching the ground? Link to comment Share on other sites More sharing options...
GBeebe Posted March 23, 2015 Share Posted March 23, 2015 This is my collision listener so far, you can play with it. It tells you what 2 bodies collided. In my code, it ignores any collision with a body that doesn't have a userData set. userData is set in the bodyDef, box2d doesn't care what it is: object, number, string, etc. I'm currently assigning it a string ex "player" or "death" <- object that instantly kills player. I don't assign one to solid ground, because there's currently no need for me to do anything when my player touches solid ground 'cause box2d takes care of what I need. var listener = new b2d.b2ContactListener;//console.log(world); listener.BeginContact = function(contact) { var a = contact.GetFixtureA().GetBody().GetUserData() var b = contact.GetFixtureB().GetBody().GetUserData(); //if we have assigned userData to both bodies, it's important enough to check into if (a && { console.log("Collision between: " + a + " & " + ; } } listener.EndContact = function(contact) { //console.log(contact.GetFixtureA().GetBody().GetUserData()); } listener.PostSolve = function(contact, impulse) { } listener.PreSolve = function(contact, oldManifold) { }Also, there's a b2Body.isAwake() function that will return false if the b2Body is "resting", but I haven't gotten into that yet. Much like yourself, I have just started playing with box2d. Sorry if this reply seems too vague for your answer, I'm currently on my lunch break edit: If you want to see if the player touched anything at all, put this in listener.BeginContact.if (a == 'player' || b == 'player') { //do what ever you need there} Link to comment Share on other sites More sharing options...
GBeebe Posted March 24, 2015 Share Posted March 24, 2015 A note on setting the velocity directly: -750 is a pretty high number, if you insist on doing it that way try adjusting it down to something like -100. Link to comment Share on other sites More sharing options...
Recommended Posts