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:
on direction key (right or left) down:
on direction key (right or left) up:
in the simulator's step method:
on contact with the ground (via b2ContactListener):
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:

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'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
- 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
- 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
- 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.
- 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
- 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 [ 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. 😉