bool GameState::detectCollision(const Rectangle& r, bool modify_angle) { const Point2d& p = m_ball.position; if (is_between(p.x, r.upper_left.x, r.lower_right.x) && is_between(p.y, r.upper_left.y, r.lower_right.y)) { // normalized ball coordinates within the rectangle // [0, 1] double nx = (p.x - r.upper_left.x) / (r.lower_right.x - r.upper_left.x); double ny = (p.y - r.upper_left.y) / (r.lower_right.y - r.upper_left.y); if (modify_angle) { double r = getVelocityMagnitude(); double a = (0.75 - nx / 2) * PI; m_ball.velocity = Vector2d(r * cos(a), -r * sin(a)); } else { if (nx < ny == (1 - nx) < ny) { m_ball.velocity.y = -m_ball.velocity.y; } else { m_ball.velocity.x = -m_ball.velocity.x; } } return true; } return false; }
void GameState::resetBall() { m_ball.position = Point2d(.5, .5); double r = getVelocityMagnitude(); double a = (0.75 - randd() / 2) * PI; m_ball.velocity = Vector2d(r * cos(a), r * -sin(a)); }
void OpenNIUser::updateFeatures() { if (user->getJoint(JOINT_TORSO).getWorldPosition() == ofPoint(0,0,0)) return; if (elements_.empty()) { for (int j = 0; j < user->getNumJoints(); j++) { MocapElement newElement(j, depth_); elements_.push_back(newElement); } } ofPoint headPos = user->getJoint(JOINT_HEAD).getWorldPosition(); ofPoint torsoPos = user->getJoint(JOINT_TORSO).getWorldPosition(); //for CI float xMax = numeric_limits<float>::min(); float yMax = numeric_limits<float>::min(); float zMax = numeric_limits<float>::min(); float xMin = numeric_limits<float>::max(); float yMin = numeric_limits<float>::max(); float zMin = numeric_limits<float>::max(); float headTorsoDist = headPos.distance(torsoPos); float meanVel = 0.0; //for qom for (int j = 0; j < user->getNumJoints(); j++) { computeJointDescriptors(j, user->getJoint((Joint) j).getWorldPosition(), headTorsoDist); //qom meanVel += getVelocityMagnitude(j); //ci xMin = min(xMin, getPositionFiltered(j).x); yMin = min(yMin, getPositionFiltered(j).y); zMin = min(zMin, getPositionFiltered(j).z); xMax = max(xMax, getPositionFiltered(j).x); yMax = max(yMax, getPositionFiltered(j).y); zMax = max(zMax, getPositionFiltered(j).z); } // Add position to history if (meanVels_.size() <= depth_) { meanVels_.insert(meanVels_.begin(), meanVel / user->getNumJoints()); } if (meanVels_.size() > depth_) { meanVels_.pop_back(); } qom_ = accumulate(meanVels_.begin(), meanVels_.end(), 0.0) / (meanVels_.size()); ci_ = ( -4.0 + (( abs(xMax-xMin) + abs(yMax-yMin) + abs(zMax-zMin) ) / headTorsoDist) ) / 6.0; symmetry_ = 1.0 - (0.5 * (abs(sqrt(getDistanceToTorso(JOINT_RIGHT_HAND))-sqrt(getDistanceToTorso(JOINT_LEFT_HAND))) + abs(sqrt(getDistanceToTorso(JOINT_RIGHT_ELBOW))-sqrt(getDistanceToTorso(JOINT_LEFT_ELBOW)))) / headTorsoDist); yMaxHands_ = max(getRelativePositionToTorso(JOINT_RIGHT_HAND).y, getRelativePositionToTorso(JOINT_LEFT_HAND).y); }
void ofxKinectFeatures::update(map<int, ofPoint> joints){ //Initialize elements if (elements_.empty()) { for (map<int, ofPoint>::iterator it = joints.begin(); it != joints.end(); it++) { ofxMocapElement newElement(it->first, depth_); elements_.push_back(newElement); } } //Compute descriptors //Hard-coded way to check if skeleton (bSkeleton doesn't work) and new data (isNewDataAvailable doesn't work) // if (user.getJoint((Joint)0).getWorldPosition() != ofPoint(0,0,0) && // user.getJoint((Joint)0).getWorldPosition() != getElement((Joint)0)->getPosition()[0] ) { newValues_ = true; //TODO solve this!! ofPoint headPos = joints[head_]; ofPoint torsoPos = joints[torso_]; float h = headPos.distance(torsoPos); float meanVel = 0.0; //for qom //for CI float xMax = numeric_limits<float>::min(); float yMax = numeric_limits<float>::min(); float zMax = numeric_limits<float>::min(); float xMin = numeric_limits<float>::max(); float yMin = numeric_limits<float>::max(); float zMin = numeric_limits<float>::max(); for (map<int, ofPoint>::iterator it = joints.begin(); it != joints.end(); it++) { int j = it->first; computeJointDescriptors(it->first, it->second, h); //qom meanVel += getVelocityMagnitude(j); //ci if (getPositionFiltered(j).x > xMax) { xMax = getPositionFiltered(j).x; } if (getPositionFiltered(j).y > yMax) { yMax = getPositionFiltered(j).y; } if (getPositionFiltered(j).z > zMax) { zMax = getPositionFiltered(j).z; } if (getPositionFiltered(j).x < xMin) { xMin = getPositionFiltered(j).x; } if (getPositionFiltered(j).y < yMin) { yMin = getPositionFiltered(j).y; } if (getPositionFiltered(j).z < zMin) { zMin = getPositionFiltered(j).z; } } // Add position to history if (meanVels_.size() <= depth_) { meanVels_.insert(meanVels_.begin(), meanVel/joints.size()); } // remove positions from history if (meanVels_.size() > depth_) { meanVels_.pop_back(); } //qom_ = accumulate(meanVels_.begin(), meanVels_.end(), 0.0) / (meanVels_.size()); // QOM insert if (qom_.size() <= depth_) { qom_.insert(qom_.begin(), accumulate(meanVels_.begin(), meanVels_.end(), 0.0) / (meanVels_.size())); } // QOM delete if (qom_.size() > depth_) { qom_.pop_back(); } checkMaxAndMin(getQomHistory(5), NO_JOINT, FEAT_QOM); //ci_ = ( -4.0 + (( abs(xMax-xMin) + abs(yMax-yMin) + abs(zMax-zMin) ) / h) ) / 6.0; if (ci_.size() <= depth_) { ci_.insert(ci_.begin(), ( -4.0 + (( abs(xMax-xMin) + abs(yMax-yMin) + abs(zMax-zMin) ) / h) ) / 6.0); } // QOM delete if (ci_.size() > depth_) { ci_.pop_back(); } checkMaxAndMin(getCIHistory(5), NO_JOINT, FEAT_CI); //TODO solve this!! // symmetry_ = 1.0 - (0.5 * (abs(sqrt(getDistanceToTorso(JOINT_RIGHT_HAND))-sqrt(getDistanceToTorso(JOINT_LEFT_HAND))) + abs(sqrt(getDistanceToTorso(JOINT_RIGHT_ELBOW))-sqrt(getDistanceToTorso(JOINT_LEFT_ELBOW)))) / h); //symmetry_ = 0.0; //TODO solve this!! //yMaxHands_ = max(getRelativePositionToTorso(JOINT_RIGHT_HAND).y, getRelativePositionToTorso(JOINT_LEFT_HAND).y); //yMaxHands_ = 0.0; // } else { // newValues_ // = false; // } }