Merge pull request #150 from PhilipRosedale/master

Added balls class for avatar simulation, removed particles class.
This commit is contained in:
ZappoMan 2013-04-26 10:07:44 -07:00
commit 4916da39fa
3 changed files with 147 additions and 1 deletions

View file

@ -19,6 +19,8 @@
using namespace std;
const bool BALLS_ON = false;
float skinColor[] = {1.0, 0.84, 0.66};
float lightBlue[] = { 0.7, 0.8, 1.0 };
float browColor[] = {210.0/255.0, 105.0/255.0, 30.0/255.0};
@ -130,7 +132,8 @@ Avatar::Avatar(bool isMine) {
}
}
_balls = new Balls(10);
if (BALLS_ON) { _balls = new Balls(100); }
else { _balls = NULL; }
}

106
interface/src/Balls.cpp Normal file
View file

@ -0,0 +1,106 @@
//
// Balls.cpp
// hifi
//
// Created by Philip on 4/25/13.
//
// A cloud of spring-mass spheres to simulate the avatar body/skin. Each ball
// connects to as many as 4 neighbors, and executes motion according to a damped
// spring, while responding physically to other avatars.
//
#include "Balls.h"
Balls::Balls(int numberOfBalls) {
_numberOfBalls = numberOfBalls;
_balls = new Ball[_numberOfBalls];
for (unsigned int i = 0; i < _numberOfBalls; ++i) {
_balls[i].position = glm::vec3(1.0 + randFloat()*0.5,
0.5 + randFloat()*0.5,
1.0 + randFloat()*0.5);
_balls[i].radius = 0.02 + randFloat()*0.06;
for (unsigned int j = 0; j < NUMBER_SPRINGS; ++j) {
_balls[i].links[j] = rand() % (numberOfBalls + 1);
if (_balls[i].links[j]-1 == i) { _balls[i].links[j] = 0; }
_balls[i].springLength[j] = 0.5;
}
}
}
const bool RENDER_SPRINGS = true;
void Balls::render() {
// Render Balls NOTE: This needs to become something other that GlutSpheres!
glColor3f(0.62,0.74,0.91);
for (unsigned int i = 0; i < _numberOfBalls; ++i) {
glPushMatrix();
glTranslatef(_balls[i].position.x, _balls[i].position.y, _balls[i].position.z);
glutSolidSphere(_balls[i].radius, 15, 15);
glPopMatrix();
}
// Render springs
if (RENDER_SPRINGS) {
glColor3f(0.74,0.91,0.62);
for (unsigned int i = 0; i < _numberOfBalls; ++i) {
glBegin(GL_LINES);
for (unsigned int j = 0; j < NUMBER_SPRINGS; ++j) {
if(_balls[i].links[j] > 0) {
glVertex3f(_balls[i].position.x,
_balls[i].position.y,
_balls[i].position.z);
glVertex3f(_balls[_balls[i].links[j]-1].position.x,
_balls[_balls[i].links[j]-1].position.y,
_balls[_balls[i].links[j]-1].position.z);
}
}
glEnd();
}
}
}
const float CONSTANT_VELOCITY_DAMPING = 1.0f;
const float NOISE_SCALE = 0.00;
const float SPRING_FORCE = 1.0;
const float SPRING_DAMPING = 1.0;
void Balls::simulate(float deltaTime) {
for (unsigned int i = 0; i < _numberOfBalls; ++i) {
// Move particles
_balls[i].position += _balls[i].velocity * deltaTime;
// Drag: decay velocity
_balls[i].velocity *= (1.f - CONSTANT_VELOCITY_DAMPING*deltaTime);
// Add noise
_balls[i].velocity += glm::vec3((randFloat() - 0.5) * NOISE_SCALE,
(randFloat() - 0.5) * NOISE_SCALE,
(randFloat() - 0.5) * NOISE_SCALE);
// 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);
}
}
}
}

37
interface/src/Balls.h Normal file
View file

@ -0,0 +1,37 @@
//
// Balls.h
// hifi
//
// Created by Philip on 4/25/13.
//
//
#ifndef hifi_Balls_h
#define hifi_Balls_h
#include <glm/glm.hpp>
#include "Util.h"
#include "world.h"
#include "InterfaceConfig.h"
const int NUMBER_SPRINGS = 4;
class Balls {
public:
Balls(int numberOfBalls);
void simulate(float deltaTime);
void render();
private:
struct Ball {
glm::vec3 position, velocity;
int links[NUMBER_SPRINGS];
float springLength[NUMBER_SPRINGS];
float radius;
} *_balls;
int _numberOfBalls;
};
#endif