views:

161

answers:

1

I'm working on a platformer for the iPhone that is using Box2D and cocos2D.

I'm trying to figure out a way to create similar behavior for my controlled character as is found in the Sonic the Hedgehog games, but maintain use of the Box2D physics library as I want projectiles and some level objects to behave according to realistic physics, along with certain situations involving the controlled character (jumping, barreling into objects, etc.).

The main issues that I'm running into are keeping the character on the ground when going over convex curves and maintaining a sense of contact with the ground until a significant distance from it is achieved. I only want the character to react to left and right commands when on the ground, but my shape tends to leave the ground when going over slight curves such that reaction time is delayed briefly until contact us reestablished. Visually there is no separation, but it's big enough to change the state of being on the ground or not. Also, the character does not "hug" bigger curves the way I would like.

I've tried exerting an additional downward force when on the ground, but it doesn't work quickly enough for bigger curves, and large enough forces to be effective interfere with the regular movement of the character. Any thoughts on these issues?

A: 

In my experience, getting true console-like feeling in requires you to create unrealistic physics, for player characters especially.

I believe in box2d you could use impulses to hack the movement system, while still having access to collision detection. It was either that, or making your character a non-moving object, and doing the movement yourself. I forget which seemed to work for me. It's been a few months ;) I believe I also did some nullifying of vectors on the y axis in some cases to fix bugs (slopes?).

If your movement is working properly for the general case, you could make movement when in ground contact with a curve be a special case, where you handle the physics yourself, and only hang out in the real world for collision detection.

Another option is to make most objects use "fake" physics, and use box2d only for collision detection, or for "realistic" objects.

Merlyn Morgan-Graham
Right now I am setting velocity vectors by hand when the player is in contact with the ground. I let box2d take over when the player leaves the ground. The real problem is telling when the player is actually leaving the ground (jumping or falling off a cliff) vs the tiny moments of being off the ground as a result of velocity carrying the player off the curve and making sure that the player follows the curve on around.
rpeck1682
@rpeck1682: I found that controlling the velocity manually didn't seem to have the effect I wanted in certain cases. Impulses might have had the same problem. Slopes were the problem I encountered, because I wanted them to behave exactly the same as normal ground, similar to curves in your case. I came to the conclusion that I'd have to either handle all physics myself while on the ground, or at least all non-flat-ground physics. Not just impulses, but completely handling all updates on the character, and only using box2d for hit/sweep tests. I gave up before trying this out, though...
Merlyn Morgan-Graham
Ok, today I changed things up quite a bit. I started using forces instead of setting the velocity vectors. I disable gravity while the character is on the ground. I apply forces to the x axis only, except for a ground hugging normal force I implemented. I adjusted it so that acceleration is higher when trying to reverse directions, so turning around is fast. I changed my control code so that you can still change directions rapidly unless you explicitely jumped. I also added a roll state that subjects the character to realistic physics. Seems to be working well so far.
rpeck1682
@rpeck1682: if you do "@Merlyn:" before your response, I'll get a message saying you replied to me :)
Merlyn Morgan-Graham
@rpeck1682: That's great to know that you got it working! If you end up putting it up on the net, I'd love to see it working, source or no source.
Merlyn Morgan-Graham
@rpeck1682: Also, if you ended up solving your own problem, you are allowed to answer your own question and accept that answer.
Merlyn Morgan-Graham
@rpeck1682: Sorry for the comment spam :) I also found, from this question's link to box2d docs, http://stackoverflow.com/questions/3226127/move-my-body-to-a-point. If you ever go nuts and decide to implement your own fake physics within box2d, b2_kinematicBody might help.
Merlyn Morgan-Graham
@Merlyn: Thanks for the tips. I completely reworked the control code so that I am evaluating the touches every tick instead of just when a touch moves. That has helped significantly. I'm still having some issues with jumping as I want the player to be able to jump only when touching the ground, but when the player is moving sideways and the ground is sloping downward, the player tends to come on and off of the ground several times while moving down the slope. If the user tries to jump during one of those times, the game thinks that the player is in the air already and doesn't actually jump
rpeck1682