00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00027
00029 #ifndef __HAPTICSPRING_H__
00030 #define __HAPTICSPRING_H__
00031
00032 #include "HapticForceEffect.h"
00033 #include "Threads.h"
00034
00035 namespace H3D {
00036
00040 class H3DAPI_API HapticSpring: public HapticForceEffect {
00041 public:
00043 HapticSpring ( const H3D::ArithmeticTypes::Matrix4f & _transform,
00044 bool _interpolate ):
00045 HapticForceEffect( _transform, _interpolate ),
00046 position( Vec3f( 0, 0, 0 ) ),
00047 spring_constant( 0 ) { }
00048
00050 HapticSpring( const H3D::ArithmeticTypes::Matrix4f & _transform,
00051 const Vec3f &_position,
00052 H3DFloat _spring_constant,
00053 bool _interpolate ):
00054 HapticForceEffect( _transform, _interpolate ),
00055 position( _position ),
00056 spring_constant( _spring_constant ) {}
00057
00059 EffectOutput virtual calculateForces( const EffectInput &input ) {
00060 lock.lock();
00061 Vec3f local_pos = transform.inverse() * input.position;
00062 Vec3f local_force = ( position - local_pos ) * spring_constant;
00063 force = local_force;
00064 lock.unlock();
00065 return EffectOutput( transform.getRotationPart() * local_force );
00066 }
00067
00068
00069 virtual void setPosition( const Vec3f &_position ) {
00070 lock.lock();
00071 position = _position;
00072 lock.unlock();
00073 }
00074
00075
00076 virtual void setVelocity( const Vec3f &_velocity ) {
00077 lock.lock();
00078 velocity = _velocity;
00079 lock.unlock();
00080 }
00081
00082
00083 virtual void setSpringConstant( const H3DFloat &_sc ) {
00084 lock.lock();
00085 spring_constant = _sc;
00086 lock.unlock();
00087 }
00088
00089
00090 virtual Vec3f getLatestForce() {
00091 lock.lock();
00092 Vec3f f = force;
00093 lock.unlock();
00094 return f;
00095 }
00096
00097 protected:
00098 Vec3f position;
00099 Vec3f velocity;
00100 Vec3f force;
00101 H3DFloat spring_constant;
00102 MutexLock lock;
00103 };
00104 }
00105
00106 #endif