1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2013 Blender Foundation
17  * All rights reserved.
18  */
19 
20 /** \file
21  * \ingroup DNA
22  * \brief Types and defines for representing Rigid Body entities
23  */
24 
25 #pragma once
26 
27 #include "DNA_listBase.h"
28 #include "DNA_object_force_types.h"
29 
30 struct Collection;
31 
32 struct EffectorWeights;
33 
34 /* ******************************** */
35 /* RigidBody World */
36 
37 /* Container for data shared by original and evaluated copies of RigidBodyWorld */
38 typedef struct RigidBodyWorld_Shared {
39   /* cache */
40   struct PointCache *pointcache;
41   struct ListBase ptcaches;
42 
43   /* References to Physics Sim objects. Exist at runtime only ---------------------- */
44   /** Physics sim world (i.e. btDiscreteDynamicsWorld). */
45   void *physics_world;
46 } RigidBodyWorld_Shared;
47 
48 /* RigidBodyWorld (rbw)
49  *
50  * Represents a "simulation scene" existing within the parent scene.
51  */
52 typedef struct RigidBodyWorld {
53   /* Sim World Settings ------------------------------------------------------------- */
54   /** Effectors info. */
55   struct EffectorWeights *effector_weights;
56 
57   /** Group containing objects to use for Rigid Bodies. */
58   struct Collection *group;
59   /** Array to access group objects by index, only used at runtime. */
60   struct Object **objects;
61 
62   /** Group containing objects to use for Rigid Body Constraint.s*/
63   struct Collection *constraints;
64 
65   char _pad[4];
66   /** Last frame world was evaluated for (internal). */
67   float ltime;
68 
69   /** This pointer is shared between all evaluated copies. */
70   struct RigidBodyWorld_Shared *shared;
71   /** Moved to shared->pointcache. */
72   struct PointCache *pointcache DNA_DEPRECATED;
73   /** Moved to shared->ptcaches. */
74   struct ListBase ptcaches DNA_DEPRECATED;
75   /** Number of objects in rigid body group. */
76   int numbodies;
77 
78   /** Number of simulation substeps steps taken per frame. */
79   short substeps_per_frame;
80   /** Number of constraint solver iterations made per simulation step. */
81   short num_solver_iterations;
82 
83   /** (eRigidBodyWorld_Flag) settings for this RigidBodyWorld. */
84   int flag;
85   /** Used to speed up or slow down the simulation. */
86   float time_scale;
87 } RigidBodyWorld;
88 
89 /* Flags for RigidBodyWorld */
90 typedef enum eRigidBodyWorld_Flag {
91   /* should sim world be skipped when evaluating (user setting) */
92   RBW_FLAG_MUTED = (1 << 0),
93   /* sim data needs to be rebuilt */
94   /* RBW_FLAG_NEEDS_REBUILD = (1 << 1), */ /* UNUSED */
95   /* usse split impulse when stepping the simulation */
96   RBW_FLAG_USE_SPLIT_IMPULSE = (1 << 2),
97 } eRigidBodyWorld_Flag;
98 
99 /* ******************************** */
100 /* RigidBody Object */
101 
102 /* Container for data that is shared among CoW copies.
103  *
104  * This is placed in a separate struct so that, for example, the physics_shape
105  * pointer can be replaced without having to update all CoW copies. */
106 #
107 #
108 typedef struct RigidBodyOb_Shared {
109   /* References to Physics Sim objects. Exist at runtime only */
110   /** Physics object representation (i.e. btRigidBody). */
111   void *physics_object;
112   /** Collision shape used by physics sim (i.e. btCollisionShape). */
113   void *physics_shape;
114 } RigidBodyOb_Shared;
115 
116 /* RigidBodyObject (rbo)
117  *
118  * Represents an object participating in a RigidBody sim.
119  * This is attached to each object that is currently
120  * participating in a sim.
121  */
122 typedef struct RigidBodyOb {
123   /* General Settings for this RigidBodyOb */
124   /** (eRigidBodyOb_Type) role of RigidBody in sim . */
125   short type;
126   /** (eRigidBody_Shape) collision shape to use. */
127   short shape;
128 
129   /** (eRigidBodyOb_Flag). */
130   int flag;
131   /** Collision groups that determines which rigid bodies can collide with each other. */
132   int col_groups;
133   /** (eRigidBody_MeshSource) mesh source for mesh based collision shapes. */
134   short mesh_source;
135   char _pad[2];
136 
137   /* Physics Parameters */
138   /** How much object 'weighs' (i.e. absolute 'amount of stuff' it holds). */
139   float mass;
140 
141   /** Resistance of object to movement. */
142   float friction;
143   /** How 'bouncy' object is when it collides. */
144   float restitution;
145 
146   /** Tolerance for detecting collisions. */
147   float margin;
148 
149   /** Damping for linear velocities. */
150   float lin_damping;
151   /** Damping for angular velocities. */
152   float ang_damping;
153 
154   /** Deactivation threshold for linear velocities. */
155   float lin_sleep_thresh;
156   /** Deactivation threshold for angular velocities. */
157   float ang_sleep_thresh;
158 
159   /** Rigid body orientation. */
160   float orn[4];
161   /** Rigid body position. */
162   float pos[3];
163   char _pad1[4];
164 
165   /** This pointer is shared between all evaluated copies. */
166   struct RigidBodyOb_Shared *shared;
167 } RigidBodyOb;
168 
169 /* Participation types for RigidBodyOb */
170 typedef enum eRigidBodyOb_Type {
171   /* active geometry participant in simulation. is directly controlled by sim */
172   RBO_TYPE_ACTIVE = 0,
173   /* passive geometry participant in simulation. is directly controlled by animsys */
174   RBO_TYPE_PASSIVE = 1,
175 } eRigidBodyOb_Type;
176 
177 /* Flags for RigidBodyOb */
178 typedef enum eRigidBodyOb_Flag {
179   /* rigidbody is kinematic (controlled by the animation system) */
180   RBO_FLAG_KINEMATIC = (1 << 0),
181   /* rigidbody needs to be validated (usually set after duplicating and not hooked up yet) */
182   RBO_FLAG_NEEDS_VALIDATE = (1 << 1),
183   /* rigidbody shape needs refreshing (usually after exiting editmode) */
184   RBO_FLAG_NEEDS_RESHAPE = (1 << 2),
185   /* rigidbody can be deactivated */
186   RBO_FLAG_USE_DEACTIVATION = (1 << 3),
187   /* rigidbody is deactivated at the beginning of simulation */
188   RBO_FLAG_START_DEACTIVATED = (1 << 4),
189   /* rigidbody is not dynamically simulated */
190   RBO_FLAG_DISABLED = (1 << 5),
191   /* collision margin is not embedded (only used by convex hull shapes for now) */
192   RBO_FLAG_USE_MARGIN = (1 << 6),
193   /* collision shape deforms during simulation (only for passive triangle mesh shapes) */
194   RBO_FLAG_USE_DEFORM = (1 << 7),
195 } eRigidBodyOb_Flag;
196 
197 /* RigidBody Collision Shape */
198 typedef enum eRigidBody_Shape {
199   /** Simple box (i.e. bounding box). */
200   RB_SHAPE_BOX = 0,
201   /** Sphere. */
202   RB_SHAPE_SPHERE = 1,
203   /** Rounded "pill" shape (i.e. calcium tablets). */
204   RB_SHAPE_CAPSULE = 2,
205   /** Cylinder (i.e. pringles can). */
206   RB_SHAPE_CYLINDER = 3,
207   /** Cone (i.e. party hat). */
208   RB_SHAPE_CONE = 4,
209 
210   /** Convex hull (minimal shrinkwrap encompassing all verts). */
211   RB_SHAPE_CONVEXH = 5,
212   /** Triangulated mesh. */
213   RB_SHAPE_TRIMESH = 6,
214 
215   /* concave mesh approximated using primitives */
216   RB_SHAPE_COMPOUND = 7,
217 } eRigidBody_Shape;
218 
219 typedef enum eRigidBody_MeshSource {
220   /* base mesh */
221   RBO_MESH_BASE = 0,
222   /* only deformations */
223   RBO_MESH_DEFORM = 1,
224   /* final derived mesh */
225   RBO_MESH_FINAL = 2,
226 } eRigidBody_MeshSource;
227 
228 /* ******************************** */
229 /* RigidBody Constraint */
230 
231 /* RigidBodyConstraint (rbc)
232  *
233  * Represents an constraint connecting two rigid bodies.
234  */
235 typedef struct RigidBodyCon {
236   /** First object influenced by the constraint. */
237   struct Object *ob1;
238   /** Second object influenced by the constraint. */
239   struct Object *ob2;
240 
241   /* General Settings for this RigidBodyCon */
242   /** (eRigidBodyCon_Type) role of RigidBody in sim . */
243   short type;
244   /** Number of constraint solver iterations made per simulation step. */
245   short num_solver_iterations;
246 
247   /** (eRigidBodyCon_Flag). */
248   int flag;
249 
250   /** Breaking impulse threshold. */
251   float breaking_threshold;
252   /** Spring implementation to use. */
253   char spring_type;
254   char _pad[3];
255 
256   /* limits */
257   /* translation limits */
258   float limit_lin_x_lower;
259   float limit_lin_x_upper;
260   float limit_lin_y_lower;
261   float limit_lin_y_upper;
262   float limit_lin_z_lower;
263   float limit_lin_z_upper;
264   /* rotation limits */
265   float limit_ang_x_lower;
266   float limit_ang_x_upper;
267   float limit_ang_y_lower;
268   float limit_ang_y_upper;
269   float limit_ang_z_lower;
270   float limit_ang_z_upper;
271 
272   /* spring settings */
273   /* resistance to deformation */
274   float spring_stiffness_x;
275   float spring_stiffness_y;
276   float spring_stiffness_z;
277   float spring_stiffness_ang_x;
278   float spring_stiffness_ang_y;
279   float spring_stiffness_ang_z;
280   /* amount of velocity lost over time */
281   float spring_damping_x;
282   float spring_damping_y;
283   float spring_damping_z;
284   float spring_damping_ang_x;
285   float spring_damping_ang_y;
286   float spring_damping_ang_z;
287 
288   /* motor settings */
289   /** Linear velocity the motor tries to hold. */
290   float motor_lin_target_velocity;
291   /** Angular velocity the motor tries to hold. */
292   float motor_ang_target_velocity;
293   /** Maximum force used to reach linear target velocity. */
294   float motor_lin_max_impulse;
295   /** Maximum force used to reach angular target velocity. */
296   float motor_ang_max_impulse;
297 
298   /* References to Physics Sim object. Exist at runtime only */
299   /** Physics object representation (i.e. btTypedConstraint). */
300   void *physics_constraint;
301 } RigidBodyCon;
302 
303 /* Participation types for RigidBodyOb */
304 typedef enum eRigidBodyCon_Type {
305   /** lets bodies rotate around a specified point */
306   RBC_TYPE_POINT = 0,
307   /** lets bodies rotate around a specified axis */
308   RBC_TYPE_HINGE = 1,
309   /** simulates wheel suspension */
310   /* RBC_TYPE_HINGE2 = 2, */ /* UNUSED */
311   /** restricts movent to a specified axis */
312   RBC_TYPE_SLIDER = 3,
313   /** lets object rotate within a specified cone */
314   /* RBC_TYPE_CONE_TWIST = 4, */ /* UNUSED */
315   /** allows user to specify constraint axes */
316   RBC_TYPE_6DOF = 5,
317   /** like 6DOF but has springs */
318   RBC_TYPE_6DOF_SPRING = 6,
319   /** simulates a universal joint */
320   /* RBC_TYPE_UNIVERSAL = 7, */ /* UNUSED */
321   /** glues two bodies together */
322   RBC_TYPE_FIXED = 8,
323   /** similar to slider but also allows rotation around slider axis */
324   RBC_TYPE_PISTON = 9,
325   /** Simplified spring constraint with only once axis that's
326    * automatically placed between the connected bodies */
327   /* RBC_TYPE_SPRING = 10, */ /* UNUSED */
328   /** dirves bodies by applying linear and angular forces */
329   RBC_TYPE_MOTOR = 11,
330 } eRigidBodyCon_Type;
331 
332 /* Spring implementation type for RigidBodyOb */
333 typedef enum eRigidBodyCon_SpringType {
334   RBC_SPRING_TYPE1 = 0, /* btGeneric6DofSpringConstraint */
335   RBC_SPRING_TYPE2 = 1, /* btGeneric6DofSpring2Constraint */
336 } eRigidBodyCon_SpringType;
337 
338 /* Flags for RigidBodyCon */
339 typedef enum eRigidBodyCon_Flag {
340   /* constraint influences rigid body motion */
341   RBC_FLAG_ENABLED = (1 << 0),
342   /* constraint needs to be validated */
343   RBC_FLAG_NEEDS_VALIDATE = (1 << 1),
344   /* allow constrained bodies to collide */
345   RBC_FLAG_DISABLE_COLLISIONS = (1 << 2),
346   /* constraint can break */
347   RBC_FLAG_USE_BREAKING = (1 << 3),
348   /* constraint use custom number of constraint solver iterations */
349   RBC_FLAG_OVERRIDE_SOLVER_ITERATIONS = (1 << 4),
350   /* limits */
351   RBC_FLAG_USE_LIMIT_LIN_X = (1 << 5),
352   RBC_FLAG_USE_LIMIT_LIN_Y = (1 << 6),
353   RBC_FLAG_USE_LIMIT_LIN_Z = (1 << 7),
354   RBC_FLAG_USE_LIMIT_ANG_X = (1 << 8),
355   RBC_FLAG_USE_LIMIT_ANG_Y = (1 << 9),
356   RBC_FLAG_USE_LIMIT_ANG_Z = (1 << 10),
357   /* springs */
358   RBC_FLAG_USE_SPRING_X = (1 << 11),
359   RBC_FLAG_USE_SPRING_Y = (1 << 12),
360   RBC_FLAG_USE_SPRING_Z = (1 << 13),
361   /* motors */
362   RBC_FLAG_USE_MOTOR_LIN = (1 << 14),
363   RBC_FLAG_USE_MOTOR_ANG = (1 << 15),
364   /* angular springs */
365   RBC_FLAG_USE_SPRING_ANG_X = (1 << 16),
366   RBC_FLAG_USE_SPRING_ANG_Y = (1 << 17),
367   RBC_FLAG_USE_SPRING_ANG_Z = (1 << 18),
368 } eRigidBodyCon_Flag;
369 
370 /* ******************************** */
371