1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
// K-3D
// Copyright (c) 1995-2009, Timothy M. Shead
//
// Contact: tshead@k-3d.com
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, read to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
/** \file
\author Timothy M. Shead
*/
#include "isimulation.h"
#include <k3d-i18n-config.h>
#include <k3dsdk/document_plugin_factory.h>
#include <k3dsdk/node.h>
#include <k3dsdk/time_sink.h>
#include <k3dsdk/vector3.h>
namespace module
{
namespace ode
{
/////////////////////////////////////////////////////////////////////////////
// simulation
class simulation :
public k3d::time_sink<k3d::node>,
public isimulation
{
typedef k3d::time_sink<k3d::node> base;
public:
simulation(k3d::iplugin_factory& Factory, k3d::idocument& Document) :
base(Factory, Document),
m_world_id(dWorldCreate()),
m_space_id(dSimpleSpaceCreate(0)),
m_plane_id(dCreatePlane(m_space_id, 0, 0, 1, 0)),
m_contact_joint_group_id(dJointGroupCreate(0)),
m_last_time(0.0),
m_modified(false),
m_gravity(init_owner(*this) + init_name("gravity") + init_label(_("Gravity")) + init_description(_("Defines the gravity direction and magnitude (if any).")) + init_value(k3d::vector3(0, 0, -9.81)))
{
dInitODE();
m_time.changed_signal().connect(sigc::mem_fun(*this, &simulation::time_changed));
}
~simulation()
{
dJointGroupDestroy(m_contact_joint_group_id);
dGeomDestroy(m_plane_id);
dSpaceDestroy(m_space_id);
dWorldDestroy(m_world_id);
}
const dWorldID world()
{
return m_world_id;
}
const dSpaceID space()
{
return m_space_id;
}
sigc::connection connect_simulation_changed_signal(const sigc::slot<void>& Slot)
{
return m_simulation_changed_signal.connect(Slot);
}
sigc::connection connect_initial_state_signal(const sigc::slot<void>& Slot)
{
return m_initial_state_signal.connect(Slot);
}
sigc::connection connect_update_state_signal(const sigc::slot<void>& Slot)
{
return m_update_state_signal.connect(Slot);
}
void simulation_changed()
{
m_modified = true;
m_simulation_changed_signal.emit();
}
void update()
{
if(!m_modified)
return;
m_modified = false;
const k3d::double_t current_time = m_time.pipeline_value();
const k3d::double_t delta_time = current_time - m_last_time;
m_last_time = current_time;
if(delta_time > 0.0)
{
// Setup the world state ...
const k3d::vector3 gravity = m_gravity.pipeline_value();
dWorldSetGravity(m_world_id, gravity[0], gravity[1], gravity[2]);
// Set body states ...
m_update_state_signal.emit();
// Detect collisions ...
dSpaceCollide(m_space_id, this, raw_collision_callback);
// Step the simulation ...
dWorldStep(m_world_id, delta_time);
// Reset collisions ...
dJointGroupEmpty(m_contact_joint_group_id);
}
else
{
// Set initial body states ...
m_initial_state_signal.emit();
}
}
void time_changed(k3d::ihint*)
{
m_modified = true;
m_simulation_changed_signal.emit();
}
static void raw_collision_callback(void* Data, dGeomID Geometry1, dGeomID Geometry2)
{
reinterpret_cast<simulation*>(Data)->collision_callback(Geometry1, Geometry2);
}
void collision_callback(dGeomID Geometry1, dGeomID Geometry2)
{
const int max_contact_count = 64;
dContact contacts[max_contact_count];
dContact* const contact_begin = contacts;
dContact* const contact_end = contact_begin + dCollide(Geometry1, Geometry2, max_contact_count, &contacts[0].geom, sizeof(dContact));
for(dContact* contact = contact_begin; contact != contact_end; ++contact)
{
// contact->surface.mode = dContactBounce | dContactSoftCFM;
contact->surface.mode = 0;
// contact->surface.mu = dInfinity;
contact->surface.mu = 0;
contact->surface.mu2 = 0;
contact->surface.bounce = 0.0;
contact->surface.bounce_vel = 0.0;
contact->surface.soft_cfm = 0.00;
const dJointID joint_id = dJointCreateContact(m_world_id, m_contact_joint_group_id, contact);
dJointAttach(joint_id, dGeomGetBody(Geometry1), dGeomGetBody(Geometry2));
}
}
static k3d::iplugin_factory& get_factory()
{
static k3d::document_plugin_factory<simulation,
k3d::interface_list<k3d::itime_sink> > factory(
k3d::uuid(0x0df159e6, 0x4d49063c, 0x391217b5, 0x8079599e),
"ODESimulation",
_("Rigid-body dynamics simulation."),
"Simulation",
k3d::iplugin_factory::EXPERIMENTAL);
return factory;
}
private:
dWorldID m_world_id;
dSpaceID m_space_id;
dGeomID m_plane_id;
dJointGroupID m_contact_joint_group_id;
k3d::bool_t m_modified;
sigc::signal<void> m_simulation_changed_signal;
sigc::signal<void> m_initial_state_signal;
sigc::signal<void> m_update_state_signal;
k3d::double_t m_last_time;
k3d_data(k3d::vector3, immutable_name, change_signal, with_undo, local_storage, no_constraint, writable_property, with_serialization) m_gravity;
};
/////////////////////////////////////////////////////////////////////////////
// simulation_factory
k3d::iplugin_factory& simulation_factory()
{
return simulation::get_factory();
}
} // namespace ode
} // namespace module
|