Hi all, I just want to preface this with me stating that I'm very very new to programming as well as cocos2d and box2d. I've spend a few weeks doing nothing but research in regards to learning objective-c as well as cocos2d and I have downloaded and read tons of tutorials on building simple games just to get myself used to everything. I've been making some pretty good headway with my game (surprisingly to me) and I've run into a snag the past couple of days regarding the gravity in my game. As stated, I'm working on a very basic game which has a player character and blocks. Basically, a 50 pixel wide block will randomly spawn from somewhere off screen and move from the bottom of the screen to the top and off screen. This goes on forever with a new block randomly spawning every second. That's all fine and good. Now I'm working on the physics of the game because I need some collision so that my character can stand on the blocks and then fall off onto the next platform. When I implement box2d to create bodies for my characters, my player works just fine and has defined properties and moves around no problem at all. When I try to define a body for my blocks, it causes all sorts of problems with the gravity in the game. Essentially, the blocks stop randomly spawning only to spawn from one spot right on top of each other and then the game crashes. When I remove any definitions to the blocks, everything goes back to normal, no problem. This leads me to believe that the gravity in the world which pulls the player down is affecting the blocks. My question is, how would I go about creating a gravity that's specific only to the randomly spawning blocks so they can continue moving upwards on their own without affecting the players gravity pulling the player down? Again, I'm very new to this whole process and I've spend 2 days searching for help online through these forums as well as other websites. If this would be easier to do in Chipmunk, I'm game, but again I was having difficulty getting that to work as well. Thanks for any help/input that any of you may have.
Box2D gravity question
(19 posts) (11 voices)-
Posted 1 year ago #
-
Gravity is just a force. If you only want some objects to be affected by gravity you either have to turn off gravity and manually apply the gravitational force to each affected object OR leave gravity on and apply a counteracting force to the objects you don't want affected by gravity.
Posted 1 year ago # -
how do you set the gravity of a body? so that body has its own gravity?
Posted 1 year ago # -
Use the ApplyForce function... see the box2d documentation.
Posted 1 year ago # -
i use that in other partys, thats not consistant gravity like the gravity in a world is though. I heard you can set the "gravity" of an object independently
Posted 1 year ago # -
I am also interrested in a way to turn gravity on/off for some bodies.
I have found this article where the author edit the source code of box2d to create a isGravited property.http://www.devnote.org/wordpress/?p=14
It's now 1 year old and I suppose box2d sources have been updated in the mean time so it mays not work "as is". I have not tested for now, I would prefer a cleaner way...
Posted 1 year ago # -
ya what i want to be able to do is have it where the worlds gravity is -30 but then some objects go up with a gravity of 20 or down with only a gravity of -20. i need to be able to set independent gravity to some objects
Posted 1 year ago # -
In this case you could just put a gravity of -30 and apply forces on bodies which need another grativy. For example apply a force +10 to the bodies that need to be only with a gravity of -20.
Posted 1 year ago # -
@tof42 the opposing force would have to be (10 * mass) in order to work correctly.
force = mass * acceleration
gravity is actually equal to acceleration in this equation.
@JMarx If your main problem is that shapes are appearing in the same spot, I would say gravity is not your issue (not the only one at least).
Posted 1 year ago # -
so how would i make this correctly have a gravity of -20 if gravity of the world is 0?
+ (void) asteroidBallx:(int) xs asteroidy: (int) ys inParent:(CCNode *)parentNode inWorld:(b2World *)parentWorld { // Create redBall body and shape CGPoint location = CGPointMake(xs-.5, ys-.5); location = [[CCDirector sharedDirector] convertToGL:location]; CCSprite *_redBall; _redBall = [CCSprite spriteWithFile:@"RedBall23.png"]; _redBall.position = ccp(location.x, location.y); _redBall.tag = 17; [parentNode addChild:_redBall]; // Create redBall body b2BodyDef redBallBodyDef; redBallBodyDef.type = b2_staticBody; redBallBodyDef.position.Set(location.x/PTM_RATIO, location.y/PTM_RATIO); redBallBodyDef.userData = _redBall; //redBallBodyDef.linearVelocity = b2Vec2(0.0f, 0.0f); b2Body *redBallBody = parentWorld->CreateBody(&redBallBodyDef); // Create redBall shape b2CircleShape circles; circles.m_radius = 11.5/PTM_RATIO; // Create shape definition and add to body b2FixtureDef redBallShapeDef; redBallShapeDef.shape = &circles; redBallShapeDef.density = 10; redBallShapeDef.friction = .1; redBallShapeDef.restitution = .7f; redBallBody->CreateFixture(&redBallShapeDef); }Posted 1 year ago # -
One solution would be right before (and every time) you call:
world->Step(....)you would first call (having a reference to redBallBody)
redBallBody->ApplyForce( redBallBody->GetMass()*customBodyGravity, redBallBody->GetWorldCenter());with customBodyGravity defined somewhere as
customBodyGravity.Set(0.0f, -20.0f);The same technique would work if you had global gravity turned on, and you wanted some bodies to ignore gravity, just apply a force with an acceleration opposite your global gravity vector.
Posted 1 year ago # -
Nope, jeff's got it. ApplyForce is only a one-time force. If you want something to be applied over time, it will need to be called every frame. Calling it once will mean the force is applied on the next
Stepand then removed. If you want to do a set-it-and-forget it approach,ApplyLinearImpulseis the method to use. It applies an Impulse that takes also takes the target's mass into account. An impulse is defined as Ns, or Netwens * Seconds. An impulse of 10Ns is equal to a force of 1 N applied over the course of 10 seconds.Posted 1 year ago # -
I removed the gravity from a body using:
body->SetGravityScale(0.0f);
Posted 2 months ago # -
Follow up question...if I wanted to have a switch turn off gravity momentarily. The best way to do that would still be to apply force of +7 (in case my gravity is -10) to every object in the update step until the switch (a sensor) is pressed again?
Posted 2 weeks ago # -
No, just set gravity to 0. It will have the same effect and not require a lot of annoying code.
Posted 2 weeks ago # -
Well that's what I thought so I started moving the gravity from -10.0 to -5, then -1 and finally -0.5. But the objects simply drop slower to the bottom. I didn't try 0 and had to leave but I was wondering if that last 0.5 to zero make the difference. What I'm expecting is the objects to float, no? Well wait, not really cause in the moon things still drop. Ok I got it, what I want is not a moon gravity but am actual zero-gravity environment, right?
Posted 2 weeks ago # -
It will stop them from accelerating, but if they are already moving downwards you need some other force like drag to slow them down. Taking away gravity won't make them completely stop.
Posted 2 weeks ago # -
Or I could give them some "bounciness"
Posted 2 weeks ago #
Reply
You must log in to post.