4 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version. The Blender
10 * Foundation also sells licenses for use in proprietary software under
11 * the Blender License. See http://www.blender.org/BL/ for information
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24 * All rights reserved.
26 * The Original Code is: all of this file.
28 * Contributor(s): none yet.
30 * ***** END GPL/BL DUAL LICENSE BLOCK *****
32 #include "SM_FhObject.h"
33 #include "MT_MinMax.h"
39 SM_FhObject::SM_FhObject(DT_ShapeHandle rayshape, MT_Vector3 ray, SM_Object *parent_object) :
40 SM_Object(rayshape, NULL, NULL, NULL),
42 m_ray_direction(ray.normalized()),
43 m_parent_object(parent_object)
47 SM_FhObject::~SM_FhObject()
51 DT_Bool SM_FhObject::ray_hit(void *client_data,
54 const DT_CollData *coll_data)
57 SM_FhObject *fh_object = dynamic_cast<SM_FhObject *>((SM_Object *)client_object2);
60 std::swap(client_object1, client_object2);
61 fh_object = dynamic_cast<SM_FhObject *>((SM_Object *)client_object2);
64 SM_Object *hit_object = (SM_Object *)client_object1;
65 const SM_MaterialProps *matProps = hit_object->getMaterialProps();
67 if ((matProps == 0) || (matProps->m_fh_distance < MT_EPSILON)) {
71 SM_Object *cl_object = fh_object->getParentObject();
75 if (hit_object == cl_object) {
76 // Shot myself in the foot...
80 const SM_ShapeProps *shapeProps = cl_object->getShapeProps();
82 // Exit if the client object is not dynamic.
83 if (shapeProps == 0) {
90 DT_Vector3 from, to, dnormal;
92 fh_object->getPosition().getValue(from);
93 fh_object->getSpot().getValue(to);
96 if (DT_ObjectRayCast(hit_object->getObjectHandle(),
103 lspot = fh_object->getPosition() + (fh_object->getSpot() - fh_object->getPosition()) * dlspot;
104 const MT_Vector3& ray_dir = fh_object->getRayDirection();
105 MT_Scalar dist = MT_distance(fh_object->getPosition(), lspot) -
106 cl_object->getMargin() - shapeProps->m_radius;
108 normal = MT_Vector3(dnormal).safe_normalized();
110 if (dist < matProps->m_fh_distance) {
112 if (shapeProps->m_do_fh) {
113 lspot -= hit_object->getPosition();
114 MT_Vector3 rel_vel = cl_object->getLinearVelocity() - hit_object->getVelocity(lspot);
115 MT_Scalar rel_vel_ray = ray_dir.dot(rel_vel);
116 MT_Scalar spring_extent = 1.0 - dist / matProps->m_fh_distance;
118 MT_Scalar i_spring = spring_extent * matProps->m_fh_spring;
119 MT_Scalar i_damp = rel_vel_ray * matProps->m_fh_damping;
121 cl_object->addLinearVelocity(-(i_spring + i_damp) * ray_dir);
122 if (matProps->m_fh_normal) {
123 cl_object->addLinearVelocity(
124 (i_spring + i_damp) *
125 (normal - normal.dot(ray_dir) * ray_dir));
128 MT_Vector3 lateral = rel_vel - rel_vel_ray * ray_dir;
129 const SM_ShapeProps *shapeProps = cl_object->getShapeProps();
131 if (shapeProps->m_do_anisotropic) {
132 MT_Matrix3x3 lcs(cl_object->getOrientation());
133 MT_Vector3 loc_lateral = lateral * lcs;
134 const MT_Vector3& friction_scaling =
135 shapeProps->m_friction_scaling;
137 loc_lateral.scale(friction_scaling[0],
139 friction_scaling[2]);
140 lateral = lcs * loc_lateral;
144 MT_Scalar rel_vel_lateral = lateral.length();
146 if (rel_vel_lateral > MT_EPSILON) {
147 MT_Scalar friction_factor = matProps->m_friction;
148 MT_Scalar max_friction = friction_factor * MT_max(MT_Scalar(0.0), i_spring);
150 MT_Scalar rel_mom_lateral = rel_vel_lateral /
151 cl_object->getInvMass();
153 MT_Vector3 friction =
154 (rel_mom_lateral > max_friction) ?
155 -lateral * (max_friction / rel_vel_lateral) :
158 cl_object->applyCenterImpulse(friction);
162 if (shapeProps->m_do_rot_fh) {
163 const double *ogl_mat = cl_object->getMatrix();
164 MT_Vector3 up(&ogl_mat[8]);
165 MT_Vector3 t_spring = up.cross(normal) * matProps->m_fh_spring;
166 MT_Vector3 ang_vel = cl_object->getAngularVelocity();
168 // only rotations that tilt relative to the normal are damped
169 ang_vel -= ang_vel.dot(normal) * normal;
171 MT_Vector3 t_damp = ang_vel * matProps->m_fh_damping;
173 cl_object->addAngularVelocity(t_spring - t_damp);