void p5drawingApp::update() { mouseDir = mousePos - mouseLast; if( abs(mouseDir.x) + abs(mouseDir.y) > 2.0f ){ mouseDir.safeNormalize(); vectorValues.push_back(mouseDir); for( list<Vec2f>::iterator listIterator = vectorValues.begin(); listIterator != vectorValues.end(); ++listIterator ) { Vec2f vec = Vec2f(listIterator->x, listIterator->y); mouseDir += vec; } mouseDir /= valAverageCount; angleOrig = math<float>::atan2(mouseDir.x, mouseDir.y); anglePlus = angleOrig + M_PI/2; angleMinus = angleOrig - M_PI/2; Vec2f mouseVelocity = mousePos - mouseLast; perpLength = abs(mouseVelocity.x) + abs(mouseVelocity.y) + 5; lengthValues.push_back(perpLength); perpLength = 0.0f; for( list<float>::iterator listIterator = lengthValues.begin(); listIterator != lengthValues.end(); ++listIterator ) { float length = *listIterator; perpLength += length; } perpLength /= valAverageCount; mouseDirPlus = Vec2f( sin(anglePlus), cos(anglePlus) ); mouseDirPlus.safeNormalize(); mouseDirPlus *= perpLength; mouseDirMinus = Vec2f( sin(angleMinus), cos(angleMinus) ); mouseDirMinus.safeNormalize(); mouseDirMinus *= perpLength; mouseDir *= 35.0f; vStart = Vec3f(mousePos.x - mouseDir.x, mousePos.y - mouseDir.y, 0.0f); vEnd = Vec3f(mousePos.x, mousePos.y, 0.0f); vEndPlus = Vec3f(mousePos.x + mouseDirPlus.x, mousePos.y + mouseDirPlus.y, 0.0f); vEndMinus = Vec3f(mousePos.x + mouseDirMinus.x, mousePos.y + mouseDirMinus.y, 0.0f); mouseLast = mousePos; vectorValues.pop_front(); lengthValues.pop_front(); } }
Vec2f Food::attract(SnakeRef snake) { Vec2f force = mLocation - snake->getLocation(); float distance = force.length(); distance = math<float>::clamp(distance, 10.0f, 25.0f); float strength = (mAmount * snake->getHunger()) / ( distance * distance ) / 10; force.safeNormalize(); force *= strength; return force; }
Vec2f Repeller::repel(Particle * p) { Vec2f dir = location - p->location; float d = constrain((float)dir.length(), 5.0f, 100.0f); dir.safeNormalize(); float G = 1200; float force = -1.0f * G / ( d * d ); return dir * force; }
void Spring::connect( Mover * mover ) { Vec2f force = mover->position - anchor; float d = force.length(); float stretch = d - len; force.safeNormalize(); force *= -1 * k * stretch; mover->applyForce(force); }
Vec2f Mover::attract(MoverRef m) { float G = 0.4; Vec2f force = mLocation - m->mLocation; float distance = force.length(); distance = math<float>::clamp(distance, 5.0, 25.0); force.safeNormalize(); float strength = (G * mMass * m->mMass) / (distance * distance); force *= strength; return force; }
Vec2f Content::seek(vector<Vec2f> aPoints){ float minDis= windowWidth * 2; for(int i = aPoints.size()-1; i >= 0; i--){ float dis = aPoints[i].distance( Vec2f(mLocation.x, mLocation.y) ); if(dis < minDis) { seekIndex = i; minDis = dis; } } Vec2f dir = aPoints[seekIndex] - Vec2f(mLocation.x, mLocation.y); Vec2f steer = dir - Vec2f( mVelocity.x, mVelocity.y ); steer.safeNormalize(); //steer.x = constrain(steer.x, -1.2f, -0.4f); steer *= maxSpeed; steer.x = 0; attracted = true; return steer*0.1; }