示例#1
0
//-----------------------------------------------
void ttChar::controlRope() {


    if (!joints.empty() && rects.size()>20) {
        if (ofGetElapsedTimeMillis()-startTime>50) {
            world.getWorld()->DestroyJoint(joints.front());
            world.getWorld()->DestroyBody(rects[1].body);
            joints.erase(joints.begin());
            rects.erase(rects.begin()+1);

            b2RevoluteJointDef revoluteJointDef;
            revoluteJointDef.Initialize(rects[0].body, rects[1].body, rects[0].body->GetWorldCenter());
            b2Vec2 p = screenPtToWorldPt(ofPoint(0,0));
            revoluteJointDef.localAnchorA.Set(p.x, p.y);
            p = screenPtToWorldPt(ofPoint(-9,0));
            revoluteJointDef.localAnchorB.Set(p.x, p.y);
            revoluteJointDef.enableLimit = true;
            revoluteJointDef.lowerAngle = -PI/3;
            revoluteJointDef.upperAngle = PI/3;
            joints.front() = (b2RevoluteJoint*)world.world->CreateJoint(&revoluteJointDef);

            startTime = ofGetElapsedTimeMillis();
        }
    }
    else
    {
        rects.back().setDensity(30.0);
    }




}
示例#2
0
//--------------------------------------------------------
void ttRope::destroy(){
    
    for(int i =0; i<m_num; i++){
        
        if (joints.size()>1)
        {
            
            world.world->DestroyJoint(joints.front());
            world.world->DestroyBody(rects[1].body);
            joints.erase(joints.begin());
            rects.erase(rects.begin()+1);
            
            ofPoint pos = rects.front().getPosition();
            rects.front().setPosition(pos.x, pos.y-20);
            b2RevoluteJointDef revoluteJointDef;
            revoluteJointDef.Initialize(rects[0].body, rects[1].body, rects[0].body->GetWorldCenter());
            
            b2Vec2 p = screenPtToWorldPt(ofPoint(0,9));
            revoluteJointDef.localAnchorA.Set(p.x, p.y);
            revoluteJointDef.localAnchorB.Set(p.x, -p.y);
            joints.front() = (b2RevoluteJoint*)world.world->CreateJoint(&revoluteJointDef);
            
        }
        else if(joints.size()==1)
        {
            world.world->DestroyJoint(joints.back());
            world.world->DestroyBody(rects.back().body);
            joints.clear();
            rects.clear();
        }
    }
 
      m_preNum = m_num;
}
示例#3
0
//----------------------------------------
void ofxBox2dEdge::create(b2World * b2dworld) {
   
    bFlagShapeUpdate = false;
    
    if(size() < 2) {
		printf("need at least 3 points\n");
		return;
	}
	
	if (body != NULL) {
		b2dworld->DestroyBody(body);
		body = NULL;
	}
	
	// create the body from the world (1)
	b2BodyDef		bd;
	bd.type			= density <= 0.0 ? b2_staticBody : b2_dynamicBody;
	body			= b2dworld->CreateBody(&bd);
    
    vector<ofPoint>&pts = ofPolyline::getVertices();
	for(int i=1; i<(int)size(); i++) {
        b2EdgeShape edge;
        edge.Set(screenPtToWorldPt(pts[i-1]), screenPtToWorldPt(pts[i]));
        body->CreateFixture(&edge, density);
    }
    mesh.clear();
    mesh.setUsage(body->GetType()==b2_staticBody?GL_STATIC_DRAW:GL_DYNAMIC_DRAW);
    mesh.setMode(OF_PRIMITIVE_LINE_STRIP);
    for(int i=0; i<(int)size(); i++) {
        mesh.addVertex(ofVec3f(pts[i].x, pts[i].y));
    }
    
    // Temporary hack to ensure it's flagged as changed, until we
    // switch to OF 0.8.0.
    setClosed(isClosed());
    alive = true;
}
示例#4
0
void Ski::create(b2World * b2dworld, float x, float y, float w, float h) {
    
    b2Vec2 center = screenPtToWorldPt(ofVec2f(x,y));
    skiWidth = w;
    skiHeight = h; 
    start = false;
    takeoff = false;
    blow = false;
    wind = 0;
    
    // Define the body and make it from the shape
    b2BodyDef bd;
    bd.type = b2_dynamicBody;
    bd.position.Set(center.x, center.y);
    b2Body* body = b2dworld->CreateBody(&bd);
    
    // Define a polygon (this is what we use for a rectangle)
    b2PolygonShape ps;
    ps.SetAsBox(b2dNum(w/2), b2dNum(h/2));
    
    // Define a fixture
    b2FixtureDef fd;
    fd.shape = &ps;
    // Parameters that affect physics
    fd.density = 20;
    fd.friction = 0.001;
    fd.restitution = 0;
    
    // attach fixture to body
    body->CreateFixture(&fd);
    
    // Give it some initial random velocity
    //body.setLinearVelocity(new Vec2(100,0));
    body->SetAngularVelocity(0);
    
    bodies.push_back(body);
    
    b2Vec2 frontPos = center;
    frontPos.x += b2dNum(skiWidth/2);
    createWheel(b2dworld, frontPos, skiHeight/2);
    b2Vec2 backPos = center;
    backPos.x -= b2dNum(skiWidth/2);
    createWheel(b2dworld, backPos, skiHeight/2);
    
    createJoint(b2dworld, bodies.at(0), bodies.at(1));
    createJoint(b2dworld, bodies.at(0), bodies.at(2));
}
示例#5
0
//-----------------------------------------------
void ttChar::controlRope(){
    
    if (rects.empty()) {
        return;
    }
    
    int size = (fabs(rects[0].getPosition().y - getPos.y)-100)/28+2;
    if (size>15) {
        size = 15;
    }
        if (!joints.empty() && rects.size()>size) {
            int counter = 0;
            for (int i = 0 ; i<rects.size(); i++) {
                if (rects[i].body->GetType() == 0) {
                    counter ++;
                }
            }
        
            if (ofGetElapsedTimeMillis()-startTime>30) {
                world.getWorld()->DestroyJoint(joints.front());
                world.getWorld()->DestroyBody(rects[1].body);
                joints.erase(joints.begin());
                rects.erase(rects.begin()+1);
                
                b2RevoluteJointDef revoluteJointDef;
                revoluteJointDef.Initialize(rects[0].body, rects[1].body, rects[0].body->GetWorldCenter());
                b2Vec2 p = screenPtToWorldPt(ofPoint(0,0));
                revoluteJointDef.localAnchorA.Set(p.x, p.y);
                p = screenPtToWorldPt(ofPoint(-14,0));
                revoluteJointDef.localAnchorB.Set(p.x, p.y);
//                revoluteJointDef.enableLimit = true;
//                revoluteJointDef.lowerAngle = -PI/3;
//                revoluteJointDef.upperAngle = PI/3;
                joints.front() = (b2RevoluteJoint*)world.world->CreateJoint(&revoluteJointDef);
                
                if (counter>1) {
                    startTime = ofGetElapsedTimeMillis();
                }
            }
            
        }
       
//            b2PolygonShape shape;
//            b2Vec2 v2;
//            if (charNum == 0) {
//               
//                v2.Set(b2dNum(0), b2dNum(-30));
//                
//            }
//            else
//            {
//                
//                v2.Set(b2dNum(0), b2dNum(30));
//            }
//
//            shape.SetAsBox(b2dNum(15), b2dNum(30),v2, 0);
//            b2FixtureDef fixture;
//            fixture.shape = &shape;
//            rects.back().body->CreateFixture(&fixture);
           
        

}
示例#6
0
//-----------------------------------------------
void ttChar::copyRope(vector<ofxBox2dRect> Rects, vector<b2RevoluteJoint *> Joints, ofPoint screen){
   
    float rectOff = 14;
    
    for (int i=0; i<Joints.size(); i++) {
        if (joints.empty()) {
            ofxBox2dRect rect;
            ofPoint pos;
            pos = Rects[i].getPosition() - screen;
            rect.setPhysics(Rects[i].density, Rects[i].bounce, Rects[i].friction);
            rect.setup(world.getWorld(), pos.x,pos.y, Rects[i].getWidth(), Rects[i].getHeight());
            rect.body->GetFixtureList()->SetSensor(true);
            rect.body->SetType(Rects[i].body->GetType());
            rect.setVelocity(Rects[i].getVelocity());
            //            rect.setAngle(Rects[i].body->GetAngle()*DEG_TO_RAD);
            rects.push_back(rect);
            
            pos = Rects[i+1].getPosition() - screen;
            rect.setPhysics(Rects[i+1].density, Rects[i+1].bounce, Rects[i+1].friction);
            rect.setup(world.getWorld(), pos.x,pos.y, Rects[i+1].getWidth(), Rects[i+1].getHeight());
            rect.body->GetFixtureList()->SetSensor(true);
            rect.body->SetType(Rects[i+1].body->GetType());
            rect.setVelocity(Rects[i+1].getVelocity());
            //            rect.setAngle(Rects[i+1].getRotation()*DEG_TO_RAD);
            //            rect.body->SetAngularDamping(b2dNum(0.9f));
            rects.push_back(rect);
            
            b2RevoluteJointDef revoluteJointDef;
            revoluteJointDef.Initialize(rects[i].body, rects[i+1].body, rects[0].body->GetWorldCenter());
            b2Vec2 p = screenPtToWorldPt(ofPoint(0,0));
            revoluteJointDef.localAnchorA.Set(p.x, p.y);
            p = screenPtToWorldPt(ofPoint(-rectOff,0));
            revoluteJointDef.localAnchorB.Set(p.x, p.y);
            joints.push_back((b2RevoluteJoint*)world.world->CreateJoint(&revoluteJointDef));
        }else if(i<Joints.size()-1){
            ofxBox2dRect rect;
            ofPoint pos;
            pos = Rects[i+1].getPosition() - screen;
            rect.setPhysics(Rects[i+1].density, Rects[i+1].bounce, Rects[i+1].friction);
            rect.setup(world.getWorld(), pos.x,pos.y, Rects[i+1].getWidth(), Rects[i+1].getHeight());
            rect.body->GetFixtureList()->SetSensor(true);
            rect.body->SetType(Rects[i+1].body->GetType());
            rect.setVelocity(Rects[i+1].getVelocity());
            //            rect.setAngle(Rects[i+1].getRotation()*DEG_TO_RAD);
            rects.push_back(rect);
            
            b2RevoluteJointDef revoluteJointDef;
            revoluteJointDef.Initialize(rects[i].body, rects[i+1].body, rects[0].body->GetWorldCenter());
            b2Vec2 p = screenPtToWorldPt(ofPoint(rectOff,0));
            revoluteJointDef.localAnchorA.Set(p.x, p.y);
            p = screenPtToWorldPt(ofPoint(-rectOff,0));
            revoluteJointDef.localAnchorB.Set(p.x, p.y);
            joints.push_back((b2RevoluteJoint*)world.world->CreateJoint(&revoluteJointDef));
            
        }else{
            ofxBox2dRect rect;
            ofPoint pos;
            pos = Rects[i+1].getPosition() - screen;
            rect.setPhysics(Rects[i+1].density, Rects[i+1].bounce, Rects[i+1].friction);
            rect.setup(world.getWorld(), pos.x,pos.y, Rects[i+1].getWidth(), Rects[i+1].getHeight());
            rect.body->GetFixtureList()->SetSensor(true);
            rect.body->SetType(Rects[i+1].body->GetType());
            rect.setVelocity(Rects[i+1].getVelocity());
            //            rect.setAngle(Rects[i+1].getRotation()*DEG_TO_RAD);
            rects.push_back(rect);
            
            b2RevoluteJointDef revoluteJointDef;
            revoluteJointDef.Initialize(rects[i].body, rects[i+1].body, rects[0].body->GetWorldCenter());
            b2Vec2 p = screenPtToWorldPt(ofPoint(rectOff,0));
            revoluteJointDef.localAnchorA.Set(p.x, p.y);
            p = screenPtToWorldPt(ofPoint(0,0));
            revoluteJointDef.localAnchorB.Set(p.x, p.y);
            joints.push_back((b2RevoluteJoint*)world.world->CreateJoint(&revoluteJointDef));
        }
    }
    
    //    b2RevoluteJointDef revoluteJointDef;
    //    revoluteJointDef.Initialize(rects.back().body, character.body, rects.back().body->GetWorldCenter());
    //    b2Vec2 p = screenPtToWorldPt(ofPoint(0,0));
    //    revoluteJointDef.localAnchorA.Set(p.x, p.y);
    //    p = screenPtToWorldPt(ofPoint(15,0));
    //    revoluteJointDef.localAnchorB.Set(p.x, p.y);
    //    joints.push_back((b2RevoluteJoint*)world.world->CreateJoint(&revoluteJointDef));



}
示例#7
0
//----------------------------------------
void ofxBox2dPolygon::create(b2World * b2dworld) {

	if(size() < 3) {
		printf("need at least 3 points\n");
		return;	
	}
	
	if (body != NULL) {
		b2dworld->DestroyBody(body);
		body = NULL;
	}
	
	// create the body from the world (1)
	b2BodyDef		bd;
	bd.type			= density <= 0.0 ? b2_staticBody : b2_dynamicBody;
	body			= b2dworld->CreateBody(&bd);

	if(bIsTriangulated) {
		
		b2PolygonShape	shape;
		b2FixtureDef	fixture;
		b2Vec2			verts[3];
		
		ofVec2f a, b, c;
		for (int i=0; i<triangles.size(); i++) {
			
			a = triangles[i].a;
			b = triangles[i].b;
			c = triangles[i].c;

			verts[0].Set(a.x/OFX_BOX2D_SCALE, a.y/OFX_BOX2D_SCALE);
			verts[1].Set(b.x/OFX_BOX2D_SCALE, b.y/OFX_BOX2D_SCALE);
			verts[2].Set(c.x/OFX_BOX2D_SCALE, c.y/OFX_BOX2D_SCALE);
			
			shape.Set(verts, 3);
			
			fixture.density		= density;
			fixture.restitution = bounce;
			fixture.friction	= friction;
			fixture.shape		= &shape;
			body->CreateFixture(&fixture);
		}
	
	}
	else {
		if(bSetAsEdge) {
			for (int i=1; i<size(); i++) {
				b2EdgeShape	shape;
				b2Vec2 a = screenPtToWorldPt(getVertices()[i-1]);
				b2Vec2 b = screenPtToWorldPt(getVertices()[i]);
				shape.Set(a, b);
				fixture.shape		= &shape;
				fixture.density		= density;
				fixture.restitution = bounce;
				fixture.friction	= friction;	
				
				body->CreateFixture(&fixture);
			}	
		}
		else {
            vector<b2Vec2>verts;
            verts.assign(size()-1, b2Vec2());
			for (int i=0; i<size(); i++) {
				ofVec2f p = getVertices()[i] / OFX_BOX2D_SCALE;
				verts[i]  = b2Vec2(p.x, p.y);
			}
			b2PolygonShape	shape;
			shape.Set(&verts[0], size()-1);
			
			fixture.shape		= &shape;
			fixture.density		= density;
			fixture.restitution = bounce;
			fixture.friction	= friction;	
			
			body->CreateFixture(&fixture);
		}		
			
	}
	
	// update the area and centroid
	updateShape();
}
示例#8
0
//----------------------------------------------
void ttChar::setup(ofxBox2d &characterWorld,
                   ttControl &ctrl_A,
                   ttControl &ctrl_B,
                   ofPoint SetPos,
                   int iCharNum){
   
    world = characterWorld;
    control_A = &ctrl_A;
    control_B = &ctrl_B;
    setWidth = 15;
    setHeight = 30;
    step = 0;
  
    bFixedMove = false;
    setPos = SetPos;
    getPos = SetPos;
    charNum = iCharNum;
    bSwing = false;
    if(charNum ==1)mirrorLeft = false;
    if(charNum ==0)mirrorLeft = true;
    bDead = false;
    deadStep = 2;
    hold_Num = 0;
    
    color.set(255, 255, 255, 255);
    character.setPhysics(40.f, 0.0f, 0.95f);
    character.setup(world.getWorld(), setPos.x, setPos.y, setWidth, setHeight);
    character.body->SetFixedRotation(true);
    character.body->SetLinearDamping(b2dNum(0.95));
    numFootContacts = 0;
    adjustedHeight = 85;
    ofDirectory dir;
    int nFiles;
    b2Vec2 v2;
    if (charNum == 0) {
        nFiles = dir.listDir("sprites/girl");
        v2 = screenPtToWorldPt(ofPoint(0,-30));
     
    }
    else
    {
        nFiles  = dir.listDir("sprites/boy");
        v2 = screenPtToWorldPt(ofPoint(0,30));
    }
 
    if (nFiles) {
        for (int i= 0; i<dir.numFiles(); i++) {
            string filePath = dir.getPath(i);
            sprite.push_back(ofImage());
            sprite.back().loadImage(filePath);
        }
    }
    
    b2PolygonShape shape;
    shape.SetAsBox(b2dNum(10), b2dNum(10), v2, b2dNum(0));
	b2FixtureDef fixture;
    fixture.isSensor = true;
    fixture.shape = &shape;
    b2Fixture* footSensorFixture = character.body->CreateFixture(&fixture);
    footSensorFixture->SetUserData(new ttSetData());
    ttSetData * sd = (ttSetData*)footSensorFixture->GetUserData();
    sd->name	= "footSenser";
    
}
//----------------------------------------
void ofxBox2dPolygon::create(b2World * b2dworld) {

	if(size() <= 3) {
		ofLog(OF_LOG_NOTICE, "need at least 3 points: %i\n", (int)size());
		return;	
	}
	
	if (body != NULL) {
		b2dworld->DestroyBody(body);
		body = NULL;
	}
	
	// create the body from the world (1)
	b2BodyDef		bd;
	bd.type			= density <= 0.0 ? b2_staticBody : b2_dynamicBody;
	body			= b2dworld->CreateBody(&bd);

	if(bIsTriangulated) {
		
		b2PolygonShape	shape;
		b2FixtureDef	fixture;
		b2Vec2			verts[3];
		
		ofVec2f a, b, c;
		for (int i=0; i<triangles.size(); i++) {
			
			a = triangles[i].a;
			b = triangles[i].b;
			c = triangles[i].c;

			verts[0].Set(a.x/OFX_BOX2D_SCALE, a.y/OFX_BOX2D_SCALE);
			verts[1].Set(b.x/OFX_BOX2D_SCALE, b.y/OFX_BOX2D_SCALE);
			verts[2].Set(c.x/OFX_BOX2D_SCALE, c.y/OFX_BOX2D_SCALE);
			
			shape.Set(verts, 3);
			
			fixture.density		= density;
			fixture.restitution = bounce;
			fixture.friction	= friction;
			fixture.shape		= &shape;
			body->CreateFixture(&fixture);
		}
	
	}
	else {
        makeConvexPoly();
		vector<ofPoint> pts = ofPolyline::getVertices();
        vector<b2Vec2>verts;
        for (int i=0; i<MIN((int)pts.size(), b2_maxPolygonVertices); i++) {
            verts.push_back(screenPtToWorldPt(pts[i]));
        }
        b2PolygonShape shape;
        shape.Set(&verts[0], verts.size()-1);
        
        fixture.shape		= &shape;
        fixture.density		= density;
        fixture.restitution = bounce;
        fixture.friction	= friction;
        
        body->CreateFixture(&fixture);
    
        
    }
    
    vector<ofPoint> pts = ofPolyline::getVertices();
    mesh.clear();
    ofPath path;
    ofPoint center = getCentroid2D();
    for (int i=0; i<pts.size(); i++) {
        ofPoint p(pts[i].x, pts[i].y);
        p -= center;
        path.lineTo(p);
    }
    mesh = path.getTessellation();
    mesh.setUsage(GL_STATIC_DRAW);

    flagHasChanged();
    alive = true;
}
示例#10
0
//--------------------------------------------------------
void ttRope::initialize(ofPoint pos){

    
    for(int i =0; i<m_num; i++){
        
        if (joints.empty()) {
            ofxBox2dRect rect;
            rect.setup(world.getWorld(), pos.x, pos.y, 1,1);
            rect.body->GetFixtureList()->SetSensor(true);
            rects.push_back(rect);
            
            rect.setPhysics(0.03f, 0.0f, 0.0f);
            rect.setup(world.world, rects[0].getPosition().x+9, rects[0].getPosition().y, 15, 2);
            rect.body->GetFixtureList()->SetSensor(true);
            rects.push_back(rect);
            
            b2RevoluteJointDef revoluteJointDef;
            revoluteJointDef.Initialize(rects[0].body, rects.back().body, rects[0].body->GetWorldCenter());
            b2Vec2 p = screenPtToWorldPt(ofPoint(0,0));
            revoluteJointDef.localAnchorA.Set(p.x, p.y);
            p = screenPtToWorldPt(ofPoint(-14,0));
            revoluteJointDef.localAnchorB.Set(p.x, p.y);
            revoluteJointDef.enableLimit = true;
            revoluteJointDef.lowerAngle = -180*DEG_TO_RAD;
            revoluteJointDef.upperAngle = 180*DEG_TO_RAD;
            joints.push_back((b2RevoluteJoint*)world.world->CreateJoint(&revoluteJointDef));
            
        }else if(i<m_num-1){
            
            ofxBox2dRect rect;
            rect.setPhysics(0.03f, 0.0f, 0.0f);
            rect.setup(world.world, rects.back().getPosition().x, rects.back().getPosition().y, 15, 2);
            rect.body->GetFixtureList()->SetSensor(true);
            rects.push_back(rect);
            
            b2RevoluteJointDef revoluteJointDef;
            revoluteJointDef.Initialize(rects[rects.size()-2].body, rects.back().body, rects.back().body->GetWorldCenter());
            b2Vec2 p;
            if(i%2==1){
                p = screenPtToWorldPt(ofPoint(14,0));
            }
            else{
                p = screenPtToWorldPt(ofPoint(-14,0));
            }
            
            revoluteJointDef.localAnchorA.Set(p.x, p.y);
            revoluteJointDef.localAnchorB.Set(p.x, p.y);
            joints.push_back((b2RevoluteJoint*)world.world->CreateJoint(&revoluteJointDef));
            
        }else{
            ofxBox2dRect rect;
            rect.setPhysics(0.10f, 0.0f, 0.0f);
            if(i%2==1){
            rect.setup(world.world, rects.back().getPosition().x+9, rects.back().getPosition().y, 5, 5);
            }else{
            rect.setup(world.world, rects.back().getPosition().x-9, rects.back().getPosition().y, 5, 5);
            }
            
            rect.body->GetFixtureList()->SetSensor(true);
            rect.body->SetFixedRotation(true);
            rects.push_back(rect);
            
            b2RevoluteJointDef revoluteJointDef;
            revoluteJointDef.Initialize(rects[rects.size()-2].body, rects.back().body, rects[rects.size()-2].body->GetWorldCenter());
            b2Vec2 p1;
            b2Vec2 p2;
            if(i%2==1){
                p1 = screenPtToWorldPt(ofPoint(14,0));
                p2 = screenPtToWorldPt(ofPoint(0,0));
            }
            else{
                p1 = screenPtToWorldPt(ofPoint(-14,0));
                p2 = screenPtToWorldPt(ofPoint(0,0));
            }
            
            revoluteJointDef.localAnchorA.Set(p1.x, p1.y);
            revoluteJointDef.localAnchorB.Set(p2.x, p2.y);
            joints.push_back((b2RevoluteJoint*)world.world->CreateJoint(&revoluteJointDef));
        }
    }
    
    for (int i =0; i<rects.size(); i++) {
        rects[i].body->SetType(b2_staticBody);
    }

}