I have seen this game

Fancy Pants 2 . And the thing I noticed apart from the physics and beautiful animations is that the character is moving along slopes , even curved slopes. I have little experience in Box2d, but this concept of curved slopes is new to me. It would be a great help to me If somebody enlightened me on this concept and its implementation. Even If the implementation is not in box2d, it is welcome.


How do you get curves in Box2D?

In the unreleased version of Box2D, there are two new shape types, b2LoopShape and b2EdgeShape, which let you build curves out of a sequence of line segments.

In the released version, you can use multiple small boxes set at angles to get a similar effect, but bodies have a tendency to do unstable and unexpected things near connection points. So I'd recommend using the unreleased version.

How can a character fall down the curve?

Make the character a wheel! My question about platformer physics has some discussion on this, but the basic idea is to mount your character's body on a rotating wheel, and move by applying rotational force to the wheel. Even though (actually, because) the wheel has friction, the downward force from gravity will cause it to rotate down slopes - like a real wheel. By changing the revolute joint's motor parameters, you can make it resist rotation as much as is necessary to avoid it rolling down small inclines.

| improve this answer | |
  • \$\begingroup\$ I agree with you, in using b2LoopShape I could create a slope as my desire. But, on following the link you posted; The problem is, it doesn't lead to a flash port of box2d just a c++ version. 1.) Do you have any suggestions for that? or 2.)Do you know of any other physics engine for flash which allows creation of curved slopes? \$\endgroup\$ – Vishnu Oct 11 '10 at 4:26
  • \$\begingroup\$ I don't think the Flash port of Box2D is up-to-date with the new joint/shape types. I have no idea what physics engines are available for Flash other than Box2D. \$\endgroup\$ – user744 Oct 11 '10 at 8:33
  • \$\begingroup\$ @ Joe : If I ask you, If it would be feasible if I tried writing an AS class for b2LoopShape?So that I could incorporate it into the Box2d Flash api. Where would I have to start heading.? Any clue? \$\endgroup\$ – Vishnu Oct 11 '10 at 8:57

Fancy Pants was made in Flash, and Flash has a specific method called 'hitTest' which allows you to test a pixel (in this case, he probably does 2-3 pixel hitTests along the bottom of the Fancy Pants character) against whatever is drawn within a designated movieclip object. It can be a bit tricky to get it feeling right, but this built-in function is why so many Flash games have collisions with curved slopes. The process is much more complicated for most other engines, and usually has to be done manually, but there are a few different methods.

The method I'm most familiar with is if your engine is tile-based (think Sonic the Hedgehog), the tiles designated as curved could each have a formula which determines the 'Y' collision position based on the character's current 'X' position on that tile. You would have to determine the formula based on how steep of a slope and how curved you're looking for. You could use a graphing calculator or graphing program to experiment with the formulas. You can have multiple tiles with different curves and formulas associated with them this way.

| improve this answer | |
  • \$\begingroup\$ Sonic does not have a curve formula for the tiles. It uses a 2d height-map for each pixel. \$\endgroup\$ – AttackingHobo Oct 8 '10 at 16:18
  • \$\begingroup\$ @AttackingHobo: Can you tell me what is 2dheight map for each pixel or can you point me to a place where I can use that for in my flash games ? \$\endgroup\$ – Vishnu Oct 11 '10 at 4:28
  • 2
    \$\begingroup\$ info.sonicretro.org/Sonic_Physics_Guide Take a look at this. I don't know if this will help you if you use box 2d. You might have to roll your own physics engine. \$\endgroup\$ – AttackingHobo Oct 11 '10 at 5:34
  • \$\begingroup\$ @Attacking Hobo : Is there any advantage or possibility in "doing" curved slopes If i try making an AS 3.0 class that allows loops in the box2d api? \$\endgroup\$ – Vishnu Oct 11 '10 at 8:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.