Skip to content

Instantly share code, notes, and snippets.

@donalmacc
Created August 19, 2012 23:12
Show Gist options
  • Save donalmacc/3398437 to your computer and use it in GitHub Desktop.
Save donalmacc/3398437 to your computer and use it in GitHub Desktop.
Shows a simple modification of gravity in Box2D
#ifndef FOOTEST_H
#define FOOTEST_H
class FooTest : public Test
{
public:
// List of shapes
b2Body *dynamicBody; // The players shape
b2Body* lowerground; // The horizontal platform
b2Body* sideground; // The vertical platform
bool isGravitySideWays;
FooTest() {
b2Vec2 gravity(0, -9.81); //normal earth gravity, 9.8 m/s/s straight down!
isGravitySideWays = false; // Gravity is not sideways
m_world->SetGravity(gravity);
//Draw the "player" shape
b2BodyDef myBodyDef;
myBodyDef.type = b2_dynamicBody; //this will be a dynamic body
myBodyDef.position.Set(0, 20); //set the starting position
myBodyDef.angle = 0; //set the starting angle
dynamicBody = m_world->CreateBody(&myBodyDef);
b2CircleShape circleShape;
circleShape.m_p.Set(0, 0); //position, relative to body position
circleShape.m_radius = 1; //radius
b2FixtureDef boxFixtureDef;
boxFixtureDef.shape = &circleShape;
boxFixtureDef.density = 1;
dynamicBody->CreateFixture(&boxFixtureDef);
//Draw the ground
myBodyDef.type = b2_staticBody; //this will be a static body
myBodyDef.position.Set(-5, 15); //slightly lower position
lowerground = m_world->CreateBody(&myBodyDef); //add body to world
b2PolygonShape hfloor;
hfloor.SetAsBox(10,1);
b2FixtureDef LongFixtureDef;
LongFixtureDef.shape = &hfloor;
LongFixtureDef.density = 1;
//Draw the side "Ground"
myBodyDef.type = b2_staticBody;
myBodyDef.type = b2_staticBody; //this will be a static body
myBodyDef.position.Set(15, 30); //slightly off to the right, and up a bit
sideground = m_world->CreateBody(&myBodyDef); //add body to world
b2PolygonShape sideFloor;
sideFloor.SetAsBox(1,10);
b2FixtureDef sideFixtureDef;
sideFixtureDef.shape = &sideFloor;
sideFixtureDef.density = 1;
lowerground->CreateFixture(&LongFixtureDef); //add fixture to body
sideground->CreateFixture(&sideFixtureDef);
} //do nothing, no scene yet
void Step(Settings* settings)
{
//run the default physics and rendering
Test::Step(settings);
b2Vec2 d1 = dynamicBody->GetPosition(); // Position of the player
b2Vec2 d2 = lowerground->GetPosition(); // Position of the horizontal ground
b2Vec2 d3 = sideground->GetPosition(); // Position of the sideways ground
d2 = d1 - d2; //Distance between the horizontal ground and the player
d3 = d1 - d3; // Same as above, but for the side platform
// Compare the lengths, and see which is shorter
if(d2.Normalize() < d3.Normalize())
{
//Closer to d2, which is lowerground, so gravity acts down
isGravitySideWays = false;
m_world->SetGravity(b2Vec2(0,-9.81)); // This should be the normal to the surface that you want gravity to be acting towards
}
else
{
//Closer to d2, which is sideground, so gravity acts sideways
isGravitySideWays = true;
m_world->SetGravity(b2Vec2(9.81,0)); // Same as above, should actually be the normal to the lower ground surface.
}
}
static Test* Create()
{
return new FooTest;
}
void Keyboard(unsigned char key)
{
float jump = 3000;
float side = 350;
b2Vec2 t;
// Deal with user input
switch (key)
{
case 'w':
//apply force upwards
//If user is closer to the side wall, then gravity acts sideways and can be set in the x direction
if (isGravitySideWays){
t.Set(-1*jump, 0);
}
else
{
t.Set(0,jump);
}
dynamicBody->ApplyForce( t, dynamicBody->GetWorldCenter() );
break;
// Set the moving sideways vectors, based on the current orientation
case 'a':
// apply force left
if (isGravitySideWays)
{
t.Set(0, -1*side);
}
else
{
t.Set(-1*side,0);
}
dynamicBody->ApplyForce( t, dynamicBody->GetWorldCenter() );
break;
case 'd':
//Go right
if (isGravitySideWays)
{
t.Set(0, side);
}
else
{
t.Set(side,0);
}
dynamicBody->ApplyForce( t, dynamicBody->GetWorldCenter() );
break;
default:
//run default behaviour
Test::Keyboard(key);
}
}
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment