|
Title: Box2D platformer issues Post by: mirosurabu on August 24, 2009, 07:10:19 AM Code: // Movement var vx:Number = body.GetLinearVelocity().x; var vy:Number = body.GetLinearVelocity().y; if (!FlxG.pressed("A") && !FlxG.pressed("LEFT") && !FlxG.pressed("D") && !FlxG.pressed("RIGHT")) body.SetLinearVelocity(new b2Vec2(0.0, vy)); if (FlxG.pressed("D") || FlxG.pressed("RIGHT")) { body.WakeUp(); body.SetLinearVelocity(new b2Vec2(1.5, vy)); } if (FlxG.pressed("A") || FlxG.pressed("LEFT")) { body.WakeUp(); body.SetLinearVelocity(new b2Vec2(-1.5, vy)); } if ((FlxG.pressed("W") || FlxG.pressed("UP")) && (_contactListener.can_jump()) ) { body.WakeUp(); body.SetLinearVelocity(new b2Vec2(vx, -4.0)); FlxG.play(SndJump); } So, this all works nicely. When you press right the x-velocity of the body is set to a positive value. When you press left the x-velocity of the body is set to a negative value. When left and right are not pressed the x-velocity is set to zero. When you press up the y-velocity of the body is set to a negative value. This all works fine except one thing. You can get stuck in another object when in the air and pushing into that object (left or right). So when I jump and hold left to jump to the left if I hit the front side of an object while I'm still in the air and still holding the left button down the character will not continue falling down and instead will get stuck in that Y-position. I don't want this. I want my character to continue falling down when I keep holding the direction button down and when the character is colliding with the front side of an object. I thought about using front sensors (left and right) but this won't work because I need my character to be able to push other objects. Any way to solve this? Title: Re: Box2D platformer issues Post by: Draknek on August 24, 2009, 07:28:57 AM Have you tried disabling friction for the player object?
Title: Re: Box2D platformer issues Post by: mirosurabu on August 24, 2009, 07:39:44 AM Ah, that change solved all of my problems. (not sure why did I set friction to 1.0) Thanks! :)
But, there's another issue and it's related to contactListener and animation. Basically, I use contactListener to check if the player object is on the ground or not. The problem with this is that when player object walks on top of movable objects (density>0.0) contactListener generates false negatives at relatively random intervals. This means that there are negatives when ground sensor is clearly overlapping other b2bodies. This in turn gives some nasty animation results. Here is the contactListener code: Code: public class b2ContactListener { private var canjump:Boolean = false; /// Called when a contact point is added. This includes the geometry /// and the forces. public function can_jump():Boolean { return (canjump); } public virtual function Add(point:b2ContactPoint):void { } /// Called when a contact point persists. This includes the geometry /// and the forces. public virtual function Persist(point:b2ContactPoint):void { if (point.shape1.GetUserData() == "groundsensor" || point.shape2.GetUserData() == "groundsensor") { canjump = true; } } /// Called when a contact point is removed. This includes the last /// computed geometry and forces. public virtual function Remove(point:b2ContactPoint):void { if (point.shape1.GetUserData() == "groundsensor" || point.shape2.GetUserData() == "groundsensor") { canjump = false; } } /// Called after a contact point is solved. public virtual function Result(point:b2ContactResult):void { } } Thanks for every bit of help! Title: Re: Box2D platformer issues Post by: Draknek on August 24, 2009, 08:06:39 AM My best guess would be that there are multiple contact points detected simultaneously, and you're setting canJump = false when any one of them is removed (even if there are others which are still there).
Title: Re: Box2D platformer issues Post by: mirosurabu on August 24, 2009, 08:25:26 AM Sir, you are speaking the truth. Solved! Thank you very much! :)
Title: Re: Box2D platformer issues Post by: weasello on August 24, 2009, 08:36:15 AM If you want to go with a bit more of a "realistic" feel, you might want to try applying a Force or an Impulse on the character when he moves left or right, with a cap on it for a top speed. Totally up to you though, what you are doing is legitimate.
Also, the best way to figure if you are touching the ground or not is to make a "Contacts":Number variable in your UserData for the object. Any time ContactListener's ADD routine is called, increment the number ++. Anytime REMOVE is called, decrement the number --. If the number > 0, then you are on the ground. :) Sometimes when walking over rough terrain or up/down slopes, you can run into issues when your character is technically in the air - even if only a tenth of a pixel. To avoid this, create a "feet" box on the bottom of your character (size to taste) with .isSensor=true set on it. Use this for your on-the-ground contactListener. I have a similar contact listener on my players left and right side which keeps track of what he's touching there, to allow things like walljumping. :) I have all these things on display in working action at www.protonaut.net (early alpha still, shhh) if you want to see. :) Title: Re: Box2D platformer issues Post by: mirosurabu on September 02, 2009, 09:50:01 AM Thanks, that's the way I implemented the ground sensor.
But I have a new problem! I have a body consisting of one sensor shape. The position of this body is changed via SetXForm(). The problem I have with this sensor body is that contacts with zero density shapes are not reported. (i've placed trace statement inside Add method of contact listener classoutput) I can't find solution to this problem. Title: Re: Box2D platformer issues Post by: Ivan on September 02, 2009, 10:07:57 AM Box2D annoyingly doesn't calculate collisions between static bodies (0 density bodies are technically static). What I did in my engine was basically implement a hack where i have shapes manually marked as collision only and for those shapes, they are normal physics shapes in Box2D, but their transforms are reset to my own transforms at every frame. That way i can move and rotate them at will without any physics, but still get box2d's collision detection.
Title: Re: Box2D platformer issues Post by: weasello on September 08, 2009, 10:51:42 PM I just made the density of my sensor shape 0.0000001. So small it doesn't affect anything.
Title: Re: Box2D platformer issues Post by: mirosurabu on September 27, 2009, 12:50:12 PM The fact that Box2D can't assign velocity to sensor bodies and the fact that it can't test between two zero-density objects makes me angry.
Title: Re: Box2D platformer issues Post by: Glaiel-Gamer on September 27, 2009, 12:55:04 PM The fact that Box2D can't assign velocity to sensor bodies and the fact that it can't test between two zero-density objects makes me angry. you have the source code, go hack it in if you need it to Title: Re: Box2D platformer issues Post by: mirosurabu on September 27, 2009, 02:35:40 PM That would be awesome if I only knew how to.
One more question: |