From 642b64dfd6d0664530fd97ff0f94bb7dd26d3a50 Mon Sep 17 00:00:00 2001 From: Philip Rosedale Date: Tue, 26 Mar 2013 17:32:54 -0700 Subject: [PATCH] First commit to improve avatar movement --- interface/src/Head.cpp | 49 +++++++++++++++++++---- interface/src/Head.h | 19 +++++++-- interface/src/main.cpp | 89 +++++++++++++++++++++--------------------- 3 files changed, 101 insertions(+), 56 deletions(-) diff --git a/interface/src/Head.cpp b/interface/src/Head.cpp index 65fd4089ef..c6f28dab4b 100644 --- a/interface/src/Head.cpp +++ b/interface/src/Head.cpp @@ -212,10 +212,46 @@ void Head::setLeanSideways(float dist){ // Simulate the head over time void Head::simulate(float deltaTime) { - // Increment position as a function of velocity - position += velocity * deltaTime; + + glm::vec3 forward(-sinf(getRenderYaw()*PI/180), + sinf(getRenderPitch()*PI/180), + cosf(getRenderYaw()*PI/180)); + + thrust = glm::vec3(0); + const float THRUST_MAG = 6.0; + const float THRUST_LATERAL_MAG = 6.0; + const float THRUST_VERTICAL_MAG = 6.0; + + if (driveKeys[FWD]) { + thrust += THRUST_MAG*forward; + } + if (driveKeys[BACK]) { + thrust += -THRUST_MAG*forward; + } + if (driveKeys[RIGHT]) { + thrust.x += forward.z*-THRUST_LATERAL_MAG; + thrust.z += forward.x*THRUST_LATERAL_MAG; + } + if (driveKeys[LEFT]) { + thrust.x += forward.z*THRUST_LATERAL_MAG; + thrust.z += forward.x*-THRUST_LATERAL_MAG; + } + if (driveKeys[UP]) { + thrust.y += -THRUST_VERTICAL_MAG; + } + if (driveKeys[DOWN]) { + thrust.y += THRUST_VERTICAL_MAG; + } + // Increment velocity as time velocity += thrust * deltaTime; + + // Increment position as a function of velocity + position += velocity * deltaTime; + + // Decay velocity + const float LIN_VEL_DECAY = 5.0; + velocity *= (1.0 - LIN_VEL_DECAY*deltaTime); if (!noise) { @@ -315,13 +351,10 @@ void Head::simulate(float deltaTime) } -void Head::render(int faceToFace, int isMine, float * myLocation) +void Head::render(int faceToFace, int isMine) { int side = 0; - - glm::vec3 cameraHead(myLocation[0], myLocation[1], myLocation[2]); - float distanceToCamera = glm::distance(cameraHead, position); - + // Always render own hand, but don't render head unless showing face2face glEnable(GL_DEPTH_TEST); glPushMatrix(); @@ -334,7 +367,7 @@ void Head::render(int faceToFace, int isMine, float * myLocation) hand->render(1); // Don't render a head if it is really close to your location, because that is your own head! - if ((distanceToCamera > 1.0) || faceToFace) { + if (!isMine || faceToFace) { glRotatef(Pitch, 1, 0, 0); glRotatef(Roll, 0, 0, 1); diff --git a/interface/src/Head.h b/interface/src/Head.h index 2c5733d28d..0aece7fe6f 100644 --- a/interface/src/Head.h +++ b/interface/src/Head.h @@ -20,6 +20,13 @@ enum eyeContactTargets {LEFT_EYE, RIGHT_EYE, MOUTH}; +#define FWD 0 +#define BACK 1 +#define LEFT 2 +#define RIGHT 3 +#define UP 4 +#define DOWN 5 + class Head : public AgentData { public: Head(); @@ -49,7 +56,7 @@ class Head : public AgentData { float getYaw() {return Yaw;} float getLastMeasuredYaw() {return YawRate;} - void render(int faceToFace, int isMine, float * myLocation); + void render(int faceToFace, int isMine); void simulate(float); // Send and receive network data @@ -65,7 +72,11 @@ class Head : public AgentData { glm::vec3 getPos() { return position; }; void setPos(glm::vec3 newpos) { position = newpos; }; - // Set/Get update the thrust that will move the avatar around + // Set what driving keys are being pressed to control thrust levels + void setDriveKeys(int key, bool val) { driveKeys[key] = val; }; + bool getDriveKeys(int key) { return driveKeys[key]; }; + + // Set/Get update the thrust that will move the avatar around void setThrust(glm::vec3 newThrust) { thrust = newThrust; }; void addThrust(glm::vec3 newThrust) { thrust += newThrust; }; glm::vec3 getThrust() { return thrust; }; @@ -111,8 +122,8 @@ class Head : public AgentData { glm::vec3 velocity; glm::vec3 thrust; - bool fwdKey, backKey, turnLeftKey, turnRightKey, slideLeftKey, slideRightKey, upKey, downKey; - + int driveKeys[6]; + int eyeContact; eyeContactTargets eyeContactTarget; diff --git a/interface/src/main.cpp b/interface/src/main.cpp index 3e66775bc1..c9b71f1975 100644 --- a/interface/src/main.cpp +++ b/interface/src/main.cpp @@ -121,14 +121,9 @@ float render_yaw_rate = 0.f; float render_pitch_rate = 0.f; float lateral_vel = 0.f; -// Manage speed and direction of motion -GLfloat fwd_vec[] = {0.0, 0.0, 1.0}; -//GLfloat start_location[] = { WORLD_SIZE*1.5, -WORLD_SIZE/2.0, -WORLD_SIZE/3.0}; -//GLfloat start_location[] = { 0.1, -0.15, 0.1}; +// Where one's own agent begins in the world (needs to become a dynamic thing passed to the program) +glm::vec3 start_location(6.1f, 0, 1.4f); -GLfloat start_location[] = {6.1f, 0, 1.4f}; - -GLfloat location[] = {start_location[0], start_location[1], start_location[2]}; float fwd_vel = 0.0f; int stats_on = 0; // Whether to show onscreen text overlay with stats @@ -319,6 +314,7 @@ void init(void) if (noise_on) { myHead.setNoise(noise); } + myHead.setPos(start_location); char output[] = "I"; char address[] = "10.0.0.10"; @@ -365,12 +361,10 @@ void reset_sensors() yaw = render_yaw_rate = 0; pitch = render_pitch = render_pitch_rate = 0; lateral_vel = 0; - location[0] = start_location[0]; - location[1] = start_location[1]; - location[2] = start_location[2]; + myHead.setPos(start_location); fwd_vel = 0.0; head_mouse_x = WIDTH/2; - head_mouse_y = HEIGHT/2; + head_mouse_y = HEIGHT/2; head_lean_x = WIDTH/2; head_lean_y = HEIGHT/2; @@ -483,26 +477,11 @@ void simulateHead(float frametime) }*/ // Decrease forward velocity fwd_vel *= (1.f - 4.0*frametime); - - - // Update forward vector based on pitch and yaw - fwd_vec[0] = -sinf(myHead.getRenderYaw()*PI/180); - fwd_vec[1] = sinf(render_pitch*PI/180); - fwd_vec[2] = cosf(myHead.getRenderYaw()*PI/180); - - // Advance location forward - location[0] += fwd_vec[0]*fwd_vel; - location[1] += fwd_vec[1]*fwd_vel; - location[2] += fwd_vec[2]*fwd_vel; - - // Slide location sideways - location[0] += fwd_vec[2]*-lateral_vel; - location[2] += fwd_vec[0]*lateral_vel; - + // Update own head data myHead.setRenderYaw(myHead.getRenderYaw() + render_yaw_rate); myHead.setRenderPitch(render_pitch); - myHead.setPos(glm::vec3(location[0], location[1], location[2])); + //myHead.setPos(glm::vec3(location[0], location[1], location[2])); // Get audio loudness data from audio input device float loudness, averageLoudness; @@ -552,12 +531,11 @@ void display(void) // Rotate, translate to camera location glRotatef(myHead.getRenderPitch(), 1, 0, 0); glRotatef(myHead.getRenderYaw(), 0, 1, 0); - glTranslatef(location[0], location[1], location[2]); + glTranslatef(myHead.getPos().x, myHead.getPos().y, myHead.getPos().z); glColor3f(1,0,0); glutSolidSphere(0.25, 15, 15); - // Draw cloud of dots glDisable( GL_POINT_SPRITE_ARB ); glDisable( GL_TEXTURE_2D ); @@ -576,7 +554,7 @@ void display(void) glPushMatrix(); glm::vec3 pos = agentHead->getPos(); glTranslatef(-pos.x, -pos.y, -pos.z); - agentHead->render(0, 0, &location[0]); + agentHead->render(0, 0); glPopMatrix(); } } @@ -590,12 +568,8 @@ void display(void) glPushMatrix(); glLoadIdentity(); glTranslatef(0.f, 0.f, -7.f); - myHead.render(display_head, 1, &location[0]); + myHead.render(display_head, 1); glPopMatrix(); - - //glm::vec3 test(0.5, 0.5, 0.5); - //render_vector(&test); - glPopMatrix(); @@ -715,23 +689,50 @@ const float KEYBOARD_YAW_RATE = 0.8; const float KEYBOARD_STRAFE_RATE = 0.03; const float KEYBOARD_FLY_RATE = 0.08; +void specialkeyUp(int k, int x, int y) { + if (k == GLUT_KEY_UP) { + myHead.setDriveKeys(FWD, 0); + myHead.setDriveKeys(UP, 0); + } + if (k == GLUT_KEY_DOWN) { + myHead.setDriveKeys(BACK, 0); + myHead.setDriveKeys(DOWN, 0); + } + if (k == GLUT_KEY_LEFT) myHead.setDriveKeys(LEFT, 0); + if (k == GLUT_KEY_RIGHT) myHead.setDriveKeys(RIGHT, 0); + +} + void specialkey(int k, int x, int y) { if (k == GLUT_KEY_UP || k == GLUT_KEY_DOWN || k == GLUT_KEY_LEFT || k == GLUT_KEY_RIGHT) { - if (k == GLUT_KEY_UP) fwd_vel += KEYBOARD_FLY_RATE; - if (k == GLUT_KEY_DOWN) fwd_vel -= KEYBOARD_FLY_RATE; + if (k == GLUT_KEY_UP) { + if (glutGetModifiers() == GLUT_ACTIVE_SHIFT) myHead.setDriveKeys(UP, 1); + else myHead.setDriveKeys(FWD, 1); + } + if (k == GLUT_KEY_DOWN) { + if (glutGetModifiers() == GLUT_ACTIVE_SHIFT) myHead.setDriveKeys(DOWN, 1); + else myHead.setDriveKeys(BACK, 1); + } if (k == GLUT_KEY_LEFT) { - if (glutGetModifiers() == GLUT_ACTIVE_SHIFT) lateral_vel -= KEYBOARD_STRAFE_RATE; + if (glutGetModifiers() == GLUT_ACTIVE_SHIFT) myHead.setDriveKeys(LEFT, 1); else render_yaw_rate -= KEYBOARD_YAW_RATE; } if (k == GLUT_KEY_RIGHT) { - if (glutGetModifiers() == GLUT_ACTIVE_SHIFT) lateral_vel += KEYBOARD_STRAFE_RATE; + if (glutGetModifiers() == GLUT_ACTIVE_SHIFT) myHead.setDriveKeys(RIGHT, 1); else render_yaw_rate += KEYBOARD_YAW_RATE; } audio.setWalkingState(true); } } + + +void keyUp(unsigned char k, int x, int y) { + if (k == 'e') myHead.setDriveKeys(UP, 0); + if (k == 'c') myHead.setDriveKeys(DOWN, 0); +} + void key(unsigned char k, int x, int y) { @@ -764,10 +765,8 @@ void key(unsigned char k, int x, int y) if (k == 'f') display_field = !display_field; if (k == 'l') display_levels = !display_levels; - - - if (k == 'e') location[1] -= WORLD_SIZE/100.0; - if (k == 'c') location[1] += WORLD_SIZE/100.0; + if (k == 'e') myHead.setDriveKeys(UP, 1); + if (k == 'c') myHead.setDriveKeys(DOWN, 1); if (k == 'w') fwd_vel += KEYBOARD_FLY_RATE; if (k == 's') fwd_vel -= KEYBOARD_FLY_RATE; if (k == ' ') reset_sensors(); @@ -960,7 +959,9 @@ int main(int argc, char** argv) glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(key); + glutKeyboardUpFunc(keyUp); glutSpecialFunc(specialkey); + glutSpecialUpFunc(specialkeyUp); glutMotionFunc(motionFunc); glutPassiveMotionFunc(mouseoverFunc); glutMouseFunc(mouseFunc);