00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
#include "Rope.h"
00013
#include <OpenCAL/PointMass.h>
00014
#include <OpenCAL/Spring.h>
00015
#include <OpenCAL/System.h>
00016
#include <OpenCAL/Renderer.h>
00017
using namespace OpenCAL;
00018
00019
#include <OpenCAL/Vector3.h>
00020
#include <OpenCAL/Material.h>
00021
using namespace Utils;
00022
00023
using namespace std;
00024
00025
00026
00027
00028
00029
00030 Rope::Rope(
System *parent,
const Vector3 &from,
const Vector3 &to)
00031 :
MassSpringSystem(parent)
00032 {
00033 constructRope(from, to);
00034 }
00035
00036 Rope::~Rope()
00037 {
00038 }
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 void Rope::render()
00051 {
00052
if(m_parent->
numObjects() == 0)
return;
00053
00054
Renderer *renderer = m_parent->
getRenderer();
00055
if(!renderer)
return;
00056
00057
for(
unsigned int i = 1; i < m_parent->
numObjects(); ++i)
00058 renderer->
renderLine(getPoint(i - 1)->getPosition(), getPoint(i)->getPosition());
00059 }
00060
00061
00062
00063
00064
00065
00066
00067
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
void Rope::constructRope(
const Vector3 &from,
const Vector3 &to)
00092 {
00093
00094
const int precision = 20;
00095
const float stiffness = 100.0f;
00096
const float damping = 0.2f;
00097
const float mass = 1.0f;
00098
00099
float distance = from.
distance(to);
00100
int particles = (
int) (distance * precision);
00101
float massEach = distance * mass / (
float) particles;
00102
00103
PointMass *prevMass =
new PointMass(
this, massEach);
00104
00105 prevMass->
setPosition(from);
00106
00107
00108
for(
int i = 1; i < particles; ++i)
00109 {
00110
PointMass *mass =
new PointMass(
this, massEach);
00111
00112 mass->
setPosition(from.
blended((
float) (i + 1) / (
float) particles, to));
00113
00114
00115
00116
Spring *spring =
new Spring(
this, prevMass, mass);
00117 spring->
setStiffness(stiffness);
00118 spring->
setDamping(damping);
00119 spring->
setDraw(
true);
00120
00121 prevMass = mass;
00122 }
00123 }