Removed hand class files, relocated transmitter receiver function to Head.h

This commit is contained in:
Philip Rosedale 2013-04-12 21:01:01 -07:00
parent 5368cbf79a
commit 1d69acef56
5 changed files with 72 additions and 292 deletions

View file

@ -1,225 +0,0 @@
//
// Hand.cpp
// interface
//
// Created by Philip Rosedale on 10/13/12.
// Copyright (c) 2012 High Fidelity, Inc. All rights reserved.
//
#include "Hand.h"
#ifdef _WIN32
#include "Systime.h"
#else
#include <sys/time.h>
#endif
const float PHI = 1.618f;
const float DEFAULT_X = 0;
const float DEFAULT_Y = -1.5;
const float DEFAULT_Z = 2.0;
const float DEFAULT_TRANSMITTER_HZ = 60.0;
Hand::Hand(glm::vec3 initcolor) {
color = initcolor;
reset();
noise = 0.0; //0.2;
scale.x = 0.07f;
scale.y = scale.x * 5.0f;
scale.z = scale.y * 1.0f;
renderPointer = true;
}
Hand::Hand(const Hand &otherHand) {
color = otherHand.color;
noise = otherHand.noise;
scale = otherHand.scale;
position = otherHand.position;
target = otherHand.target;
velocity = otherHand.color;
pitch = otherHand.pitch;
yaw = otherHand.yaw;
roll = otherHand.roll;
pitchRate = otherHand.pitchRate;
yawRate = otherHand.yawRate;
rollRate = otherHand.rollRate;
transmitterTimer = otherHand.transmitterTimer;
transmitterHz = otherHand.transmitterHz;
transmitterPackets = otherHand.transmitterPackets;
renderPointer = otherHand.renderPointer;
}
void Hand::reset() {
position.x = DEFAULT_X;
position.y = DEFAULT_Y;
position.z = DEFAULT_Z;
pitch = yaw = roll = 0;
pitchRate = yawRate = rollRate = 0;
setTarget(position);
velocity.x = velocity.y = velocity.z = 0;
transmitterPackets = 0;
transmitterHz = DEFAULT_TRANSMITTER_HZ;
}
void Hand::render(int isMine) {
const float POINTER_LENGTH = 20.0;
glPushMatrix();
glTranslatef(position.x, position.y, position.z);
glRotatef(yaw, 0, 1, 0);
glRotatef(pitch, 1, 0, 0);
glRotatef(roll, 0, 0, 1);
glColor3f(color.x, color.y, color.z);
glScalef(scale.x, scale.y, scale.z);
//glutSolidSphere(1.5, 20, 20);
glutSolidCube(1.0);
if (renderPointer) {
glBegin(GL_TRIANGLES);
glColor3f(1,0,0);
glNormal3f(0,-1,0);
glVertex3f(-0.4f,0,0);
glVertex3f(0.4f,0,0);
glVertex3f(0,0,-POINTER_LENGTH);
glEnd();
glPushMatrix();
glTranslatef(0,0,-POINTER_LENGTH);
glutSolidCube(1.0);
glPopMatrix();
}
glPopMatrix();
}
void Hand::addAngularVelocity (float pRate, float yRate, float rRate) {
pitchRate += pRate;
yawRate += yRate;
rollRate += rRate;
}
void Hand::processTransmitterData(char *packetData, int numBytes) {
// Read a packet from a transmitter app, process the data
float accX, accY, accZ,
graX, graY, graZ,
gyrX, gyrY, gyrZ,
linX, linY, linZ,
rot1, rot2, rot3, rot4;
sscanf((char *)packetData, "tacc %f %f %f gra %f %f %f gyr %f %f %f lin %f %f %f rot %f %f %f %f",
&accX, &accY, &accZ,
&graX, &graY, &graZ,
&gyrX, &gyrY, &gyrZ,
&linX, &linY, &linZ,
&rot1, &rot2, &rot3, &rot4);
if (transmitterPackets++ == 0) {
gettimeofday(&transmitterTimer, NULL);
}
const int TRANSMITTER_COUNT = 100;
if (transmitterPackets % TRANSMITTER_COUNT == 0) {
// Every 100 packets, record the observed Hz of the transmitter data
timeval now;
gettimeofday(&now, NULL);
double msecsElapsed = diffclock(&transmitterTimer, &now);
transmitterHz = static_cast<float>( (double)TRANSMITTER_COUNT/(msecsElapsed/1000.0) );
//std::cout << "Transmitter Hz: " << (float)TRANSMITTER_COUNT/(msecsElapsed/1000.0) << "\n";
//memcpy(&transmitterTimer, &now, sizeof(timeval));
transmitterTimer = now;
}
// Add rotational forces to the hand
const float ANG_VEL_SENSITIVITY = 4.0;
const float ANG_VEL_THRESHOLD = 0.0;
float angVelScale = ANG_VEL_SENSITIVITY*(1.0f/getTransmitterHz());
//addAngularVelocity(gyrX*angVelScale,gyrZ*angVelScale,-gyrY*angVelScale);
addAngularVelocity(fabs(gyrX*angVelScale)>ANG_VEL_THRESHOLD?gyrX*angVelScale:0,
fabs(gyrZ*angVelScale)>ANG_VEL_THRESHOLD?gyrZ*angVelScale:0,
fabs(-gyrY*angVelScale)>ANG_VEL_THRESHOLD?-gyrY*angVelScale:0);
// Add linear forces to the hand
//const float LINEAR_VEL_SENSITIVITY = 50.0;
const float LINEAR_VEL_SENSITIVITY = 5.0;
float linVelScale = LINEAR_VEL_SENSITIVITY*(1.0f/getTransmitterHz());
glm::vec3 linVel(linX*linVelScale, linZ*linVelScale, -linY*linVelScale);
addVelocity(linVel);
}
void Hand::simulate(float deltaTime) {
const float ANGULAR_SPRING_CONSTANT = 0.25;
const float ANGULAR_DAMPING_COEFFICIENT = 5*2.0f*powf(ANGULAR_SPRING_CONSTANT,0.5f);
const float LINEAR_SPRING_CONSTANT = 100;
const float LINEAR_DAMPING_COEFFICIENT = 2.0f*powf(LINEAR_SPRING_CONSTANT,0.5f);
// If noise, add a bit of random velocity
const float RNOISE = 0.0;
const float VNOISE = 0.01f;
if (noise) {
glm::vec3 nVel(randFloat() - 0.5f, randFloat() - 0.5f, randFloat() - 0.5f);
nVel *= VNOISE;
addVelocity(nVel);
addAngularVelocity(RNOISE*(randFloat() - 0.5f),
RNOISE*(randFloat() - 0.5f),
RNOISE*(randFloat() - 0.5f));
}
position += velocity*deltaTime;
pitch += pitchRate;
yaw += yawRate;
roll += rollRate;
// The spring method
if (0) {
// Use a linear spring to attempt to return the hand to the target position
glm::vec3 springForce = target - position;
springForce *= LINEAR_SPRING_CONSTANT;
addVelocity(springForce * deltaTime);
// Critically damp the linear spring
glm::vec3 dampingForce(velocity);
dampingForce *= LINEAR_DAMPING_COEFFICIENT;
addVelocity(-dampingForce * deltaTime);
// Use angular spring to return hand to target rotation (0,0,0)
addAngularVelocity(-pitch * ANGULAR_SPRING_CONSTANT * deltaTime,
-yaw * ANGULAR_SPRING_CONSTANT * deltaTime,
-roll * ANGULAR_SPRING_CONSTANT * deltaTime);
// Critically damp angular spring
addAngularVelocity(-pitchRate*ANGULAR_DAMPING_COEFFICIENT*deltaTime,
-yawRate*ANGULAR_DAMPING_COEFFICIENT*deltaTime,
-rollRate*ANGULAR_DAMPING_COEFFICIENT*deltaTime);
}
// The absolute limits method (no springs)
if (1) {
// Limit rotation
const float YAW_LIMIT = 20;
const float PITCH_LIMIT = 20;
if (yaw > YAW_LIMIT) { yaw = YAW_LIMIT; yawRate = 0.0; }
if (yaw < -YAW_LIMIT) { yaw = -YAW_LIMIT; yawRate = 0.0; }
if (pitch > PITCH_LIMIT) { pitch = PITCH_LIMIT; pitchRate = 0.0; }
if (pitch < -PITCH_LIMIT) { pitch = -PITCH_LIMIT; pitchRate = 0.0; }
// Damp Rotation Rates
yawRate *= 0.99f;
pitchRate *= 0.99f;
rollRate *= 0.99f;
// Limit position
const float X_LIMIT = 1.0;
const float Y_LIMIT = 1.0;
const float Z_LIMIT = 1.0;
if (position.x > DEFAULT_X + X_LIMIT) { position.x = DEFAULT_X + X_LIMIT; velocity.x = 0; }
if (position.x < DEFAULT_X - X_LIMIT) { position.x = DEFAULT_X - X_LIMIT; velocity.x = 0; }
if (position.y > DEFAULT_Y + Y_LIMIT) { position.y = DEFAULT_Y + Y_LIMIT; velocity.y = 0; }
if (position.y < DEFAULT_Y - Y_LIMIT) { position.y = DEFAULT_Y - Y_LIMIT; velocity.y = 0; }
if (position.z > DEFAULT_Z + Z_LIMIT) { position.z = DEFAULT_Z + Z_LIMIT; velocity.z = 0; }
if (position.z < DEFAULT_Z - Z_LIMIT) { position.z = DEFAULT_Z - Z_LIMIT; velocity.z = 0; }
// Damp Velocity
velocity *= 0.99;
}
}

View file

@ -1,47 +0,0 @@
//
// Hand.h
// interface
//
// Created by Philip Rosedale on 10/13/12.
// Copyright (c) 2012 High Fidelity, Inc. All rights reserved.
//
#ifndef __interface__Hand__
#define __interface__Hand__
#include <glm/glm.hpp>
#include <iostream>
#include "Util.h"
#include "Field.h"
#include "world.h"
#include "InterfaceConfig.h"
class Hand {
public:
Hand(glm::vec3 color);
Hand(const Hand &otherHand);
void simulate (float deltaTime);
void render (int isMine);
void reset ();
void setNoise (float mag) { noise = mag; };
void addVelocity (glm::vec3 v) { velocity += v; };
void addAngularVelocity (float pRate, float yRate, float rRate);
glm::vec3 getPos() { return position; };
void setPos(glm::vec3 p) { position = p; };
void setTarget(glm::vec3 t) { target = t; };
void processTransmitterData(char * packetData, int numBytes);
float getTransmitterHz() { return transmitterHz; };
void setRenderPointer(bool p) { renderPointer = p; };
private:
glm::vec3 position, target, velocity, color, scale;
float pitch, yaw, roll, pitchRate, yawRate, rollRate;
float noise;
timeval transmitterTimer;
float transmitterHz;
int transmitterPackets;
bool renderPointer;
};
#endif

View file

@ -97,8 +97,6 @@ Head::Head() {
springForce = 6.0f;
springVelocityDecay = 16.0f;
hand = new Hand(glm::vec3(skinColor[0], skinColor[1], skinColor[2]));
if (iris_texture.size() == 0) {
switchToResourcesIfRequired();
unsigned error = lodepng::decode(iris_texture, iris_texture_width, iris_texture_height, iris_texture_file);
@ -174,12 +172,8 @@ Head::Head(const Head &otherHead) {
sphere = NULL;
Hand newHand = Hand(*otherHead.hand);
hand = &newHand;
}
Head::~Head() {
if (sphere != NULL) {
gluDeleteQuadric(sphere);
@ -1224,10 +1218,55 @@ void Head::parseData(void *data, int size) {
handBeingMoved = true;
}
void Head::SetNewHeadTarget(float pitch, float yaw) {
PitchTarget = pitch;
YawTarget = yaw;
}
void Head::processTransmitterData(char *packetData, int numBytes) {
// Read a packet from a transmitter app, process the data
float accX, accY, accZ,
graX, graY, graZ,
gyrX, gyrY, gyrZ,
linX, linY, linZ,
rot1, rot2, rot3, rot4;
sscanf((char *)packetData, "tacc %f %f %f gra %f %f %f gyr %f %f %f lin %f %f %f rot %f %f %f %f",
&accX, &accY, &accZ,
&graX, &graY, &graZ,
&gyrX, &gyrY, &gyrZ,
&linX, &linY, &linZ,
&rot1, &rot2, &rot3, &rot4);
if (transmitterPackets++ == 0) {
gettimeofday(&transmitterTimer, NULL);
}
const int TRANSMITTER_COUNT = 100;
if (transmitterPackets % TRANSMITTER_COUNT == 0) {
// Every 100 packets, record the observed Hz of the transmitter data
timeval now;
gettimeofday(&now, NULL);
double msecsElapsed = diffclock(&transmitterTimer, &now);
transmitterHz = static_cast<float>( (double)TRANSMITTER_COUNT/(msecsElapsed/1000.0) );
transmitterTimer = now;
}
/* NOTE: PR: Will add back in when ready to animate avatar hand
// Add rotational forces to the hand
const float ANG_VEL_SENSITIVITY = 4.0;
const float ANG_VEL_THRESHOLD = 0.0;
float angVelScale = ANG_VEL_SENSITIVITY*(1.0f/getTransmitterHz());
addAngularVelocity(fabs(gyrX*angVelScale)>ANG_VEL_THRESHOLD?gyrX*angVelScale:0,
fabs(gyrZ*angVelScale)>ANG_VEL_THRESHOLD?gyrZ*angVelScale:0,
fabs(-gyrY*angVelScale)>ANG_VEL_THRESHOLD?-gyrY*angVelScale:0);
// Add linear forces to the hand
//const float LINEAR_VEL_SENSITIVITY = 50.0;
const float LINEAR_VEL_SENSITIVITY = 5.0;
float linVelScale = LINEAR_VEL_SENSITIVITY*(1.0f/getTransmitterHz());
glm::vec3 linVel(linX*linVelScale, linZ*linVelScale, -linY*linVelScale);
addVelocity(linVel);
*/
}

View file

@ -13,7 +13,6 @@
#include "AgentData.h"
#include "Field.h"
#include "world.h"
#include "Hand.h"
#include "Orientation.h" // added by Ventrella as a utility
#include "InterfaceConfig.h"
#include "SerialInterface.h"
@ -196,7 +195,12 @@ class Head : public AgentData {
void addThrust(glm::vec3 newThrust) { avatar.thrust += newThrust; };
glm::vec3 getThrust() { return avatar.thrust; };
Hand * hand;
//
// Related to getting transmitter UDP data used to animate the avatar hand
//
void processTransmitterData(char * packetData, int numBytes);
float getTransmitterHz() { return transmitterHz; };
private:
float noise;
@ -273,6 +277,13 @@ class Head : public AgentData {
void readSensors();
float renderYaw, renderPitch; // Pitch from view frustum when this is own head.
//
// Related to getting transmitter UDP data used to animate the avatar hand
//
timeval transmitterTimer;
float transmitterHz;
int transmitterPackets;
};
#endif

View file

@ -60,9 +60,8 @@
#include "MenuRow.h"
#include "MenuColumn.h"
#include "Menu.h"
#include "Head.h"
#include "Hand.h"
#include "Camera.h"
#include "Head.h"
#include "Particle.h"
#include "Texture.h"
#include "Cloud.h"
@ -372,12 +371,13 @@ void simulateHand(float deltaTime) {
// If mouse is being dragged, send current force to the hand controller
if (mousePressed == 1)
{
// Add a velocity to the hand corresponding to the detected size of the drag vector
// NOTE--PER: Need to re-implement when ready for new avatar hand movements
const float MOUSE_HAND_FORCE = 1.5;
float dx = mouseX - mouseStartX;
float dy = mouseY - mouseStartY;
glm::vec3 vel(dx*MOUSE_HAND_FORCE, -dy*MOUSE_HAND_FORCE*(WIDTH/HEIGHT), 0);
myAvatar.hand->addVelocity(vel*deltaTime);
//myAvatar.hand->addVelocity(vel*deltaTime);
}
}
@ -799,7 +799,12 @@ void display(void)
// Test - Draw a blue sphere around a body part of mine!
glPushMatrix();
//glTranslate3f(myAvatar.GetRight
glColor4f(0,0,1, 0.7);
glTranslatef(myAvatar.getBonePosition(AVATAR_BONE_RIGHT_HAND).x,
myAvatar.getBonePosition(AVATAR_BONE_RIGHT_HAND).y,
myAvatar.getBonePosition(AVATAR_BONE_RIGHT_HAND).z);
glutSolidSphere(0.03, 10, 10);
glPopMatrix();
// draw a red sphere
@ -1208,7 +1213,7 @@ void *networkReceive(void *args)
switch (incomingPacket[0]) {
case PACKET_HEADER_TRANSMITTER_DATA:
myAvatar.hand->processTransmitterData(incomingPacket, bytesReceived);
myAvatar.processTransmitterData(incomingPacket, bytesReceived);
break;
case PACKET_HEADER_VOXEL_DATA:
case PACKET_HEADER_Z_COMMAND:
@ -1405,7 +1410,6 @@ int main(int argc, const char * argv[])
initDisplay();
printf( "Initialized Display.\n" );
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(key);
@ -1417,11 +1421,9 @@ int main(int argc, const char * argv[])
glutMouseFunc(mouseFunc);
glutIdleFunc(idle);
init();
printf( "Init() complete.\n" );
// Check to see if the user passed in a command line option for randomizing colors
if (cmdOptionExists(argc, argv, "--NoColorRandomizer")) {
wantColorRandomizer = false;