Side-scrolling game with Box2D (physics question)

Started by
4 comments, last by TobiO 15 years, 11 months ago
Hello, I'm currently developing a simple side-scrolling game in outer space. You control an astronaut and you have to collect all sorts of things. [grin] The blocks you can stand on are not like in the old "Super Mario" games but they can also be irregular polygons. (tilted ground) I want some simple physics in the game. Now my question: As I think to implement my own physics engine would be too much, I searched for some 2D physics libraries. I found Box2D. (Or do you have a better suggestion?) How do you set up your objects in a physical library so that they behave correctly? I want the player to be always straight up. Should I create the player's body and shape and set the rotational inertia to 0? Then nothing can tilt the player over. What if I want the player to rotate when for example a bomb explodes and throws him into the air? Maybe I can set the rotational inertia back to a positive number? Or should I always have the player with an inertia not equal to 0 and write a function that balances the player on the ground? What I would really like is when you are able to rotate the player while you are in the air (with your jet pack) but not on the ground. You can press keys to rotate the player and another to accelerate him in the direction he is facing. But what if you land on the ground upside down? The game has to apply a torque that it turns back? (stand up) I want to know if somebody has ever done something of this type or if there is a better solution than mine. Thanks, I'm looking forward to your replies! aeroz [Edited by - aeroz on April 24, 2008 3:07:07 PM]
Advertisement
Quote:Original post by aeroz
Hello,
I'm currently developing a simple side-scrolling game in outer space. You control an astronaut and you have to collect all sorts of things. [grin]
The blocks you can stand on are not like in the old "Super Mario" games but they can also be irregular polygons. (tilted ground) I want some simple physics in the game.
There are games that are usable because they adhere to traditional and "fun" unrealistic conventions (e.g. standing unbalanced on the last pixel of a platform in a platformer) and games that are usable because they adhere to physical intuition (e.g. unlimited-speed Asteroids); with "some simple physics" you risk to fall in the middle, with an impractical simulation that is too complex and quirky to be realistic and too strange and unpredictable to be conventional.
Quote:
I want the player to be always straight up.
In outer space there is no "straight up". I think you really want to align the astronaut with the screen, and perhaps keep him completely or partially centered, for user interface reasons; but you can let him move naturally and rotate and translate the viewport to place him as desired, instead of hacking the physical model and opening the door to worse problems.
Quote:

Should I create the player's body and shape and set the rotational inertia to 0? Then nothing can tilt the player over. What if I want the player to rotate when for example a bomb explodes and throws him into the air? Maybe I can set the rotational inertia back to a positive number? Or should I always have the player with an inertia not equal to 0 and write a function that balances the player on the ground?

You can use a correct inertia moment and apply (with the mentioned jetpack and other very visible means) torques and forces that compensates unwanted pushing, when the astronaut wants and with appropriate limitations and quirks. Small nozzles on the shoulders, pointing straight forward and straight back, should be good-looking and physically sensible (as far as possible from the center of mass while still on the astronaut's torso).
Quote:
What I would really like is when you are able to rotate the player while you are in the air (with your jet pack) but not on the ground. You can press keys to rotate the player and another to accelerate him in the direction he is facing.
A straightforward control scheme, but does the jetpack point up or forward? Is there any gravity or fake gravity? And most importantly, what happens "on the ground"?
My first choice would be a jetpack pointing up, that always propels the astronaut where his head points (up arrow), and left/right arrows that make the astronaut walk when his feet are in contact with a surface (easily deduced by the reported contact points), regardless of gravity (he can have magnetic boots), and make him turn when he's flying or falling (at a fixed speed, firing a propulsor briefly when the key is pressed and the contrary one while the key is released; this translates into applying a torque to accelerate and to decelerate for a few simulation frames, with constant angular speed in the middle).
Quote:
But what if you land on the ground upside down? The game has to apply a torque that it turns back? (stand up)

Predict the time to impact and automatically turn him with his thrusters. You might get away without adding animations for staggering and for standing up from prone and supine positions.

Omae Wa Mou Shindeiru

Thanks for the response. Sorry for my late answering.
Quote:There are games that are usable because they adhere to traditional and "fun" unrealistic conventions (e.g. standing unbalanced on the last pixel of a platform in a platformer) and games that are usable because they adhere to physical intuition (e.g. unlimited-speed Asteroids); with "some simple physics" you risk to fall in the middle, with an impractical simulation that is too complex and quirky to be realistic and too strange and unpredictable to be conventional.
Yeah, you are right. But in some FPS you can for example push boxes that behave physically correct. You can shoot at some Objects that fly away and collide with the walls. But the player is a body that can't be tilted in any case. It's something like this that I want to do. (in 2D)

So I think I can set the rotational inertia of the astronaut to 0 and let him shoot at boxes and objects that will fly away...
Quote:A straightforward control scheme, but does the jetpack point up or forward? Is there any gravity or fake gravity? And most importantly, what happens "on the ground"?
I haven't explained it well enough I think. When you are on the ground you can just walk right or left, like in a platformer. When you walk into a stack of boxes the boxes should fall down. Maybe I can find a game as example later.
But since I'm using a physics library, I think I have to always apply forces to the astronauts center of mass when he wants to move.
When he is in the air, he should be able to apply a torque at himself (so the inertia can't be 0), then he rotates.
Quote:My first choice would be a jetpack pointing up, that always propels the astronaut where his head points (up arrow), and left/right arrows that make the astronaut walk when his feet are in contact with a surface
Exactly!... [smile]
Quote:
Quote:But what if you land on the ground upside down? The game has to apply a torque that it turns back? (stand up)
Predict the time to impact and automatically turn him with his thrusters.
I don't know if this would be a good idea. (I mean from the players view). Don't you think it would be nicer when the astronaut falls with whatever angle on the ground and then stands up? So if he lies on the ground, I apply a torque to him so he stands up. This could be a bit more complex to implement.

Well, this is not the problem at the moment (optional). The important thing is how much rotational inertia the player body has? The astronaut's shape would be a rectangle probably. So when the player walks on a tilted surface the player would tilt and not stay straight up. (against gravity) How can I solve this?
Sorry for my English mistakes...
Hope somebody has ideas!
aeroz
You can use a physics system like Box2D in a sidescroller. It can be a bit of a pain setting things up correctly, but its certainly doable.
Thank you, I will try it out.
But I think I will not allow the astronaut to rotate.
Hey,
I'm playing around with Chipmunk Physics at the moment and tried to do about the same except that I have gravity all the time. For the player I use a normal box to represent his body as long as he only collides with the ground. Right now I'm working on the animation system, which only draws the body inside the box. The rotation on uneven ground is a problem for me as well and I kinda solved it by setting the momentum to a high number and I change the angular velocity to zero as long as the box touches the ground. So it doesn't spin around all the time. Sometimes when walking over shapes with a big difference in angle the body falls over, which doesn't look very cool. My temporary solution for that is setting the angle back to zero when it is too big. Another way could be setting the angle to the ground's one, although I didn't try it yet because I don't know what to do if it touches different shapes with different angles.

EDIT: Found a solution: I count the number of collisions between the player and the ground and take the normal force to set the angle if there is only one collision. Seems like it works pretty good.

[Edited by - TobiO on April 27, 2008 12:26:07 PM]

This topic is closed to new replies.

Advertisement