Beispiel #1
0
//-----------------------------------------------
void ttChar::destroyRope(){
    
    if (joints.empty()) {
        return;
    }
    
    for(int i =joints.size()-1; i>=0; i--){
        world.world->DestroyJoint(joints[i]);
    }
    
    joints.clear();
    
    for(int i =0; i<rects.size(); i++){
        if (charNum == 0) {
        rects[i].setVelocity(ofRandom(-10,10),ofRandom(0,5));
        }else{
        rects[i].setVelocity(ofRandom(-10,10),ofRandom(-5,0));
        }
        
        rects[i].body->SetFixedRotation(false);
        rects[i].body->SetAngularDamping(b2dNum(0));
        rects[i].body->SetAngularVelocity(b2dNum(ofRandom(-3000,3000)));
    }

    
}
//------------------------------------------------ 
void ofxBox2dBaseShape::setPosition(float x, float y) {
	if(!body || body == NULL) {
		ofLog(OF_LOG_NOTICE, "- Body is NULL -");
		return;
	}
	body->SetTransform(b2Vec2(b2dNum(x), b2dNum(y)), 0);
	body->SetLinearVelocity(b2Vec2(0, 0)); // maybe bring this back...
	body->SetAwake(true); // this sounds backwards but that is what the doc says todo...
}
Beispiel #3
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));
}
Beispiel #4
0
void Ski::createWheel(b2World* b2dworld, b2Vec2 pos, float r) {
    b2BodyDef bd;
    bd.type = b2_dynamicBody;
    bd.position.Set(pos.x, pos.y);
    b2Body* body = b2dworld->CreateBody(&bd);
    
    b2CircleShape cs;
    cs.m_radius = b2dNum(r);
    
    b2FixtureDef fd;
    fd.shape = &cs;
    fd.density = 10;
    fd.friction = 0.001;
    fd.restitution = 0;
    body->CreateFixture(&fd);
    
//    body->SetLinearVelocity(b2Vec2(100,0));
    body->SetAngularVelocity(0);
    
    bodies.push_back(body);
}
Beispiel #5
0
//----------------------------------------------
void ttChar::setup(ofxBox2d &characterWorld,
                   ttControl &ctrl,
                   ofPoint SetPos,
                   ofPoint &Acc,
                   int iCharNum){
   
    world = characterWorld;
    control = &ctrl;
    accFroce = &Acc;
    setWidth = 15;
    setHeight = 30;
    step = 0;
   
    setPos = SetPos;
    getPos = SetPos;
    charNum = iCharNum;
    
    if(charNum ==1)mirrorLeft = false;
    if(charNum ==0)mirrorLeft = true;
    bSwing = false;
    bDead = false;
    bDestroyRect = false;
    bInSky = false;
    alpha = 255;
    deadStep = 2;
    hold_Num = 0;
    
    color.set(255, 255, 255, 255);
    character.setPhysics(4.0f, 0.1f, 0.1);
    
    character.setup(world.getWorld(), setPos.x, setPos.y, setWidth, setHeight);
    character.body->SetLinearDamping(b2dNum(0.999));
    character.body->SetFixedRotation(true);
    numFootContacts = 0;
    adjustedHeight = 85;
    ofDirectory dir;
    int nFiles;
    b2Vec2 v2;
    if (charNum == 0) {
        nFiles = dir.listDir("sprites/girl");
        v2.Set(b2dNum(0), b2dNum(-30));
     
    }
    else
    {
        nFiles  = dir.listDir("sprites/boy");
        v2.Set(b2dNum(0), b2dNum(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";
    
    startTime = ofGetElapsedTimeMillis();
    
    cout<<dir.numFiles()<<endl;
}
Beispiel #6
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";
    
}
Beispiel #7
0
//----------------------------------------------
void ttChar::setup(ofxBox2d &characterWorld,
                   ttControl &ctrl,
                   ofPoint SetPos,
                   ofPoint &Acc,
                   int iCharNum){
   
    world = characterWorld;
    control = &ctrl;
    accForce = &Acc;
    setWidth = 15;
    setHeight = 30;
    step = 0;
   
    setPos = SetPos;
    getPos = SetPos;
    charNum = iCharNum;
    
    if(charNum ==1)mirrorLeft = false;
    if(charNum ==0)mirrorLeft = true;
    bSwing = false;
    bDead = false;
    bDestroyRect = false;
    bInSky = false;
    alpha = 255;
    deadStep = 2;
    hold_Num = 0;
    
    color.set(255, 255, 255, 255);
    character.setPhysics(4.0f, 0.1f, 0.1);
    
    character.setup(world.getWorld(), setPos.x, setPos.y, setWidth, setHeight);
    character.body->SetLinearDamping(b2dNum(0.999));
    character.body->SetFixedRotation(true);
    numFootContacts = 0;
    adjustedHeight = 85;
    ofDirectory walkDir;
    int walknFiles;
    b2Vec2 v2;
    if (charNum == 0) {
        walknFiles = walkDir.listDir("sprites/girl");
        v2.Set(b2dNum(0), b2dNum(-30));
     
    }
    else
    {
        walknFiles  = walkDir.listDir("sprites/boy");
        v2.Set(b2dNum(0), b2dNum(30));
    }
 
    if (walknFiles) {
        for (int i= 0; i<walkDir.numFiles(); i++) {
            string filePath = walkDir.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";
    
    startTime = ofGetElapsedTimeMillis();
    
    // frame index April 22
    //            girl      boy
    //walk        0-19      52-68
    //die         20-26     0-6
    //fall        27-36
    //starthung   37-42     7-9
    //hung        43-54     10-19
    //startpull   55-70     20-37
    //pull        71-81     38-51
    
    //set sprite constants
    if (charNum == 0) {
        fOffsetWalk         = 0 ;
        fOffsetDie          = 20;
        fOffsetFall         = 27;
        fOffsetStarthung    = 37;
        fOffsetHung         = 43;
        fOffsetStartpull    = 55;
        fOffsetPull         = 71;
        
        fAmountWalk         = 19;
        fAmountDie          = 26-20;
        fAmountFall         = 36-27;
        fAmountStarthung    = 42-37;
        fAmountHung         = 54-43;
        fAmountStartpull    = 70-55;
        fAmountPull         = 81-71;
    }
    else{
        fOffsetWalk         = 52 ;
        fOffsetDie          = 0;
//        fOffsetFall         = ;
        fOffsetStarthung    = 7;
        fOffsetHung         = 10;
        fOffsetStartpull    = 20;
        fOffsetPull         = 38;
        
        fAmountWalk         = 68-52;
        fAmountDie          = 6;
//        fAmountFall         = 36-27;
        fAmountStarthung    = 42-37;
        fAmountHung         = 54-43;
        fAmountStartpull    = 70-55;
        fAmountPull         = 81-71;
        
    }
}
Beispiel #8
0
void PowerChange::showAt(float x, float y) 
{
	body->body->SetTransform(b2Vec2(b2dNum(x), b2dNum(y)), ofRandom(HALF_PI));
	millis = ofGetElapsedTimeMillis();
	enabled = true;
}
//----------------------------------------
void ofxBox2dJoint::setLength(float len) {
	if(joint) {
		joint->SetLength((float32)b2dNum(len));
	}
}
Beispiel #10
0
//----------------------------------------
void ofxBox2dDistanceJoint::setLength(float val){
    joint->SetLength((float32)b2dNum(val));
}