Jumping: Best Practice?

Hi,

I'm writing a jump and run game and I'm wondering what the best (or simply: working!) method to control jumping behavior is.

However, the behavior I try to achieve is not really trivial: I try to develop a double jump with rotation. Once the jump key is pressed, a simple force is applied to the character and it moves upwards. If the player presses the jump key again while the character is still in the air, a less strong force is applied and the Blobby (I'm calling the character Blobby) starts to rotate (clockwise if the character is jumping to the right, ccw otherwise).

That's how I am doing it so far:
A Blobby has 4 flags: isOnGround (is the Blobby really on the ground?), isJumping (is the Blobby jumping? falling is not jumping!), isRotating (is the character rotating?), isWalking (is it walking?). There are also variables for the angle of the rotation and the direction.
Algorithm:
on jump key pressed:
Code:
- Is the Blobby not jumping?
    - Is it on ground?
       - apply force
       - set isJumping = true
       - set isOnGround = false
- Or is it jumping and not rotating?
    - apply (less strong) force
    - set isRotating = true

on direction key (right or left) down:
Code:
- set direction accordingly
- set isWalking = true

on direction key (right or left) up:
Code:
- is it walking and on ground?
    - set the x value of the linear velocity to 0 (stop it)
    - apply a low force the make it smooth

in the simulator's step method:
Code:
- is it jumping and rotating?
    - is the angle = 360?
       - set angle to 0
       - set isJumping = false
       - set isRotating = false
    - increase angle (for me its +4)
- or
    - set angle = 0
- is it walking and the [i]direction[/i] is set?
    - set linear velocity for this direction.

on contact with the ground (via b2ContactListener):
Code:
- on contact begin:
    - set isOnGround = true
    - set isJumping = false
    - set isRotating = false
- on contact end:
    - set isOnGround = false

I've implemented this algorithm, but I thought it would be easier in plain text than in code (but if you want the code, here it is: http://nopaste.visusnet.de/aaa94a16143d ... 8ac81fec8c ... I've wrapped the contact listener (and other listeners in the system) into a generic publish/subscribe pattern)

Long story, but here are some problems I am facing now:
- There is no difference between walls and ground objects. If we hit a wall, the behavior is wrong (you are unable to jump again, etc.)
- If I add an edge shape as a sensor to the bottom of the character which is not as width as the character's widest shape, I should theoretically be able to react only to the correct contact events, but since the shapes (wall/ground and character) will intersect and raise a contact event and the Blobby is relatively small, the edge shape nearly always causes contact events. => pretty useless.
- The character consists of two circle shapes whereas the lower circle is a bit larger then the upper one. I would like to get the point on the circle which is tangent to the wall/ground. This contact point would allow me to define an angle within the circle to distinguish between ground and wall (the real question is not ground or wall, but is jumping allowed or not). Confusing? Yes... This image might help you:
Attachment:
angularareas.png
angularareas.png [ 4.81 KiB | Viewed 156 times ]


My question(s) is(/are): How are you doing it? Do you have any suggestions for me? Am I doing something terribly wrong?

P.S.: Yes, it has something to do with Blobby Volley and yes, it will be a violent jump and run. 😉
I didn't get why the approach of adding a sensor to the bottom of the player would be useless - I am doing that and it works quite nicely. Bear in mind that since the sensor can touch more than one thing at once you can't just have a boolean variable in your player to say whether the foot sensor is touching something. You will need to store a list of all fixtures the sensor has touched as given to you by BeginContact, and only remove a fixture from that list when you are told by EndContact that it has gone out of the sensor. Then all you need to do is check whether this list is empty or not to know if you are standing on something. And if you want to know what you are standing on, you can always look at the actual contents of the list.
It is not a problem to detect whether the shape hits a ground, but to detect the contact point relative to center of the circle shape. I have to distinguish between "is walking against a wall" and "is walking on ground". Grounds and walls are basically the same, but differ in their angle. I am applying a linear velocity to the character if the player presses one of the direction keys. If the character is in the air and the player moves the character towards a wall (and keeps pressing the key into this direction), the character sticks to the wall. I want to disable the velocity if the Blobby hits a wall (but not on ground). That's why I had the idea with the ground and wall contact angle (see angularareas.png in my previous post). Unfortunately, the b2Manifold I get for every contact event contains only the contact point relative to the center of the ground. If I could get the contact point relative to the center of the circle, I would be able to calculate an angle:
Attachment:
angledesc.png
angledesc.png [ 11.75 KiB | Viewed 120 times ]
So just make your sensor the green part in your picture - but sticking out a bit from the actual player body of course.
Is it even possible to make a segment of a circle a sensor? I just found out how I can calculate the desired angle and it works fine, but is relatively complex. The only problem which is left, is that the character rotates on a double jump... I think I'll find a solution for this, too.

But, if there is way to create a sensor which is just a segment of a circle, it would simplify many things.

FYI, the updated code: http://nopaste.visusnet.de/518f63560545 ... ff8f053f19

P.S.: As you'll see, I've used your suggestion of collecting contact points in a list. Works pretty well.
No, but you could make a polygon which would fit fairly good.
Can u show me your "contact point" class plz. How can u call contact point->getangle ?
These classes could help you:
ContactListener.cpp
ContactListener.h
PublishSubscribe.cpp
PublishSubscribe.h
Blobby.cpp
Blobby.h
BlobbyFactory.cpp
BlobbyFactory.h

ContactPoint is defined in Blobby.h.

Original thread: https://box2d.org/forum/viewtopic.php?t=5813