2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali/internal/event/dynamics/dynamics-joint-impl.h>
24 #include <dali/integration-api/debug.h>
25 #include <dali/internal/event/common/stage-impl.h>
26 #include <dali/internal/event/dynamics/dynamics-body-impl.h>
27 #include <dali/internal/event/dynamics/dynamics-world-impl.h>
28 #include <dali/internal/update/dynamics/scene-graph-dynamics-body.h>
29 #include <dali/internal/update/dynamics/scene-graph-dynamics-joint.h>
37 DynamicsJoint::DynamicsJoint(DynamicsWorldPtr world, DynamicsBodyPtr bodyA, DynamicsBodyPtr bodyB, const Vector3& offsetA, const Vector3& offsetB )
38 : mDynamicsJoint(NULL),
47 for( unsigned int i = 0; i < RotationAxis; ++i )
49 mTranslationLowerLimit[i] = 0.0f;
50 mTranslationUpperLimit[i] = 0.0f;
51 mRotationLowerLimit[i] = 0.0f;
52 mRotationUpperLimit[i] = 0.0f;
55 for( unsigned int i = 0; i < MaxAxis; ++i )
57 mSpringStiffness[i] = 0.0f;
58 mSpringDamping[i] = 0.5f;
59 mSpringCenterPoint[i] = 0.0f;
60 mMotorVelocity[i] = 0.0f;
61 mMotorForce[i] = 0.0f;
64 mDynamicsJoint = new SceneGraph::DynamicsJoint( *(world->GetSceneObject()) );
67 DynamicsJoint::~DynamicsJoint()
69 DALI_LOG_INFO(Debug::Filter::gDynamics, Debug::Verbose, "%s\n", __PRETTY_FUNCTION__);
71 if( Stage::IsInstalled() )
73 DeleteJointMessage( Stage::GetCurrent()->GetUpdateInterface(), *(GetSceneObject()) );
77 void DynamicsJoint::SetLinearLimit(const int axisIndex, const float lowerLimit, const float upperLimit)
79 bool valueChanged(false);
82 for( unsigned int i = 0; i < RotationAxis; ++i)
84 if( (bit & axisIndex) )
86 if( fabsf(lowerLimit - mTranslationLowerLimit[i]) >= GetRangedEpsilon(lowerLimit, mTranslationLowerLimit[i]) ||
87 fabsf(upperLimit - mTranslationUpperLimit[i]) >= GetRangedEpsilon(upperLimit, mTranslationUpperLimit[i]) )
89 mTranslationLowerLimit[i] = lowerLimit;
90 mTranslationUpperLimit[i] = upperLimit;
100 SetLimitMessage( Stage::GetCurrent()->GetUpdateInterface(), *(GetSceneObject()), axisIndex, lowerLimit, upperLimit );
104 void DynamicsJoint::SetAngularLimit(const int axisIndex, const Radian& lowerLimit, const Radian& upperLimit)
106 bool valueChanged(false);
108 int bit(1 << RotationAxis);
109 for( unsigned int i = 0; i < RotationAxis; ++i)
111 if( (bit & axisIndex) )
113 if( ( ! Equals( lowerLimit, mRotationLowerLimit[i] ) ) || ( ! Equals( upperLimit, mRotationUpperLimit[i] ) ) )
115 mRotationLowerLimit[i] = lowerLimit;
116 mRotationUpperLimit[i] = upperLimit;
126 SetLimitMessage( Stage::GetCurrent()->GetUpdateInterface(), *(GetSceneObject()), axisIndex, lowerLimit, upperLimit );
130 void DynamicsJoint::EnableSpring(const int axisIndex, const bool flag)
135 for( unsigned int i = 0; i < MaxAxis; ++i)
137 if( (bit & axisIndex) && (flag != (mSpringEnabled & bit)) )
141 mSpringEnabled |= bit;
145 mSpringEnabled &= ~bit;
153 if( 0 != valueChanged )
155 SetEnableSpringMessage( Stage::GetCurrent()->GetUpdateInterface(), *(GetSceneObject()), valueChanged, flag );
159 void DynamicsJoint::DynamicsJoint::SetSpringStiffness(const int axisIndex, const float stiffness)
164 for( unsigned int i = 0; i < MaxAxis; ++i)
166 if( (bit & axisIndex) && fabsf(stiffness - mSpringStiffness[i]) >= GetRangedEpsilon(stiffness, mSpringStiffness[i]) )
168 mSpringStiffness[i] = stiffness;
175 if( 0 != valueChanged )
177 SetSpringStiffnessMessage( Stage::GetCurrent()->GetUpdateInterface(), *(GetSceneObject()), valueChanged, stiffness );
181 void DynamicsJoint::SetSpringDamping(const int axisIndex, const float damping)
183 const float clampedDamping( Clamp(damping, 0.0f, 1.0f) );
188 for( unsigned int i = 0; i < MaxAxis; ++i)
190 if( (bit & axisIndex) && fabsf(clampedDamping - mSpringDamping[i]) >= GetRangedEpsilon(clampedDamping, mSpringDamping[i]) )
192 mSpringDamping[i] = clampedDamping;
199 if( 0 != valueChanged )
201 SetSpringDampingMessage( Stage::GetCurrent()->GetUpdateInterface(), *(GetSceneObject()), valueChanged, clampedDamping );
205 void DynamicsJoint::DynamicsJoint::SetSpringCenterPoint(const int axisIndex, const float ratio)
210 for( unsigned int i = 0; i < MaxAxis; ++i)
212 if( (bit & axisIndex) && fabsf(ratio - mSpringCenterPoint[i]) >= GetRangedEpsilon(ratio, mSpringCenterPoint[i]) )
214 mSpringCenterPoint[i] = ratio;
221 if( 0 != valueChanged )
223 SetSpringCenterPointMessage( Stage::GetCurrent()->GetUpdateInterface(), *(GetSceneObject()), valueChanged, ratio );
227 void DynamicsJoint::EnableMotor(const int axisIndex, const bool flag)
229 bool valueChanged(false);
232 for( unsigned int i = 0; i < MaxAxis; ++i)
234 if( (bit & axisIndex) && (flag != (mMotorEnabled & bit)) )
238 mMotorEnabled |= bit;
242 mMotorEnabled &= ~bit;
251 SetEnableMotorMessage( Stage::GetCurrent()->GetUpdateInterface(), *(GetSceneObject()), axisIndex, flag );
255 void DynamicsJoint::SetMotorVelocity(const int axisIndex, const float velocity)
257 bool valueChanged(false);
260 for( unsigned int i = 0; i < MaxAxis; ++i)
262 if( (bit & axisIndex) && fabsf(velocity - mMotorVelocity[i]) >= GetRangedEpsilon(velocity, mMotorVelocity[i]) )
264 mMotorVelocity[i] = velocity;
273 SetMotorVelocityMessage( Stage::GetCurrent()->GetUpdateInterface(), *(GetSceneObject()), axisIndex, velocity );
277 void DynamicsJoint::SetMotorForce(const int axisIndex, const float force)
279 bool valueChanged(false);
282 for( unsigned int i = 0; i < MaxAxis; ++i)
284 if( (bit & axisIndex) && fabsf(force - mMotorForce[i]) >= GetRangedEpsilon(force, mMotorForce[i]) )
286 mMotorForce[i] = force;
295 SetMotorForceMessage( Stage::GetCurrent()->GetUpdateInterface(), *(GetSceneObject()), axisIndex, force );
299 ActorPtr DynamicsJoint::GetActor( const bool first ) const
301 DynamicsBodyPtr body( first ? mBodyA : mBodyB );
303 return Stage::GetCurrent()->GetDynamicsWorld()->GetMappedActor( body->GetSceneObject() );
306 void DynamicsJoint::Connect(Stage& stage)
308 DALI_LOG_INFO(Debug::Filter::gDynamics, Debug::Verbose, "%s\n", __PRETTY_FUNCTION__);
312 InitializeDynamicsJointMessage( stage.GetUpdateInterface(), *mDynamicsJoint, *(mBodyA->GetSceneObject()), *(mBodyB->GetSceneObject()), mOffsetA, mOffsetB );
316 ConnectJointMessage( stage.GetUpdateInterface(), *(GetSceneObject()) );
319 void DynamicsJoint::Disconnect(Stage& stage)
321 DALI_LOG_INFO(Debug::Filter::gDynamics, Debug::Verbose, "%s\n", __PRETTY_FUNCTION__);
323 if( stage.IsInstalled() )
325 DisconnectJointMessage( stage.GetUpdateInterface(), *(GetSceneObject()) );
329 SceneGraph::DynamicsJoint* DynamicsJoint::GetSceneObject() const
331 return mDynamicsJoint;
334 } // namespace Internal