IntersectionData ofxIntersection::LinePlaneIntersection(IsLine& line, IsPlane& plane){ IntersectionData idata; float dist1=PointPlaneDistance(line.p0, plane); float dist2=PointPlaneDistance(line.p1, plane); int pos1=ofSign(dist1); int pos2=ofSign(dist2); if(pos1==pos2){ idata.isIntersection=false; return idata; }; float u=0; float denom = plane.getNormal().dot(line.getVec()); // check if ray is paralles to plane: if (fabs(denom) > EPS) { u= plane.getNormal().dot(plane.getP0()-line.getP0())/denom; // check if intersection is within line-segment: if(u>1.0 || u<0){ idata.isIntersection=false; return idata; } idata.isIntersection=true; idata.pos.set(line.getP0()+line.getVec()*u); return idata; }else{ idata.isIntersection=false; return idata; } }
//UPDATE ------------------------------------------------------------- void Screen_game::update(){ background.update(); if(robotFalling == true ){ if(ofSign(prevRotation) == -1 && fabs(prevRotation) <= 90.0){ prevRotation -= 1; fallPositionX -= 5; fallPositionY -= 2; }else if(ofSign(prevRotation) == 1 && fabs(prevRotation) <= 90.0){ prevRotation += 1; fallPositionX += 5; fallPositionY -= 2; }else if(fabs(prevRotation) >= 90.0){ robotFalling = false; new_num_lives = num_lives - 1; } }else{ fallPositionX = 0; fallPositionY = 0; } }
//-------------------------------------------------------------- void testApp::mouseDragged(int x, int y, int button){ prevMouse = currMouse; currMouse = ofPoint(x,y); float time = (float)(ofGetElapsedTimeMillis() - timeElapsed); timeElapsed = ofGetElapsedTimeMillis(); float dist = ofDist(currMouse.x, currMouse.y, prevMouse.x, prevMouse.y); float speed = dist/time; //cout << "speed " << speed << endl; float newwidth = MAX(0.5,1/speed); _x=x; _y=y; if (ofDist(x, y, targetPos.x, targetPos.y)<80) //Hit the target! { //change the sound randomly int i = (int)ofRandom(0,files.size()); currentSound = files[i]; //change the position of the target targetPos.x =(int)ofRandom(0,ofGetWidth()); targetPos.y = (int)ofRandom(0, ofGetHeight()); blobColor.b = (int)ofRandom(0,255); } if (speed > 0.1 && speed<1.5) { float xacc = ofSign(x-prevMouse.x)*(pow(abs((int)(_x-lastBlobPosition.x)),0.3)); float yacc = ofSign(y-prevMouse.y)*(pow(abs((int)(_y-lastBlobPosition.y)),0.3)); blobs.push_back(blob(x,y,xacc,yacc,BLOB_SIZE, currentSound,blobColor)); } }
IntersectionData ofxIntersection::PlaneTriangleIntersection(IsPlane& plane, IsTriangle& triangle){ IntersectionData idata; ofVec3f tp0=triangle.getP0(); ofVec3f tp1=triangle.getP1(); ofVec3f tp2=triangle.getP2(); float dist1=PointPlaneDistance(tp0, plane); float dist2=PointPlaneDistance(tp1, plane); float dist3=PointPlaneDistance(tp2, plane); int pos1=ofSign(dist1); int pos2=ofSign(dist2); int pos3=ofSign(dist3); if(pos1==pos2 && pos1==pos3){ idata.isIntersection=false; return idata; }; vector<ofPoint>ispoints; bool bintersects=false; ofVec3f ip; if(pos1!=pos2){ ip=LinePlaneIntersectionFast(tp0, tp1, plane); if(!containsValue(&ispoints, ip)){ ispoints.push_back(ip); }; } if(pos2!=pos3){ ip=LinePlaneIntersectionFast(tp1, tp2, plane); if(!containsValue(&ispoints, ip)){ ispoints.push_back(ip); }; } if(pos3!=pos1){ ip=LinePlaneIntersectionFast(tp2, tp0, plane); if(!containsValue(&ispoints, ip)){ ispoints.push_back(ip); }; } idata.isIntersection=true; idata.pos.set(ispoints.at(0)); if(ispoints.size()==2){ idata.dir.set(ispoints.at(1)); idata.dir-=idata.pos; idata.dist=idata.dir.length(); } return idata; }
//-------------------------------------------------------------- void ofApp::draw(){ ofBackground( 255, 255, 255 ); //Set the background color int w = 1024; //Width of the sound visualization picture //Graph of the sound buffer ofSetColor( 0, 0, 0 ); for (int x=0; x<w; x++) { int i = float(x) * N / w; //Convert x to buffer's position ofLine( x, 100, x, 100 - buffer[i] * 100 ); } //Stripe image for (int x=0; x<w; x++) { int i = float(x) * N / w; //Convert x to buffer's position float v = buffer[i]; //Get buffer value //Apply square-root transformation //and also shift by 0.2 to make the picture brighter v = ofSign( v ) * sqrt( fabs( v ) ) + 0.2; //Convert v to range [0,255] float col = ofMap( v, -1, 1, 0, 255, true ); //Set color and draw vertical line ofSetColor( col, col, col ); ofLine( x, 200, x, 300 ); } }
void testApp::updateTripno(float dt) { float signal = 0; double blockWidth = gameField.width / SEGMENTS_PER_VIEWPORT; tripno.position.x = gameField.width * 0.3; // pop max signal from the control data if (control.size() - lastSignalIndex > 1) { soundMutex.lock(); signal = (*max_element(control.begin() + lastSignalIndex, control.end())) * config.signalAmp; lastSignalIndex = control.size() - 1; soundMutex.unlock(); } signal = signal != signal ? 0 : signal; tripno.dbgSignal = signal ? signal : tripno.dbgSignal; tripno.elastic = - config.elasticKoeff * tripno.position.y; tripno.resistance = - ofSign(tripno.velocity) * tripno.velocity * tripno.velocity * config.resistanceKoeff; double acceleration = (signal/* as control force */ + tripno.elastic + tripno.resistance) * tripno.mass; tripno.position.y += tripno.velocity * dt + acceleration * dt * dt; tripno.velocity += acceleration * dt; int blockIndex = tripno.position.x / blockWidth; int tripnoY = getTripnoAbsoluteY(); if (tripnoY <= gameField.y + ceilHeights[blockIndex] || tripnoY >= gameField.y + gameField.height - floorHeights[blockIndex]) { tripno.velocity *= -1; } }
vector <double> ofxURDriver::getAchievablePosition(vector <double> position){ float maxAccelDeg = 500.0; float maxSpeedPct = 1.0; if( !bMove && deccelCount > 0 ){ maxSpeedPct = ofMap(deccelCount, 1, numDeccelSteps-1, 0.0, 1.0, true); if( maxSpeedPct < 0.9 ){ acceleratePct = 0; } //cout << " deccelCount " << deccelCount << " maxSpeedPct " << maxSpeedPct << endl; } if( bMove ){ acceleratePct += 0.02; if( acceleratePct > 1.0 ){ acceleratePct = 1.0; } } maxSpeedPct *= acceleratePct; //this seeems to do much better with a hardcoded timedelta float timeDiff = 1.0/125.0;//timeNow-lastTimeSentMove; if( currentRobotPositionRadians.size() && position.size() ){ bool bHasSpeed = true; vector <double> lastSpeed = calculatedSpeed; if( calculatedSpeed.size() != position.size() ){ calculatedSpeed.assign(position.size(), 0); lastSpeed = calculatedSpeed; bHasSpeed = false; } for(int d= 0; d < position.size(); d++){ calculatedSpeed[d] = (position[d]-currentRobotPositionRadians[d])/timeDiff; } vector <double> acceleration; acceleration.assign(calculatedSpeed.size(), 0); for(int d = 0; d < acceleration.size(); d++){ acceleration[d] = (calculatedSpeed[d]-lastSpeed[d])/timeDiff; float accelDegPerSec = ofRadToDeg(acceleration[d]); //this is the max accel reccomended. //if we are over it we limit the new position to being the old position plus the current speed, plus the max acceleration //this seems to actually work - f**k yes! if( fabs( accelDegPerSec ) > maxAccelDeg ){ //cout << d << " currentRobotPositionRadians is " << ofRadToDeg( currentRobotPositionRadians[d] ) << " request is " << ofRadToDeg(position[d]) << " speed is " << ofRadToDeg( calculatedSpeed[d] ) << " prev Speed is " << ofRadToDeg(lastSpeed[d]) << " accel is " << accelDegPerSec << endl; float newAccel = ofDegToRad( maxAccelDeg ) * (float)ofSign(accelDegPerSec); float speedDiff = newAccel * timeDiff; float targetSpeed = lastSpeed[d] + speedDiff; position[d] = currentRobotPositionRadians[d] + ( targetSpeed * timeDiff * maxSpeedPct ); //cout << "---- hit limit: accel is " << ofRadToDeg(newAccel) << " targetSpeed is now " << ofRadToDeg(targetSpeed) << " pos is now " << position[d] << endl; }else if( maxSpeedPct < 1.0 ){ position[d] = currentRobotPositionRadians[d] + ( currentSpeed[d] * timeDiff * maxSpeedPct ); } } } return position; }
//-------------------------------------------------------------- void testApp::update(){ // update the sound playing system: ofSoundUpdate(); //OpenCV stuff if (!usingMouseInput) { bool bNewFrame = false; vidGrabber.grabFrame(); bNewFrame = vidGrabber.isFrameNew(); if (bNewFrame){ colorImg.setFromPixels(vidGrabber.getPixels(), 320,240); grayImage = colorImg; } // take the abs value of the difference between background and incoming and then threshold: grayDiff.absDiff(grayBg, grayImage); grayDiff.threshold(threshold); // find contours which are between the size of 20 pixels and 1/3 the w*h pixels. // also, find holes is set to true so we will get interior contours as well.... contourFinder.findContours(grayDiff, 20, 320*240/3, 10, true); // find holes if (contourFinder.nBlobs>0) { lastBlobPosition = blobPosition; if (_x<ofGetWidth()/2) { _x = blobPosition.x = ofGetWidth()-contourFinder.blobs[0].boundingRect.x*ofGetWidth()/320; } else _x = blobPosition.x = ofGetWidth()-(contourFinder.blobs[0].boundingRect.x+contourFinder.blobs[0].boundingRect.width)*ofGetWidth()/320; _y = blobPosition.y = contourFinder.blobs[0].boundingRect.y*ofGetHeight()/240; indexSmoothPos = (indexSmoothPos+1)%LOWPASS; smoothPos[indexSmoothPos].x = _x; smoothPos[indexSmoothPos].y = _y; float tmp_x, tmp_y; tmp_x = tmp_y =0; for (int i = 0 ; i<LOWPASS ; i++) { tmp_x+=smoothPos[i].x; tmp_y+=smoothPos[i].y; } _x = tmp_x/LOWPASS; _y = tmp_y/LOWPASS; } } if (!explode && ofDist(_x, _y, targetPos.x, targetPos.y)<80) //Hit the target! { explode = true; explosionTimer = ofGetElapsedTimeMillis(); transparency = 180; //change the sound randomly int i = (int)ofRandom(0,files.size()); currentSound = files[i]; //cout <<numFiles<< " " << i << " sound changed to:" << currentSound << endl; //change the position of the target blobColor.b = (int)ofRandom(40,205); } if (!usingMouseInput) { float time = (float)(ofGetElapsedTimeMillis() - timeElapsed); timeElapsed = ofGetElapsedTimeMillis(); ofPoint lastPos = smoothPos[(indexSmoothPos+LOWPASS+1)%LOWPASS]; float dist = ofDist(_x, _y, lastPos.x,lastPos.y); float speed = pow(dist/time,0.3); float newwidth = MAX(0.5,1/speed); if (speed > 0.1 && speed < 1.5) { float xacc = ofSign(blobPosition.x-lastBlobPosition.x)*(pow(abs((int)(_x-lastBlobPosition.x)),0.3)); float yacc = ofSign(blobPosition.y-lastBlobPosition.y)*(pow(abs((int)(_y-lastBlobPosition.y)),0.3)); // cout << "xacc, yacc " << xacc << " " << yacc << endl; blobs.push_back(blob(_x,_y,xacc,yacc,BLOB_SIZE+(int)(10/(speed)), currentSound, blobColor)); cout << xacc << " " << yacc <<" speed "<< speed << endl; } } if (bLearnBakground == true){ grayBg = grayImage; // the = sign copys the pixels from grayImage into grayBg (operator overloading) // bLearnBakground = false; } }