Further work on Cloud class

This commit is contained in:
Philip Rosedale 2012-11-18 10:45:41 -08:00
parent 86d9230181
commit 4174364adc
6 changed files with 42 additions and 33 deletions

View file

@ -63,34 +63,21 @@ void Cloud::render() {
void Cloud::simulate (float deltaTime) {
int i;
float verts[3], fadd[3], fval[3];
for (i = 0; i < count; ++i) {
// Update position
//particles[i].position += particles[i].velocity*deltaTime;
particles[i].position += particles[i].velocity;
// Drag: decay velocity
// Decay Velocity (Drag)
const float CONSTANT_DAMPING = 1.0;
particles[i].velocity *= (1.f - CONSTANT_DAMPING*deltaTime);
// Read from field
verts[0] = particles[i].position.x;
verts[1] = particles[i].position.y;
verts[2] = particles[i].position.z;
field_value(fval, &verts[0]);
particles[i].velocity.x += fval[0];
particles[i].velocity.y += fval[1];
particles[i].velocity.z += fval[2];
// Add back to field
// Interact with Field
const float FIELD_COUPLE = 0.0000001;
fadd[0] = particles[i].velocity.x*FIELD_COUPLE;
fadd[1] = particles[i].velocity.y*FIELD_COUPLE;
fadd[2] = particles[i].velocity.z*FIELD_COUPLE;
field_add(fadd, &verts[0]);
field_interact(&particles[i].position, &particles[i].velocity, FIELD_COUPLE);
// Bounce or Wrap
if (wrapBounds) {
// wrap around bounds
if (particles[i].position.x > bounds.x)

View file

@ -60,6 +60,21 @@ void field_add(float* add, float *pos)
}
}
void field_interact(glm::vec3 * pos, glm::vec3 * vel, float coupling) {
int index = (int)(pos->x/WORLD_SIZE*10.0) +
(int)(pos->y/WORLD_SIZE*10.0)*10 +
(int)(pos->z/WORLD_SIZE*10.0)*100;
if ((index >= 0) && (index < FIELD_ELEMENTS)) {
// Add velocity to particle from field
*vel += field[index].val;
// Add back to field from particle velocity
glm::vec3 temp = *vel;
temp *= coupling;
field[index].val += temp;
}
}
void field_avg_neighbors(int index, glm::vec3 * result) {
// Given index to field element i, return neighbor field values
glm::vec3 neighbors(0,0,0);

View file

@ -27,6 +27,7 @@ void field_init();
int field_value(float *ret, float *pos);
void field_render();
void field_add(float* add, float *loc);
void field_interact(glm::vec3 * pos, glm::vec3 * vel, float coupling);
void field_simulate(float dt);
#endif

View file

@ -8,11 +8,11 @@
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "field.cpp"
timestampString = "372274896.176083"
timestampString = "374955033.430214"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "83"
endingLineNumber = "83"
startingLineNumber = "98"
endingLineNumber = "98"
landmarkName = "field_avg_neighbors(int index, glm::vec3 * result)"
landmarkType = "7">
</FileBreakpoint>

View file

@ -58,6 +58,7 @@ using namespace std;
// Junk for talking to the Serial Port
int serial_on = 0; // Is serial connection on/off? System will try
int audio_on = 0; // Whether to turn on the audio support
int simulate_on = 1;
// Network Socket Stuff
// For testing, add milliseconds of delay for received UDP packets
@ -96,13 +97,13 @@ Hand myHand(HAND_RADIUS,
glm::vec3 box(WORLD_SIZE,WORLD_SIZE,WORLD_SIZE);
ParticleSystem balls(0,
box,
false, // Wrap?
0.02, // Noise
false, // Wrap?
0.02, // Noise
0.3, // Size scale
0.0 // Gravity
0.0 // Gravity
);
Cloud cloud(0, // Particles
Cloud cloud(250000, // Particles
box, // Bounding Box
false // Wrap
);
@ -120,7 +121,7 @@ Cloud cloud(0, // Particles
#define RENDER_FRAME_MSECS 10
#define SLEEP 0
#define NUM_TRIS 250000
#define NUM_TRIS 0
struct {
float vertices[NUM_TRIS * 3];
float vel [NUM_TRIS * 3];
@ -394,6 +395,7 @@ void update_tris()
if (tris.element[i] == 1)
{
// Read and add velocity from field
field_value(field_val, &tris.vertices[i*3]);
tris.vel[i*3] += field_val[0];
@ -406,6 +408,7 @@ void update_tris()
field_contrib[1] = tris.vel[i*3+1]*FIELD_COUPLE;
field_contrib[2] = tris.vel[i*3+2]*FIELD_COUPLE;
field_add(field_contrib, &tris.vertices[i*3]);
}
// bounce at edge of world
@ -734,7 +737,7 @@ void key(unsigned char k, int x, int y)
if (k == '/') stats_on = !stats_on; // toggle stats
if (k == 'n')
{
noise_on = !noise_on; // Toggle noise
noise_on = !noise_on; // Toggle noise
if (noise_on)
{
myHand.setNoise(noise);
@ -759,6 +762,7 @@ void key(unsigned char k, int x, int y)
if (k == ' ') reset_sensors();
if (k == 'a') render_yaw_rate -= 0.25;
if (k == 'd') render_yaw_rate += 0.25;
if (k == 'o') simulate_on = !simulate_on;
if (k == 'p')
{
// Add to field vector
@ -808,12 +812,14 @@ void idle(void)
{
// Simulation
update_pos(1.f/FPS);
update_tris();
field_simulate(1.f/FPS);
myHead.simulate(1.f/FPS);
myHand.simulate(1.f/FPS);
balls.simulate(1.f/FPS);
cloud.simulate(1.f/FPS);
if (simulate_on) {
update_tris();
field_simulate(1.f/FPS);
myHead.simulate(1.f/FPS);
myHand.simulate(1.f/FPS);
balls.simulate(1.f/FPS);
cloud.simulate(1.f/FPS);
}
if (!step_on) glutPostRedisplay();
last_frame = check;