added BendyLine

This commit is contained in:
Jeffrey Ventrella 2013-08-05 11:35:02 -07:00
parent 7693165578
commit e2ba8e22d5
2 changed files with 171 additions and 0 deletions

119
interface/src/BendyLine.cpp Normal file
View file

@ -0,0 +1,119 @@
//
// BendyLine.cpp
// interface
//
// Copyright (c) 2013 High Fidelity, Inc. All rights reserved.
#include "BendyLine.h"
#include "Util.h"
#include "world.h"
const float DEFAULT_BENDY_LINE_SPRING_FORCE = 10.0f;
const float DEFAULT_BENDY_LINE_TORQUE_FORCE = 0.1f;
const float DEFAULT_BENDY_LINE_DRAG = 10.0f;
const float DEFAULT_BENDY_LINE_LENGTH = 0.09f;
const float DEFAULT_BENDY_LINE_THICKNESS = 0.03f;
BendyLine::BendyLine(){
_springForce = DEFAULT_BENDY_LINE_SPRING_FORCE;
_torqueForce = DEFAULT_BENDY_LINE_TORQUE_FORCE;
_drag = DEFAULT_BENDY_LINE_DRAG;
_length = DEFAULT_BENDY_LINE_LENGTH;
_thickness = DEFAULT_BENDY_LINE_THICKNESS;
_gravityForce = glm::vec3(0.0f, 0.0f, 0.0f);
_basePosition = glm::vec3(0.0f, 0.0f, 0.0f);
_baseDirection = glm::vec3(0.0f, 0.0f, 0.0f);
_midPosition = glm::vec3(0.0f, 0.0f, 0.0f);
_endPosition = glm::vec3(0.0f, 0.0f, 0.0f);
_midVelocity = glm::vec3(0.0f, 0.0f, 0.0f);
_endVelocity = glm::vec3(0.0f, 0.0f, 0.0f);
}
void BendyLine::reset() {
_midPosition = _basePosition + _baseDirection * _length * ONE_HALF;
_endPosition = _midPosition + _baseDirection * _length * ONE_HALF;
_midVelocity = glm::vec3(0.0f, 0.0f, 0.0f);
_endVelocity = glm::vec3(0.0f, 0.0f, 0.0f);
}
void BendyLine::update(float deltaTime) {
glm::vec3 midAxis = _midPosition - _basePosition;
glm::vec3 endAxis = _endPosition - _midPosition;
float midLength = glm::length(midAxis);
float endLength = glm::length(endAxis);
glm::vec3 midDirection;
glm::vec3 endDirection;
if (midLength > 0.0f) {
midDirection = midAxis / midLength;
} else {
midDirection = _baseDirection;
}
if (endLength > 0.0f) {
endDirection = endAxis / endLength;
} else {
endDirection = _baseDirection;
}
// add spring force
float midForce = midLength - _length * ONE_HALF;
float endForce = endLength - _length * ONE_HALF;
_midVelocity -= midDirection * midForce * _springForce * deltaTime;
_endVelocity -= endDirection * endForce * _springForce * deltaTime;
// add gravity force
_midVelocity += _gravityForce;
_endVelocity += _gravityForce;
// add torque force
_midVelocity += _baseDirection * _torqueForce * deltaTime;
_endVelocity += midDirection * _torqueForce * deltaTime;
// add drag force
float momentum = 1.0f - (_drag * deltaTime);
if (momentum < 0.0f) {
_midVelocity = glm::vec3(0.0f, 0.0f, 0.0f);
_endVelocity = glm::vec3(0.0f, 0.0f, 0.0f);
} else {
_midVelocity *= momentum;
_endVelocity *= momentum;
}
// update position by velocity
_midPosition += _midVelocity;
_endPosition += _endVelocity;
// clamp lengths
glm::vec3 newMidVector = _midPosition - _basePosition;
glm::vec3 newEndVector = _endPosition - _midPosition;
float newMidLength = glm::length(newMidVector);
float newEndLength = glm::length(newEndVector);
glm::vec3 newMidDirection;
glm::vec3 newEndDirection;
if (newMidLength > 0.0f) {
newMidDirection = newMidVector/newMidLength;
} else {
newMidDirection = _baseDirection;
}
if (newEndLength > 0.0f) {
newEndDirection = newEndVector/newEndLength;
} else {
newEndDirection = _baseDirection;
}
_endPosition = _midPosition + newEndDirection * _length * ONE_HALF;
_midPosition = _basePosition + newMidDirection * _length * ONE_HALF;
}

52
interface/src/BendyLine.h Normal file
View file

@ -0,0 +1,52 @@
//
// BendyLine.h
// interface
//
// Copyright (c) 2013 High Fidelity, Inc. All rights reserved.
//
#ifndef hifi_bendyLine_h
#define hifi_bendyLine_h
#include <SharedUtil.h>
#include <glm/glm.hpp>
#include <glm/gtc/quaternion.hpp>
class BendyLine {
public:
BendyLine();
void update(float deltaTime);
void reset();
void setLength (float length ) { _length = length; }
void setThickness (float thickness ) { _thickness = thickness; }
void setSpringForce (float springForce ) { _springForce = springForce; }
void setTorqueForce (float torqueForce ) { _torqueForce = torqueForce; }
void setDrag (float drag ) { _drag = drag; }
void setBasePosition (glm::vec3 basePosition ) { _basePosition = basePosition; }
void setBaseDirection(glm::vec3 baseDirection) { _baseDirection = baseDirection;}
void setGravityForce (glm::vec3 gravityForce ) { _gravityForce = gravityForce; }
glm::vec3 getBasePosition() { return _basePosition; }
glm::vec3 getMidPosition () { return _midPosition; }
glm::vec3 getEndPosition () { return _endPosition; }
float getThickness () { return _thickness; }
private:
float _springForce;
float _torqueForce;
float _drag;
float _length;
float _thickness;
glm::vec3 _gravityForce;
glm::vec3 _basePosition;
glm::vec3 _baseDirection;
glm::vec3 _midPosition;
glm::vec3 _endPosition;
glm::vec3 _midVelocity;
glm::vec3 _endVelocity;
};
#endif