예제 #1
0
// Make some bucky balls for the avatar
BuckyBalls::BuckyBalls() {
    _bballIsGrabbed[0] = 0;
    _bballIsGrabbed[1] = 0;
    colors[0] = glm::vec3(0.13f, 0.55f, 0.13f);
    colors[1] = glm::vec3(0.64f, 0.16f, 0.16f);
    colors[2] = glm::vec3(0.31f, 0.58f, 0.80f);

    qDebug("Creating buckyballs...");
    for (int i = 0; i < NUM_BBALLS; i++) {
        _bballPosition[i] = CORNER_BBALLS + randVector() * RANGE_BBALLS;
        int element = (rand() % NUM_ELEMENTS);
        if (element == 0) {
            _bballRadius[i] = SIZE_BBALLS;
            _bballColor[i] = colors[0];
        } else if (element == 1) {
            _bballRadius[i] = SIZE_BBALLS / 2.f;
            _bballColor[i] = colors[1];
        } else {
            _bballRadius[i] = SIZE_BBALLS * 2.f;
            _bballColor[i] = colors[2];
        }
        _bballColliding[i] = 0.f;
        _bballElement[i] = element;
        if (_bballElement[i] != 1) {
            _bballVelocity[i] = randVector() * INITIAL_VELOCITY;
        } else {
            _bballVelocity[i] = glm::vec3(0);
        }
    }
}
예제 #2
0
int main(int argc, char** argv) {
    
    srand(time(NULL));    // seed random
    
    std::cout << CLEAR;
    
    // get 2 vector objects based on randomly generated 2D Vectors
    Vector2D vectorA = randVector();
    Vector2D vectorB = randVector();
    
    // output Vectors to console
    std::cout << "vectorA = " << vectorA.getX() << "x + " <<
            vectorA.getY() << "y\n";
    std::cout << "vectorB = " << vectorB.getX() << "x + " <<
            vectorB.getY() << "y\n";
    std::cout << "\nLAB ASSIGNMENT PART 1: VECTOR DOT PRODUCT WITH * "
            << "OPERATOR OVERLOAD\n";
    std::cout << "--------------------------------------------------------"
            <<"----------\n";
    std::cout << "Dot product = vectorA * vectorB = " << vectorA * vectorB
            << std::endl;
    
    // Lab part 2 - do vector addition overload with + operator
    std::cout << "\nLAB ASSIGNMENT PART 2: VECTOR ADDITION WITH + "
            << "OPERATOR OVERLOAD\n";
    std::cout << "-------------------------------------------------------"
            <<"--------\n";
    Vector2D vectorC = vectorA + vectorB;
    std::cout << "Vector addition = vectorA + vectorB = " <<
            (vectorC).getX() << "x + " <<
            (vectorC).getY() << "y\n";
            

    return 0;
}
예제 #3
0
Ingredient::Ingredient(const Vector &pos, IngredientData *data, int amount)
 : Entity(),  data(data), amount(amount), gone(false), used(false)
{
	addType(SCO_INGREDIENT);
	entityType = ET_INGREDIENT;
	position = pos;
	lifeSpan = 30;
	if (data)
	{
		setTexture("Ingredients/"+data->gfx);
	}
	int mag = 600;
	if (isRotKind())
		velocity = randVector(mag)*0.5f + Vector(0, -mag)*0.5f;
	else
		velocity = Vector(0,-mag*0.5f);
	gravity = Vector(0, 250); //300
	scale = Vector(0.2,0.2);
	scale.interpolateTo(Vector(1, 1), 0.75);

	if (isRotKind())
		rotation.z = randAngle360();

	layer = LR_ENTITIES;
}
예제 #4
0
std::vector<int> GenerateMyRandomVector(size_t fSize, int fMin, int fMax) {
    std::vector<int> randVector(fSize);
    std::random_device rd;
    for (int i = 0; i < fSize; ++i) {
        randVector[i] = rd() % (1 + fMax - fMin) + fMin;
    }
    return randVector;
}
예제 #5
0
파일: Balls.cpp 프로젝트: Freidrica/hifi
void Balls::simulate(float deltaTime) {
    for (unsigned int i = 0; i < _numberOfBalls; ++i) {
        
        // Move particles
        _balls[i].position += _balls[i].velocity * deltaTime;
        _balls[i].targetPosition += _balls[i].velocity * deltaTime;
        
        // Drag: decay velocity
        _balls[i].velocity *= (1.f - CONSTANT_VELOCITY_DAMPING * deltaTime);
        
        // Add noise
        _balls[i].velocity += randVector() * NOISE_SCALE;
        
        //  spring force to origin
        float separation = glm::distance(_balls[i].position,
                                         _origin);

        _balls[i].velocity +=
            glm::normalize(_balls[i].position - _origin)
            * deltaTime
            *
        SPRING_FORCE *
        (ORIGIN_DISTANCE - separation);
        
        // Approach target position
//        for (unsigned int i = 0; i < _numberOfBalls; ++i) {
//            _balls[i].position += randFloat() * deltaTime * (_balls[i].targetPosition - _balls[i].position);
//        }

        // Spring Force
        
        /*
        for (unsigned int j = 0; j < NUMBER_SPRINGS; ++j) {
            if(_balls[i].links[j] > 0) {
                float separation = glm::distance(_balls[i].position,
                                                _balls[_balls[i].links[j]-1].position);
                _balls[i].velocity += glm::normalize(_balls[i].position -
                                                   _balls[_balls[i].links[j]-1].position) *
                                        deltaTime *
                                        SPRING_FORCE *
                                        (_balls[i].springLength[j] - separation);
                
                //_balls[i].velocity *= (1.f - SPRING_DAMPING*deltaTime);

            }
        } */
         
         
        

    }
}
예제 #6
0
void tst_QGLBuilder::addQuadRandom()
{
    QFETCH(int, size);
    QFETCH(int, type);

    int n = qSqrt(size);
    size = n * n;
    QVector3DArray data;
    data.reserve(size);
    for (int i = 0; i < size; ++i)
    {
        // make sure (in face of randomness) we get a planar quad
        QVector3D origin = randVector();
        QVector3D a;
        while (a.isNull())
            a = randVector();
        QVector3D b;
        while (b.isNull())
            b = randVector();
        data.append(origin, a, a+b, b);
    }
    addQuadBenchMarks(data, type);
}
예제 #7
0
파일: Balls.cpp 프로젝트: Freidrica/hifi
Balls::Balls(int numberOfBalls) {
    _numberOfBalls = numberOfBalls;
    _balls = new Ball[_numberOfBalls];
    for (unsigned int i = 0; i < _numberOfBalls; ++i) {
        _balls[i].position = randVector() * INITIAL_AREA;
        _balls[i].targetPosition = _balls[i].position;
        _balls[i].velocity = glm::vec3(0, 0, 0);
        _balls[i].radius = BALL_RADIUS;
        for (unsigned int j = 0; j < NUMBER_SPRINGS; ++j) {
            _balls[i].links[j] = 0;
          }
    }
    _color = INITIAL_COLOR;
    _origin = glm::vec3(0, 0, 0);
}
예제 #8
0
inline void FMPWPartTmpl<GainTmpl>::
initGrouping2()
{
  vector<unsigned int> randVector(getNumPartitions());
  for (unsigned int k=0; k<getNumPartitions(); ++k) {
    _moveTo[k] = k;
    _groupMap[k] = 255;
    randVector[k] = k;
  }
  std::random_shuffle(randVector.begin(), randVector.end());
  for (unsigned int j=0; j<_numGroups; ++j) {
    const unsigned int i = _numGroups + j;
    _moveTo[randVector[j]] = randVector[i];
    _moveTo[randVector[i]] = randVector[j];
    _groupMap[randVector[j]] = j;
    _groupMap[randVector[i]] = j;
    _groupInvMap[j] = randVector[j];
  }
}
예제 #9
0
void MainWindow::on_action_packetDelete_triggered()
{
    std::map<std::string,std::string> wav = file->wav_header->getHeader();

    if(wav.empty())
        return;

    Dialog_PacketDelete *dpd = new Dialog_PacketDelete(sizeinbytes,this);

    if(dpd->exec()) //show dialog
    {
        this->packetLength = dpd->packet_length;
        int packets_amount = sizeinbytes/this->packetLength;   // size of new array: data divided into packets

        algorithms->setPacketsAmount(packets_amount);
        algorithms->container->createPackets(this->packetLength); // create packets from bytes

        if(dpd->to_delete > 0)
        {
            del_index = randVector(1,packets_amount,dpd->to_delete);
            this->plotReplot(del_index);
            const QCPDataMap *dataMap = ui->customPlot->graph(0)->data();
            QVector <double> y;
            for(QMap<double, QCPData>::const_iterator it = dataMap->constBegin();
                it!=dataMap->constEnd(); ++it)
            {
                y.push_back(it.value().value);
            }
            file->wav_header->convert2data(y);
        }

        if(dpd->to_delete > 0)
        {
            setWindowTitle(windowTitle().remove("*"));
            setWindowTitle(windowTitle() + "*");
        }
    }
    delete dpd;

    // backup data with deleted packets
    memcpy(algorithms->container->data_del,algorithms->container->data,sizeof(packet) * sizeinbytes);
}
예제 #10
0
파일: Hair.cpp 프로젝트: RyanDowne/hifi
Hair::Hair(int strands,
           int links,
           float radius,
           float linkLength,
           float hairThickness) :
    _strands(strands),
    _links(links),
    _linkLength(linkLength),
    _hairThickness(hairThickness),
    _radius(radius),
    _acceleration(0.0f),
    _angularVelocity(0.0f),
    _angularAcceleration(0.0f),
    _gravity(DEFAULT_GRAVITY),
    _loudness(0.0f)
{
    _hairPosition = new glm::vec3[_strands * _links];
    _hairOriginalPosition = new glm::vec3[_strands * _links];
    _hairLastPosition = new glm::vec3[_strands * _links];
    _hairQuadDelta = new glm::vec3[_strands * _links];
    _hairNormals = new glm::vec3[_strands * _links];
    _hairColors = new glm::vec3[_strands * _links];
    _hairIsMoveable = new int[_strands * _links];
    _hairConstraints = new int[_strands * _links * HAIR_CONSTRAINTS];     // Hair can link to two others
    glm::vec3 thisVertex;
    for (int strand = 0; strand < _strands; strand++) {
        float strandAngle = randFloat() * PI;

        float azimuth = (float)strand / (float)_strands * PI * 2.0f;
        float elevation = 0.0f;
        
        glm::vec3 thisStrand(sinf(azimuth) * cosf(elevation), sinf(elevation), -cosf(azimuth) * cosf(elevation));
        thisStrand *= _radius;
        
        for (int link = 0; link < _links; link++) {
            int vertexIndex = strand * _links + link;
            //  Clear constraints
            for (int link2 = 0; link2 < HAIR_CONSTRAINTS; link2++) {
                _hairConstraints[vertexIndex * HAIR_CONSTRAINTS + link2] = -1;
            }
            if (vertexIndex % _links == 0) {
                //  start of strand
                thisVertex = thisStrand;
            } else {
                thisVertex+= glm::normalize(thisStrand) * _linkLength;
                //  Set constraints to vertex before and maybe vertex after in strand
                _hairConstraints[vertexIndex * HAIR_CONSTRAINTS] = vertexIndex - 1;
                if (link < (_links - 1)) {
                    _hairConstraints[vertexIndex * HAIR_CONSTRAINTS + 1] = vertexIndex + 1;
                }
            }
            _hairOriginalPosition[vertexIndex] = _hairLastPosition[vertexIndex] = _hairPosition[vertexIndex] = thisVertex;
            
            _hairQuadDelta[vertexIndex] = glm::vec3(cos(strandAngle) * _hairThickness, 0.0f, sin(strandAngle) * _hairThickness);
            _hairQuadDelta[vertexIndex] *= ((float)link / _links);
            _hairNormals[vertexIndex] = glm::normalize(randVector());
            if (randFloat() < elevation / PI_OVER_TWO) {
                _hairColors[vertexIndex] = HAIR_COLOR1 * ((float)(link + 1) / (float)_links);
            } else {
                _hairColors[vertexIndex] = HAIR_COLOR2 * ((float)(link + 1) / (float)_links);
            }
        }
    }
}
예제 #11
0
파일: Hair.cpp 프로젝트: RyanDowne/hifi
void Hair::simulate(float deltaTime) {
    deltaTime = glm::clamp(deltaTime, 0.0f, 1.0f / 30.0f);
    glm::vec3 acceleration = _acceleration;
    if (glm::length(acceleration) > HAIR_MAX_LINEAR_ACCELERATION) {
        acceleration = glm::normalize(acceleration) * HAIR_MAX_LINEAR_ACCELERATION;
    }
    
    for (int strand = 0; strand < _strands; strand++) {
        for (int link = 0; link < _links; link++) {
            int vertexIndex = strand * _links + link;
            if (vertexIndex % _links == 0) {
                //  Base Joint - no integration
            } else {
                //
                //  Vertlet Integration
                //
                //  Add velocity from last position, with damping
                glm::vec3 thisPosition = _hairPosition[vertexIndex];
                glm::vec3 diff = thisPosition - _hairLastPosition[vertexIndex];
                _hairPosition[vertexIndex] += diff * HAIR_DAMPING;
                
                /*
                //  Resolve collisions with sphere
                if (glm::length(_hairPosition[vertexIndex]) < _radius) {
                    _hairPosition[vertexIndex] += glm::normalize(_hairPosition[vertexIndex]) *
                    (_radius - glm::length(_hairPosition[vertexIndex]));
                } */
                
                //  Collide with a conical body descending from the root of the hair
                glm::vec3 thisVertex = _hairPosition[vertexIndex];
                float depth = -thisVertex.y;
                thisVertex.y = 0.0f;
                const float BODY_CONE_ANGLE = 0.30;
                if (glm::length(thisVertex) < depth * BODY_CONE_ANGLE) {
                    _hairPosition[vertexIndex] += glm::normalize(thisVertex) * (depth * BODY_CONE_ANGLE - glm::length(thisVertex));
                }
                
                //  Add random thing driven by loudness
                float loudnessFactor = (_loudness > SOUND_THRESHOLD) ? logf(_loudness - SOUND_THRESHOLD) / 2000.0f : 0.0f;

                const float QUIESCENT_LOUDNESS = 0.0f;
                _hairPosition[vertexIndex] += randVector() * (QUIESCENT_LOUDNESS + loudnessFactor) * ((float)link / (float)_links);

                //  Add gravity
                const float SCALE_GRAVITY = 0.001f;
                _hairPosition[vertexIndex] += _gravity * deltaTime * SCALE_GRAVITY;
                
                //  Add linear acceleration
                _hairPosition[vertexIndex] -= acceleration * HAIR_ACCELERATION_COUPLING * deltaTime;
                
                //  Add stiffness to return to original position
                _hairPosition[vertexIndex] += (_hairOriginalPosition[vertexIndex] - _hairPosition[vertexIndex])
                * powf(1.0f - (float)link / _links, 2.0f) * HAIR_STIFFNESS;
                
                //  Add angular acceleration
                const float ANGULAR_VELOCITY_MIN = 0.001f;
                if (glm::length(_angularVelocity) > ANGULAR_VELOCITY_MIN) {
                    glm::vec3 yawVector = _hairPosition[vertexIndex];
                    glm::vec3 angularVelocity = _angularVelocity * HAIR_ANGULAR_VELOCITY_COUPLING;
                    glm::vec3 angularAcceleration = _angularAcceleration * HAIR_ANGULAR_ACCELERATION_COUPLING;
                    yawVector.y = 0.0f;
                    if (glm::length(yawVector) > EPSILON) {
                        float radius = glm::length(yawVector);
                        yawVector = glm::normalize(yawVector);
                        float angle = atan2f(yawVector.x, -yawVector.z) + PI;
                        glm::vec3 delta = glm::vec3(-1.0f, 0.0f, 0.0f) * glm::angleAxis(angle, glm::vec3(0, 1, 0));
                        _hairPosition[vertexIndex] -= delta * radius * (angularVelocity.y - angularAcceleration.y) * deltaTime;
                    }
                    glm::vec3 pitchVector = _hairPosition[vertexIndex];
                    pitchVector.x = 0.0f;
                    if (glm::length(pitchVector) > EPSILON) {
                        float radius = glm::length(pitchVector);
                        pitchVector = glm::normalize(pitchVector);
                        float angle = atan2f(pitchVector.y, -pitchVector.z) + PI;
                        glm::vec3 delta = glm::vec3(0.0f, 1.0f, 0.0f) * glm::angleAxis(angle, glm::vec3(1, 0, 0));
                        _hairPosition[vertexIndex] -= delta * radius * (angularVelocity.x - angularAcceleration.x) * deltaTime;
                    }
                    glm::vec3 rollVector = _hairPosition[vertexIndex];
                    rollVector.z = 0.0f;
                    if (glm::length(rollVector) > EPSILON) {
                        float radius = glm::length(rollVector);
                        pitchVector = glm::normalize(rollVector);
                        float angle = atan2f(rollVector.x, rollVector.y) + PI;
                        glm::vec3 delta = glm::vec3(-1.0f, 0.0f, 0.0f) * glm::angleAxis(angle, glm::vec3(0, 0, 1));
                        _hairPosition[vertexIndex] -= delta * radius * (angularVelocity.z - angularAcceleration.z) * deltaTime;
                    }
                }
                
                //  Impose link constraints
                for (int link = 0; link < HAIR_CONSTRAINTS; link++) {
                    if (_hairConstraints[vertexIndex * HAIR_CONSTRAINTS + link] > -1) {
                        //  If there is a constraint, try to enforce it
                        glm::vec3 vectorBetween = _hairPosition[_hairConstraints[vertexIndex * HAIR_CONSTRAINTS + link]] - _hairPosition[vertexIndex];
                        _hairPosition[vertexIndex] += glm::normalize(vectorBetween) * (glm::length(vectorBetween) - _linkLength) * CONSTRAINT_RELAXATION * deltaTime;
                    }
                }
                
                //  Store start position for next verlet pass
                _hairLastPosition[vertexIndex] = thisPosition;
            }
        }
    }
}