physics_body.h
1 /*************************************************************************/
2 /* physics_body.h */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* http://www.godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
9 /* */
10 /* Permission is hereby granted, free of charge, to any person obtaining */
11 /* a copy of this software and associated documentation files (the */
12 /* "Software"), to deal in the Software without restriction, including */
13 /* without limitation the rights to use, copy, modify, merge, publish, */
14 /* distribute, sublicense, and/or sell copies of the Software, and to */
15 /* permit persons to whom the Software is furnished to do so, subject to */
16 /* the following conditions: */
17 /* */
18 /* The above copyright notice and this permission notice shall be */
19 /* included in all copies or substantial portions of the Software. */
20 /* */
21 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
22 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
23 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
24 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
25 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
26 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
27 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
28 /*************************************************************************/
29 #ifndef PHYSICS_BODY__H
30 #define PHYSICS_BODY__H
31 
32 #include "scene/3d/collision_object.h"
33 #include "servers/physics_server.h"
34 #include "vset.h"
35 
36 
37 class PhysicsBody : public CollisionObject {
38 
39  OBJ_TYPE(PhysicsBody,CollisionObject);
40 
41  uint32_t layer_mask;
42 protected:
43 
44  static void _bind_methods();
45  void _notification(int p_what);
46  PhysicsBody(PhysicsServer::BodyMode p_mode);
47 public:
48 
49  virtual Vector3 get_linear_velocity() const;
50  virtual Vector3 get_angular_velocity() const;
51  virtual float get_inverse_mass() const;
52 
53  void set_layer_mask(uint32_t p_mask);
54  uint32_t get_layer_mask() const;
55 
56  void add_collision_exception_with(Node* p_node); //must be physicsbody
57  void remove_collision_exception_with(Node* p_node);
58 
59 
60 
61  PhysicsBody();
62 
63 };
64 
65 class StaticBody : public PhysicsBody {
66 
67  OBJ_TYPE(StaticBody,PhysicsBody);
68 
69  Vector3 constant_linear_velocity;
70  Vector3 constant_angular_velocity;
71 
72  real_t bounce;
73  real_t friction;
74 
75 
76 protected:
77 
78  static void _bind_methods();
79 
80 public:
81 
82 
83  void set_friction(real_t p_friction);
84  real_t get_friction() const;
85 
86  void set_bounce(real_t p_bounce);
87  real_t get_bounce() const;
88 
89 
90  void set_constant_linear_velocity(const Vector3& p_vel);
91  void set_constant_angular_velocity(const Vector3& p_vel);
92 
93  Vector3 get_constant_linear_velocity() const;
94  Vector3 get_constant_angular_velocity() const;
95 
96  StaticBody();
97  ~StaticBody();
98 
99 };
100 
101 class RigidBody : public PhysicsBody {
102 
103  OBJ_TYPE(RigidBody,PhysicsBody);
104 public:
105 
106  enum Mode {
107  MODE_RIGID,
108  MODE_STATIC,
109  MODE_CHARACTER,
110  MODE_KINEMATIC,
111  };
112 
113  enum AxisLock {
114  AXIS_LOCK_DISABLED,
115  AXIS_LOCK_X,
116  AXIS_LOCK_Y,
117  AXIS_LOCK_Z,
118  };
119 
120 private:
121 
122  bool can_sleep;
123  PhysicsDirectBodyState *state;
124  Mode mode;
125 
126  real_t bounce;
127  real_t mass;
128  real_t friction;
129 
130  Vector3 linear_velocity;
131  Vector3 angular_velocity;
132  real_t gravity_scale;
133  real_t linear_damp;
134  real_t angular_damp;
135 
136  bool sleeping;
137  bool ccd;
138 
139  AxisLock axis_lock;
140 
141 
142  int max_contacts_reported;
143 
144  bool custom_integrator;
145 
146 
147  struct ShapePair {
148 
149  int body_shape;
150  int local_shape;
151  bool tagged;
152  bool operator<(const ShapePair& p_sp) const {
153  if (body_shape==p_sp.body_shape)
154  return local_shape < p_sp.local_shape;
155  else
156  return body_shape < p_sp.body_shape;
157  }
158 
159  ShapePair() {}
160  ShapePair(int p_bs, int p_ls) { body_shape=p_bs; local_shape=p_ls; }
161  };
162  struct RigidBody_RemoveAction {
163 
164 
165  ObjectID body_id;
166  ShapePair pair;
167 
168  };
169  struct BodyState {
170 
171  //int rc;
172  bool in_tree;
173  VSet<ShapePair> shapes;
174  };
175 
176  struct ContactMonitor {
177 
178  bool locked;
179  Map<ObjectID,BodyState> body_map;
180 
181  };
182 
183 
184  ContactMonitor *contact_monitor;
185  void _body_enter_tree(ObjectID p_id);
186  void _body_exit_tree(ObjectID p_id);
187 
188 
189  void _body_inout(int p_status, ObjectID p_instance, int p_body_shape,int p_local_shape);
190  void _direct_state_changed(Object *p_state);
191 
192 
193 protected:
194 
195  void _notification(int p_what);
196  static void _bind_methods();
197 public:
198 
199  void set_mode(Mode p_mode);
200  Mode get_mode() const;
201 
202  void set_mass(real_t p_mass);
203  real_t get_mass() const;
204 
205  virtual float get_inverse_mass() const { return 1.0/mass; }
206 
207  void set_weight(real_t p_weight);
208  real_t get_weight() const;
209 
210  void set_friction(real_t p_friction);
211  real_t get_friction() const;
212 
213  void set_bounce(real_t p_bounce);
214  real_t get_bounce() const;
215 
216  void set_linear_velocity(const Vector3& p_velocity);
217  Vector3 get_linear_velocity() const;
218 
219  void set_axis_velocity(const Vector3& p_axis);
220 
221  void set_angular_velocity(const Vector3&p_velocity);
222  Vector3 get_angular_velocity() const;
223 
224  void set_gravity_scale(real_t p_gravity_scale);
225  real_t get_gravity_scale() const;
226 
227  void set_linear_damp(real_t p_linear_damp);
228  real_t get_linear_damp() const;
229 
230  void set_angular_damp(real_t p_angular_damp);
231  real_t get_angular_damp() const;
232 
233 
234  void set_use_custom_integrator(bool p_enable);
235  bool is_using_custom_integrator();
236 
237  void set_sleeping(bool p_sleeping);
238  bool is_sleeping() const;
239 
240  void set_can_sleep(bool p_active);
241  bool is_able_to_sleep() const;
242 
243  void set_contact_monitor(bool p_enabled);
244  bool is_contact_monitor_enabled() const;
245 
246  void set_max_contacts_reported(int p_amount);
247  int get_max_contacts_reported() const;
248 
249  void set_use_continuous_collision_detection(bool p_enable);
250  bool is_using_continuous_collision_detection() const;
251 
252  void set_axis_lock(AxisLock p_lock);
253  AxisLock get_axis_lock() const;
254 
255  Array get_colliding_bodies() const;
256 
257  void apply_impulse(const Vector3& p_pos, const Vector3& p_impulse);
258 
259  RigidBody();
260  ~RigidBody();
261 
262 };
263 
264 VARIANT_ENUM_CAST(RigidBody::Mode);
265 VARIANT_ENUM_CAST(RigidBody::AxisLock);
266 
267 
268 
269 
270 
271 class KinematicBody : public PhysicsBody {
272 
273  OBJ_TYPE(KinematicBody,PhysicsBody);
274 
275  float margin;
276  bool collide_static;
277  bool collide_rigid;
278  bool collide_kinematic;
279  bool collide_character;
280 
281  bool colliding;
282  Vector3 collision;
283  Vector3 normal;
284  Vector3 collider_vel;
285  ObjectID collider;
286  int collider_shape;
287 
288 
289 
290  Variant _get_collider() const;
291 
292  _FORCE_INLINE_ bool _ignores_mode(PhysicsServer::BodyMode) const;
293 protected:
294 
295  static void _bind_methods();
296 public:
297 
298  enum {
299  SLIDE_FLAG_FLOOR,
300  SLIDE_FLAG_WALL,
301  SLIDE_FLAG_ROOF
302  };
303 
304  Vector3 move(const Vector3& p_motion);
305  Vector3 move_to(const Vector3& p_position);
306 
307  bool can_teleport_to(const Vector3& p_position);
308  bool is_colliding() const;
309  Vector3 get_collision_pos() const;
310  Vector3 get_collision_normal() const;
311  Vector3 get_collider_velocity() const;
312  ObjectID get_collider() const;
313  int get_collider_shape() const;
314 
315  void set_collide_with_static_bodies(bool p_enable);
316  bool can_collide_with_static_bodies() const;
317 
318  void set_collide_with_rigid_bodies(bool p_enable);
319  bool can_collide_with_rigid_bodies() const;
320 
321  void set_collide_with_kinematic_bodies(bool p_enable);
322  bool can_collide_with_kinematic_bodies() const;
323 
324  void set_collide_with_character_bodies(bool p_enable);
325  bool can_collide_with_character_bodies() const;
326 
327  void set_collision_margin(float p_margin);
328  float get_collision_margin() const;
329 
330  KinematicBody();
331  ~KinematicBody();
332 
333 };
334 
335 #endif // PHYSICS_BODY__H
Definition: tween_interpolaters.cpp:313
Definition: array.h:38
Definition: physics_body.h:65
Definition: variant.h:74
Definition: node.h:42
Definition: vector3.h:38
Definition: physics_body.h:101
Definition: physics_body.h:271
Definition: collision_object.h:35
Definition: object.h:317
Definition: physics_body.h:37