Rube Goldberg Machine 1.0
This is the base code for Rube Goldberg designed for the CS296 Software Systems Lab
|
This is the class that sets up the Box2D simulation world Notice the public inheritance - why do we inherit the base_sim_t class? More...
#include <dominos.hpp>
Public Member Functions | |
dominos_t () | |
Default Constructor which creates all the bodies in the world. | |
Static Public Member Functions | |
static base_sim_t * | create () |
Create. |
This is the class that sets up the Box2D simulation world Notice the public inheritance - why do we inherit the base_sim_t class?
The base_sim_t class contains the simulation functions and the global variables
Definition at line 33 of file dominos.hpp.
cs296::dominos_t::dominos_t | ( | ) |
Default Constructor which creates all the bodies in the world.
This constructor is used to create bodies in the Box2D world. The steps to create a body in the world:
1. Define a body with position, damping, etc.
2. Use the world object to create the body.
3. Define fixtures with a shape, friction, density, etc.
4. Create fixtures on the body.
The bodies created in this world are:
The two Grounds of the World.
Top horizontal shelf
The trigger balloon which rises in the beginning of the Simulation.
The first revolving horizontal platform which is hit by the balloon.
The ball that falls on the dominoes
Dominos on the horizontal shelf which topple one after the other.
Newton's Pendulums which are triggered by the Dominos.
Sphere hit by newton's Pendulum which resides on the platform and falls on the platform
The see-saw system at the bottom. Contains a triangular wedge and a plank balanced on top of the wedge.
The pulley system. Contains an open box on one side, which resides on a revolving platform.
On the other side of the pulley is the lid to an open box which contains a balloon.
The center revolving horizontal platform on which the box connected to the pulley rests.
The box with the balloon which is closed by the lid which is connected to pulley.
The balloon in the box
The second revolving horizontal platform on the top with the top spheres on it(to balance it)
The two small spheres on the top revolving platform.
The slant edge on which one of the small spheres slides down.
The Spherical tube(semi-circular). The balls slides down this tube.
The horizontal parallel planks which have flaps at one end.
The vertical rotating flaps at the end of the horizontal planks(which hit the spheres on each floor and hit the next flap triggering a chain reaction)
The balls on the planks(which hit the next flap and trigger the chain reaction.
The small blockade which stops the rocket from take-off.
The rocket which flies off after its base is displaced slightly.
The block which acts as a base for the rocket. When the block is hit by the sphere, the base provides torque and the rocket frees from the blockade and flies off.
Definition at line 54 of file dominos.cpp.
{ //The two Grounds of the World. b2Body* b1; { //There is a hole in the higher ground where the sphere lodges itself { b2EdgeShape shape; // line segment used to define the higher ground. shape.Set(b2Vec2(-90.0f, 0.0f), b2Vec2(-20.0f, 0.0f)); // The end points of the ground is set. by the function set(). The parameters // specified are the end points of the ground. b2BodyDef bd; //Used to define the body. b1 = m_world->CreateBody(&bd); // Body created in the Box2D world. b1->CreateFixture(&shape, 0.0f); // Static Body, so the fixture is created without changing its default values. } { b2EdgeShape shape; // line segment used to define the higher ground. shape.Set(b2Vec2(-10.0f, 0.0f), b2Vec2(90.0f, 0.0f)); // The end points of the ground is set. by the function set(). The parameters // specified are the end points of the ground. b2BodyDef bd; //Used to define the body. b1 = m_world->CreateBody(&bd); // Body created in the Box2D world. b1->CreateFixture(&shape, 0.0f); // Static Body, so the fixture is created without changing its default values. } { b2EdgeShape shape; // line segment used to define the lower ground. shape.Set(b2Vec2(-90.0f, -2.0f), b2Vec2(90.0f, -2.0f)); // The end points of the ground is set. by the function set(). The parameters // specified are the end points of the ground. b2BodyDef bd; //Used to define the body. b1 = m_world->CreateBody(&bd); // Body created in the Box2D world. b1->CreateFixture(&shape, 0.0f); // Static Body, so the fixture is created without changing its default values. } } //Top horizontal shelf { b2PolygonShape shape; // used to define a polygon shape. shape.SetAsBox(6.0f, 0.25f); // Create a box of half-length 6f and half-width 0.25f b2BodyDef bd; // Used to define the shelf. bd.position.Set(-30.5f, 30.0f); // Set the position of the shelf. b2Body* ground = m_world->CreateBody(&bd); // Create the shelf in the Box2D world. ground->CreateFixture(&shape, 0.0f); // Static Body, so the fixture is created without changing its default values. } //The trigger balloon which rises in the beginning of the Simulation. { b2Body* sbody; // The heavy sphere object b2CircleShape circle; // Defining the shape of the sphere(circle) circle.m_radius = 1.0; // radius of the sphere b2FixtureDef ballfd; // FIxture of the heavy sphere ballfd.shape = &circle; // Assigning the shape to the fixture ballfd.density = 1.0f; // Setting the density ballfd.friction = 0.0f; // Frictionless ball ballfd.restitution = 0.0f; // Collision of Restitution = 0 (Inelastic Collision) b2BodyDef ballbd; // Defining the heavy sphere ballbd.type = b2_dynamicBody; // Setting the sphere to dynamic ballbd.position.Set(-38.5f, 22.0f); sbody = m_world->CreateBody(&ballbd); // Creating the sphere in the world sbody -> SetGravityScale(-0.2f); // Setting the Gravity Scale(SInce its a balloon, it has negative gravity to rise) sbody->CreateFixture(&ballfd); // Assigning the fixture to the sphere. } //The first revolving horizontal platform which is hit by the balloon. { //The Plank which revolves. b2PolygonShape shape; // Creating a shape for the platform. shape.SetAsBox(2.4f, 0.2f); // Defining the dimensions of the platform b2BodyDef bd; bd.position.Set(-37.0f, 35.0f); // Setting position of the platform. bd.type = b2_dynamicBody; // Setting type to dynamic b2Body* body = m_world->CreateBody(&bd); // Creating the platform in the world b2FixtureDef *fd = new b2FixtureDef; // Creating a new Fixture to set the platform's physical properties. fd->density = 1.f; // Setting density of the platform. fd->shape = new b2PolygonShape; fd->shape = &shape; body->CreateFixture(fd); // Assigning fixture to the body. // Creating an invisible hinge to rotate the platform: Static object. b2PolygonShape shape2; shape2.SetAsBox(0.2f, 1.0f); //Dimensions of the hinge. b2BodyDef bd2; bd2.position.Set(-37.0, 35.0f); b2Body* body2 = m_world->CreateBody(&bd2); //Static Object,so default fixture. // Revolute joint between the plank and the hinge. b2RevoluteJointDef jointDef; // Creating a revolute joint to rotate the platform jointDef.bodyA = body; // by joining the bar and the hinge. jointDef.bodyB = body2; jointDef.localAnchorA.Set(0,0); // Setting Local Anchor jointDef.localAnchorB.Set(0,0); jointDef.collideConnected = false; // The bodies will not collide with each other m_world->CreateJoint(&jointDef); // Creating the joint in the world. } //The ball that falls on the dominoes { b2Body* spherebody; // Creating the spheres b2CircleShape circle; // Defining the shape of the sphere circle.m_radius = 1.0; // Circle of radius 1.0f b2FixtureDef ballfd; // Defining the fixture of the sphere ballfd.shape = &circle; // Assigning the shape to the fixture ballfd.density = 1.0f; // Assigning the density ballfd.friction = 0.0f; // Assigning the friction ballfd.restitution = 0.0f; b2BodyDef ballbd; // Defining the sphere ballbd.type = b2_dynamicBody; // Setting the type to dynamic ballbd.position.Set(-37.0f, 37.0f); // Setting the position of the spheres spherebody = m_world->CreateBody(&ballbd); // Creating the spheres in the Box2D world spherebody->CreateFixture(&ballfd); // Creating the spheres with fixture defined } //Dominos on the horizontal shelf which topple one after the other. { b2PolygonShape shape; // used to define a polygon shape. shape.SetAsBox(0.1f, 1.0f); // Create the domino of half-width 0.1f and half-height 1.0f b2FixtureDef fd; // Create a fixture object to change the properties of the domino(dynamic body in motion) fd.shape = &shape; // Set the shape of the fixture. fd.density = 20.0f; // Set the density of the fixture. fd.friction = 0.1f; // Set the friction of the fixture. for (int i = 1; i < 10; ++i) // For-loop for the 10 dominoes { b2BodyDef bd; // Used to define the body. bd.type = b2_dynamicBody; // Set the type of the domino to dynamic( so that it detects collision) bd.position.Set(-35.5f + 1.0f * i, 31.25f); // set the position of the domino b2Body* body = m_world->CreateBody(&bd); // Create the domino in the Box2D world. body->CreateFixture(&fd); // Create the domino fixture as set above. } } //Newton's Pendulums which are triggered by the Dominos. { //Creating the ten pendulums. for( int i = 0; i < 10 ; i++) { // the top support of the pendulum b2Body* b2; { b2PolygonShape shape; // Defining the shape of the support shape.SetAsBox(0.25f, 0.5f); // Setting the box dimensions. b2BodyDef bd; bd.position.Set(-25.5f + 0.5*i, 35.0f); // Set the position of the body b2 = m_world->CreateBody(&bd); // Creating the top support in the world. b2->CreateFixture(&shape, 10.0f); // Creating the fixture of the top support } //Bob of the Pendulum b2Body* b4; // the bob of the pendulum { b2PolygonShape shape; // Defining the shape of the bob. shape.SetAsBox(0.25f, 0.25f); // Setting the bob dimensions. b2BodyDef bd; bd.type = b2_dynamicBody; // The bob type is set to dynamic. bd.position.Set(-25.5f + 0.5*i, 31.0f); // The positions of the bob are set. b4 = m_world->CreateBody(&bd); // Creating the bob in the Box2D world. b4->CreateFixture(&shape, 2.0f); // Creating the fixture of the bob. } //The revolute joint between the bob and the support. b2RevoluteJointDef jd; b2Vec2 anchor; // Creating the anchor that links the bob and the base support. Center of rotation anchor.Set(-25.5f + 0.5*i, 35.0f); jd.Initialize(b2, b4, anchor); // Creating the revolute joint. The bob can only revolve about the anchor m_world->CreateJoint(&jd); // Creating the joint in the world. } } // Sphere hit by newton's Pendulum which resides on the platform and falls on the platform { //The shelf. { b2PolygonShape shape; // used to define a polygon shape. shape.SetAsBox(1.0f, 0.025f); // Create a box of half-length 6f and half-width 0.25f b2BodyDef bd; // Used to define the shelf. bd.position.Set(-17.0f, 31.0f); // Set the position of the shelf. b2Body* ground = m_world->CreateBody(&bd); // Create the shelf in the Box2D world. ground->CreateFixture(&shape, 0.0f); // Static Body, so the fixture is created without changing its default values. } //The sphere on the shelf. { b2Body* spherebody; // Creating the spheres b2CircleShape circle; // Defining the shape of the sphere circle.m_radius = 2.0; // Circle of radius 2.0f b2FixtureDef ballfd; // Defining the fixture of the sphere ballfd.shape = &circle; // Assigning the shape to the fixture ballfd.density = 1.0f; // Assigning the density ballfd.friction = 0.0f; // Assigning the friction ballfd.restitution = 0.0f; b2BodyDef ballbd; // Defining the sphere ballbd.type = b2_dynamicBody; // Setting the type to dynamic ballbd.position.Set(-16.0f, 32.0f); // Setting the position of the spheres spherebody = m_world->CreateBody(&ballbd); // Creating the spheres in the Box2D world spherebody->CreateFixture(&ballfd); // Creating the spheres with fixture defined spherebody-> SetGravityScale(1.0f); //Normal Garvity Scale. } } //The see-saw system at the bottom. Contains a triangular wedge and a plank balanced on top of the wedge. { //The triangle wedge b2Body* sbody; b2PolygonShape poly; // Defining a polygon shape by defining the points. b2Vec2 vertices[3]; //Using an array of vertices to define the three endpoints of the triangle vertices[0].Set(-5,0); vertices[1].Set(5,0); vertices[2].Set(0,7.5); poly.Set(vertices, 3); // Setting the shape of the triangle wedge. b2FixtureDef wedgefd; // Defining the fixture for the wedge. wedgefd.shape = &poly; // Assigning the shape to the fixture wedgefd.density = 10.0f; // Setting the density wedgefd.friction = 0.0f; // Frictionless wedge wedgefd.restitution = 0.0f; b2BodyDef wedgebd; wedgebd.position.Set(-4.0f, 0.0f); sbody = m_world->CreateBody(&wedgebd); // Creating the wedge in the world. Static Body sbody->CreateFixture(&wedgefd); // Assigning the fixture to the wedge // The plank on top of the wedge b2PolygonShape shape; // Defining the shape of the plank shape.SetAsBox(15.0f, 0.2f); // Setting the dimensions b2BodyDef bd2; bd2.position.Set(-4.0f, 7.0f); bd2.type = b2_dynamicBody; // Setting the plank to dynamic body. b2Body* body = m_world->CreateBody(&bd2); // Creating the plank in the world b2FixtureDef *fd2 = new b2FixtureDef; // Creating the fixture of the plank fd2->density = 1.f; fd2->shape = new b2PolygonShape; // Defining the shape of the plank fd2->shape = &shape; // Passing the shape by reference. body->CreateFixture(fd2); b2RevoluteJointDef jd; // Creating a revolute joint between the wedge and plank b2Vec2 anchor; anchor.Set(-4.0f, 7.0f); // the center of rotation is set to the center of the plank jd.Initialize(sbody, body, anchor); // Initializing the revolute joint. m_world->CreateJoint(&jd); } //The pulley system. Contains an open box on one side, which resides on a revolving platform. //On the other side of the pulley is the lid to an open box which contains a balloon. { b2BodyDef *bd = new b2BodyDef; // Creating a new Body definition bd->type = b2_dynamicBody; // Setting the pulley system to be dynamic. bd->position.Set(14,15); bd->fixedRotation = true; // Setting fixed rotation to true. The pulley system will not rotate. //The open box //Creating the box by 3 planks which form the open box. //The three fixtures are assigned to same body object to create an open box // Plank 1 b2FixtureDef *fd1 = new b2FixtureDef; // Creating a fixture of the plank. fd1->density = 10.0; // Assigning the properties to the box(density) fd1->friction = 0.5; // Friction fd1->restitution = 0.f; // Restitution fd1->shape = new b2PolygonShape; // Creating a new b2PolygonShape for the fixture fd1 b2PolygonShape bs1; bs1.SetAsBox(2,0.2, b2Vec2(0.f,-1.9f), 0); fd1->shape = &bs1; // Assigning reference to the shape defined above. // Plank 2 b2FixtureDef *fd2 = new b2FixtureDef; fd2->density = 10.0; fd2->friction = 0.5; fd2->restitution = 0.f; fd2->shape = new b2PolygonShape; b2PolygonShape bs2; bs2.SetAsBox(0.2,2, b2Vec2(2.0f,0.f), 0); fd2->shape = &bs2; // Plank 3 b2FixtureDef *fd3 = new b2FixtureDef; fd3->density = 10.0; fd3->friction = 0.5; fd3->restitution = 0.f; fd3->shape = new b2PolygonShape; b2PolygonShape bs3; bs3.SetAsBox(0.2,2, b2Vec2(-2.0f,0.f), 0); fd3->shape = &bs3; b2Body* box1 = m_world->CreateBody(bd); // Creating the body in the Box2D world. //Assigning the three fixtures to box1 to create a open box box1->CreateFixture(fd1); box1->CreateFixture(fd2); box1->CreateFixture(fd3); //The bar which acts a lid to the box with the balloon. bd->position.Set(30,20); // Setting the position of the bar fd1->density = 34.0; b2Body* box2 = m_world->CreateBody(bd); // Creating the bar in the world b2FixtureDef *fd4 = new b2FixtureDef; // Creating a fixture of the bar. fd4->density = 10.0; // Assigning the properties to the bar(density) fd4->friction = 0.5; // Friction fd4->restitution = 0.f; // Restitution fd4->shape = new b2PolygonShape; b2PolygonShape bs4; bs4.SetAsBox(5,0.2, b2Vec2(0.f,-1.9f), 0); fd4->shape = &bs4; box2->CreateFixture(fd4); //Shelf on which the box lies { b2PolygonShape shape; // used to define a polygon shape. shape.SetAsBox(2.0f, 0.25f); // Create a box b2BodyDef bd; // Used to define the shelf. bd.position.Set(30.0f, 14.0f); // Set the position of the shelf. b2Body* ground = m_world->CreateBody(&bd); // Create the shelf in the Box2D world. ground->CreateFixture(&shape, 0.0f); // Static Body, so the fixture is created without changing its default values. } // The pulley joint b2PulleyJointDef* myjoint = new b2PulleyJointDef(); // Creating a pulley joint. //b2Vec2 worldAnchorOnBody1(0, 15); // Anchor point on body 1 in world axis //b2Vec2 worldAnchorOnBody2(15, 15); // Anchor point on body 2 in world axis b2Vec2 worldAnchorGround1(14, 20); // Anchor point for ground 1 in world axis b2Vec2 worldAnchorGround2(20, 20); // Anchor point for ground 2 in world axis float32 ratio = 1.0f; // Define ratio myjoint->Initialize(box1, box2, worldAnchorGround1, worldAnchorGround2, box1->GetWorldCenter(), box2->GetWorldCenter(), ratio); m_world->CreateJoint(myjoint); // Initializing the joint with box1, box2 and the coordinates. } //The center revolving horizontal platform on which the box connected to the pulley rests. { b2PolygonShape shape; shape.SetAsBox(2.4f, 0.2f); b2BodyDef bd; bd.position.Set(12.25f, 12.0f); bd.type = b2_dynamicBody; // Setting type to dynamic b2Body* body = m_world->CreateBody(&bd); // Creating the platform in the world b2FixtureDef *fd = new b2FixtureDef; fd->density = 1.f; fd->shape = new b2PolygonShape; fd->shape = &shape; body->CreateFixture(fd); // Creating an invisible hinge to rotate the platform b2PolygonShape shape2; shape2.SetAsBox(0.2f, 1.0f); b2BodyDef bd2; bd2.position.Set(12.25f, 12.0f); b2Body* body2 = m_world->CreateBody(&bd2); b2RevoluteJointDef jointDef; // Creating a revolute joint to rotate the platform. jointDef.bodyA = body; jointDef.bodyB = body2; jointDef.localAnchorA.Set(0,0); jointDef.localAnchorB.Set(0,0); jointDef.collideConnected = false; // The bodies will not collide with each other m_world->CreateJoint(&jointDef); // Creating the joint in the world. } //The box with the balloon which is closed by the lid which is connected to pulley. { b2BodyDef *bd = new b2BodyDef; // Creating a new Body definition bd->type = b2_dynamicBody; // Setting the box to be dynamic. bd->position.Set(30,16); //The open box //Creating the box by 3 planks which form the open box. //The three fixtures are assigned to same body object to create an open box // Plank 1 b2FixtureDef *fd1 = new b2FixtureDef; // Creating a fixture of the plank. fd1->density = 10.0; // Assigning the properties to the box(density) fd1->friction = 0.5; // Friction fd1->restitution = 0.f; // Restitution fd1->shape = new b2PolygonShape; // Creating a new b2PolygonShape for the fixture fd1 b2PolygonShape bs1; bs1.SetAsBox(5,0.2, b2Vec2(0.f,-1.9f), 0); fd1->shape = &bs1; // Assigning reference to the shape defined above. // Plank 2 b2FixtureDef *fd2 = new b2FixtureDef; // Creating a fixture of the plank. fd2->density = 10.0; // Assigning the properties to the box(density) fd2->friction = 0.5; // Friction fd2->restitution = 0.f; // Restitution fd2->shape = new b2PolygonShape; // Creating a new b2PolygonShape for the fixture fd2 b2PolygonShape bs2; bs2.SetAsBox(0.2,2, b2Vec2(5.0f,0.f), 0); fd2->shape = &bs2; // Plank 3 b2FixtureDef *fd3 = new b2FixtureDef; fd3->density = 10.0; fd3->friction = 0.5; fd3->restitution = 0.f; fd3->shape = new b2PolygonShape; b2PolygonShape bs3; bs3.SetAsBox(0.2,2, b2Vec2(-5.0f,0.f), 0); fd3->shape = &bs3; b2Body* box1 = m_world->CreateBody(bd); // Creating the body in the Box2D world. //Assigning the three fixtures to box1 to create a open box //The three fixtures define the 3 planks. Assigning them to box1 creates a fixed box with 3 planks. box1->CreateFixture(fd1); box1->CreateFixture(fd2); box1->CreateFixture(fd3); } //The balloon in the box { b2Body* sbody; // The balloon object b2CircleShape circle; // Defining the shape of the balloon(circle) circle.m_radius = 1.0; // radius of the balloon b2FixtureDef ballfd; // FIxture of the balloon ballfd.shape = &circle; // Assigning the shape to the fixture ballfd.density = 1.0f; // Setting the density ballfd.friction = 0.0f; // Frictionless ball ballfd.restitution = 0.0f; // Collision of Restitution = 0 (Inelastic Collision) b2BodyDef ballbd; // Defining the balloon ballbd.type = b2_dynamicBody; // Setting the balloon to dynamic ballbd.position.Set(29.0f, 16.0f); sbody = m_world->CreateBody(&ballbd); // Creating the balloon in the world sbody -> SetGravityScale(-0.1f); // Setting Negative Gravity so that balloon rises up. sbody->CreateFixture(&ballfd); // Assigning the fixture to the balloon. } //The second revolving horizontal platform on the top with the top spheres on it(to balance it) { b2PolygonShape shape; shape.SetAsBox(4.0f, 0.2f); b2BodyDef bd; bd.position.Set(27.5f, 42.25f); bd.type = b2_dynamicBody; // Setting type to dynamic b2Body* body = m_world->CreateBody(&bd); // Creating the platform in the world b2FixtureDef *fd = new b2FixtureDef; fd->density = 1.f; fd->shape = new b2PolygonShape; fd->shape = &shape; body->CreateFixture(fd); // Creating an invisible hinge to rotate the platform b2PolygonShape shape2; shape2.SetAsBox(0.2f, 1.0f); b2BodyDef bd2; bd2.position.Set(27.5f, 42.25f); b2Body* body2 = m_world->CreateBody(&bd2); b2RevoluteJointDef jointDef; // Creating a revolute joint to rotate the platform. jointDef.bodyA = body; jointDef.bodyB = body2; jointDef.localAnchorA.Set(0,0); jointDef.localAnchorB.Set(0,0); jointDef.collideConnected = false; // The bodies will not collide with each other m_world->CreateJoint(&jointDef); // Creating the joint in the world. } //The two small spheres on the top revolving platform. { b2Body* spherebody; // Creating the spheres b2CircleShape circle; // Defining the shape of the sphere circle.m_radius = 0.5; // Circle of radius 0.5f ; b2FixtureDef ballfd; // Defining the fixture of the sphere ballfd.shape = &circle; // Assigning the shape to the fixture ballfd.density = 1.0f; // Assigning the density ballfd.friction = 0.0f; // Assigning the friction ballfd.restitution = 0.0f; { b2BodyDef ballbd; // Defining the sphere ballbd.type = b2_dynamicBody; // Setting the type to dynamic ballbd.position.Set(24.0f , 42.8f); // Setting the position of the sphere spherebody = m_world->CreateBody(&ballbd); // Creating the sphere in the Box2D world spherebody->CreateFixture(&ballfd); // Creating the sphere with fixture defined } { b2BodyDef ballbd; // Defining the sphere ballbd.type = b2_dynamicBody; // Setting the type to dynamic ballbd.position.Set(31.0f , 42.8f); // Setting the position of the sphere spherebody = m_world->CreateBody(&ballbd); // Creating the sphere in the Box2D world spherebody->CreateFixture(&ballfd); // Creating the sphere with fixture defined } } //The slant edge on which one of the small spheres slides down. { b2EdgeShape shape; // line segment used to define the path. shape.Set(b2Vec2(-40.0f, 38.0f), b2Vec2(23.6f, 41.75f)); // The two end points of the path are specified. b2BodyDef bd; //Used to define the body. b1 = m_world->CreateBody(&bd); // Body created in the Box2D world. b1->CreateFixture(&shape, 0.0f); // Static Body, so the fixture is created without changing its default values. } //The Spherical tube(semi-circular). The balls slides down this tube. { float pi = 3.14159 ; float inrad = 8.0, outrad = 9.25, cx = -40.0, cy = 30.0 ; //Temporary positional variables. //One circular edge of the tube. for(int i=0,j=2; i<90; i+=2, j+=2) { b2EdgeShape shape; // Define line segment shape shape.Set(b2Vec2(cx - inrad * sin(2*pi*i/180), cy + inrad * cos(2*pi*i/180)), b2Vec2(cx - inrad * sin(2*pi*j/180), cy + inrad * cos(2*pi*j/180))); // Position the end points of the segments // To make a circular structure of 180 degrees, divided the semi-circle // into 90 arc segments of 2 degrees each and created line segments // with end points of each of the arcs b2BodyDef bd; // Used to define Box2D body b1 = m_world->CreateBody(&bd); // Used to define a fixture b1->CreateFixture(&shape, 0.0f); } //The other circular edge of the tube. for(int i=0,j=2; i<90; i+=2, j+=2) { b2EdgeShape shape; shape.Set(b2Vec2(cx - outrad * sin(2*pi*i/180), cy + outrad * cos(2*pi*i/180)), b2Vec2(cx - outrad * sin(2*pi*j/180), cy + outrad * cos(2*pi*j/180))); b2BodyDef bd; b1 = m_world->CreateBody(&bd); b1->CreateFixture(&shape, 0.0f); } } //The horizontal parallel planks which have flaps at one end. { for(int i=0; i<6; i++) { b2EdgeShape shape; // line segment used to define the planks shape.Set(b2Vec2(-40.0f, 20.0f - 4.0f * i), b2Vec2(-22.0f, 20.0f - 4.0f * i)); b2BodyDef bd; //Used to define the body. b1 = m_world->CreateBody(&bd); // Body created in the Box2D world. b1->CreateFixture(&shape, 0.0f); } } //The vertical rotating flaps at the end of the horizontal planks(which hit the spheres on each floor and hit the next flap triggering a chain reaction) { //Right Flaps for(int i = 0 ; i < 3 ; i++) { b2PolygonShape shape; shape.SetAsBox(0.2f, 2.2f); // Setting Dimensions of the flap. b2BodyDef bd; bd.position.Set(-21.7f, 18.3f - 8.0f*i); // Setting position of the flap. bd.type = b2_dynamicBody; // Setting type to dynamic b2Body* body = m_world->CreateBody(&bd); // Creating the flap in the world b2FixtureDef *fd = new b2FixtureDef; fd->density = 10.0f; // Assigning density to the fixture fd->restitution = 1.0f ; fd->shape = new b2PolygonShape; fd->shape = &shape; body->CreateFixture(fd); // Creating an invisible hinge to rotate the platform b2PolygonShape shape2; shape2.SetAsBox(0.2f, 1.0f); b2BodyDef bd2; bd2.position.Set(-21.7f, 18.3f-8.0f*i); b2Body* body2 = m_world->CreateBody(&bd2); b2RevoluteJointDef jointDef; // Creating a revolute joint to rotate the flap. jointDef.bodyA = body; jointDef.bodyB = body2; jointDef.localAnchorA.Set(0,0); jointDef.localAnchorB.Set(0,0); jointDef.collideConnected = false; // The bodies will not collide with each other m_world->CreateJoint(&jointDef); } //Left Flaps for(int i = 0; i < 2; i++) { b2PolygonShape shape; shape.SetAsBox(0.2f, 2.2f); b2BodyDef bd; bd.position.Set(-40.3f, 14.2f - 8.0f*i); bd.type = b2_dynamicBody; // Setting type to dynamic b2Body* body = m_world->CreateBody(&bd); // Creating the platform in the world b2FixtureDef *fd = new b2FixtureDef; fd->density = 10.0f; fd->restitution = 1.0f ; fd->shape = new b2PolygonShape; fd->shape = &shape; body->CreateFixture(fd); // Creating an invisible hinge to rotate the platform b2PolygonShape shape2; shape2.SetAsBox(0.2f, 1.0f); b2BodyDef bd2; bd2.position.Set(-40.3f, 14.2f-8.0f*i); b2Body* body2 = m_world->CreateBody(&bd2); b2RevoluteJointDef jointDef; // Creating a revolute joint to rotate the platform. jointDef.bodyA = body; jointDef.bodyB = body2; jointDef.localAnchorA.Set(0,0); jointDef.localAnchorB.Set(0,0); jointDef.collideConnected = false; // The bodies will not collide with each other m_world->CreateJoint(&jointDef); } } //The balls on the planks(which hit the next flap and trigger the chain reaction. { //Right side balls. for(int i = 0 ; i < 3 ; i++) { b2Body* spherebody; // Creating the spheres b2CircleShape circle; // Defining the shape of the sphere circle.m_radius = 0.5; // Circle of radius 0.5f ; b2FixtureDef ballfd; // Defining the fixture of the sphere ballfd.shape = &circle; // Assigning the shape to the fixture ballfd.density = 10.0f; // Assigning the density ballfd.friction = 0.0f; // Assigning the friction ballfd.restitution = 0.0f; { b2BodyDef ballbd; // Defining the sphere ballbd.type = b2_dynamicBody; // Setting the type to dynamic ballbd.position.Set(-22.5f , 16.6 - 8.0f*i); // Setting the position of the spheres spherebody = m_world->CreateBody(&ballbd); // Creating the spheres in the Box2D world spherebody->CreateFixture(&ballfd); // Creating the spheres with fixture defined } } //Left side balls. for(int i = 0 ; i < 2 ; i++) { b2Body* spherebody; // Creating the spheres b2CircleShape circle; // Defining the shape of the sphere circle.m_radius = 0.5; // Circle of radius 0.5f ; b2FixtureDef ballfd; // Defining the fixture of the sphere ballfd.shape = &circle; // Assigning the shape to the fixture ballfd.density = 10.0f; // Assigning the density ballfd.friction = 0.0f; // Assigning the friction ballfd.restitution = 0.0f; { b2BodyDef ballbd; // Defining the sphere ballbd.type = b2_dynamicBody; // Setting the type to dynamic ballbd.position.Set(-39.5f , 12.6f - 8.0f*i); // Setting the position of the spheres spherebody = m_world->CreateBody(&ballbd); // Creating the spheres in the Box2D world spherebody->CreateFixture(&ballfd); // Creating the spheres with fixture defined } } } // The small blockade which stops the rocket from take-off. { b2BodyDef *bd = new b2BodyDef; // Creating a new Body definition bd->position.Set(-47,10); // Horizontal Plank b2FixtureDef *fd2 = new b2FixtureDef; // Defining a fixture for the blockade. fd2->density = 10.0; // Assigning density. fd2->friction = 0.0; // Assigning friction fd2->restitution = 0.f; fd2->shape = new b2PolygonShape; b2PolygonShape bs2; bs2.SetAsBox(0.2,0.2, b2Vec2(-1.5f,0.0f), 0); // Setting dimensions of the blockade. fd2->shape = &bs2; b2Body* box1 = m_world->CreateBody(bd); // Creating the body in the Box2D world. box1->CreateFixture(fd2); } //The rocket which flies off after its base is displaced slightly. { b2BodyDef *rocket = new b2BodyDef; // Creating a new Body definition rocket->type = b2_dynamicBody; // Setting the rocket to be dynamic. rocket->position.Set(-48.4,8); //The base of the rocket. b2FixtureDef *fd2 = new b2FixtureDef; // Defining a fixture for the rocket. fd2->density = 0.5f; // Low density rocket. fd2->friction = 0.88; // Friction Cofficient is set a large value to provide torque to rocket. fd2->restitution = 0.f; fd2->shape = new b2PolygonShape; b2PolygonShape bs2; bs2.SetAsBox(1.0,1.5, b2Vec2(0.0f,-1.5f), 0); fd2->shape = &bs2; //The triangular head of the rocket. b2PolygonShape poly; b2Vec2 vertices[3]; //Using an array of vertices to define the three endpoints of the triangle vertices[0].Set(-1,0); vertices[1].Set(1,0); vertices[2].Set(0,1.73); poly.Set(vertices, 3); // Setting the shape of the triangle rocket. b2FixtureDef rocketfd; // Defining the fixture for the rocket. rocketfd.shape = &poly; // Assigning the shape to the fixture rocketfd.density = 0.5f; // Setting the density rocketfd.friction = 0.88f; // Frictionless rocket rocketfd.restitution = 0.0f; b2Body* rocket_body; rocket_body = m_world->CreateBody(rocket); // Creating the rocket in the world. Static Body rocket_body -> SetGravityScale(-10.0f); // Setting the Gravity Scale(SInce its a rocket, it has large negative gravity to fly off) rocket_body->CreateFixture(&rocketfd); // Assigning the fixture to the rocket rocket_body->CreateFixture(fd2); } //The block which acts as a base for the rocket. When the block is hit by the sphere, the base provides torque and the rocket frees from the blockade and flies off. { b2PolygonShape shape; shape.SetAsBox(1.0f, 2.5f); b2BodyDef bd; bd.position.Set(-48.2f, 2.0f); bd.type = b2_dynamicBody; // Setting type to dynamic b2Body* body = m_world->CreateBody(&bd); // Creating the block in the world b2FixtureDef *fd = new b2FixtureDef; fd->density = 0.5f; // Low density so sphere can displace it. fd->friction = 0.87f; // High cofficient of friction to provide torque to the rocket. fd->shape = new b2PolygonShape; fd->shape = &shape; body->CreateFixture(fd); } }
static base_sim_t* cs296::dominos_t::create | ( | ) | [inline, static] |
Create.
Returns a new dominos_t object.
Definition at line 48 of file dominos.hpp.
{ return new dominos_t; }