physics_body_2d.h
1 /*************************************************************************/
2 /* physics_body_2d.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_2D_H
30 #define PHYSICS_BODY_2D_H
31 
32 #include "scene/2d/collision_object_2d.h"
33 #include "servers/physics_2d_server.h"
34 #include "vset.h"
35 
36 
38 
40 
41  uint32_t mask;
42  uint32_t collision_mask;
43  Vector2 one_way_collision_direction;
44  float one_way_collision_max_depth;
45 
46 
47  void _set_layers(uint32_t p_mask);
48  uint32_t _get_layers() const;
49 
50 protected:
51 
52  void _notification(int p_what);
53  PhysicsBody2D(Physics2DServer::BodyMode p_mode);
54 
55  static void _bind_methods();
56 public:
57 
58  void set_layer_mask(uint32_t p_mask);
59  uint32_t get_layer_mask() const;
60 
61  void set_collision_mask(uint32_t p_mask);
62  uint32_t get_collision_mask() const;
63 
64 
65  void set_collision_mask_bit(int p_bit, bool p_value);
66  bool get_collision_mask_bit(int p_bit) const;
67 
68  void set_layer_mask_bit(int p_bit, bool p_value);
69  bool get_layer_mask_bit(int p_bit) const;
70 
71  void add_collision_exception_with(Node* p_node); //must be physicsbody
72  void remove_collision_exception_with(Node* p_node);
73 
74  void set_one_way_collision_direction(const Vector2& p_dir);
75  Vector2 get_one_way_collision_direction() const;
76 
77  void set_one_way_collision_max_depth(float p_dir);
78  float get_one_way_collision_max_depth() const;
79 
80  PhysicsBody2D();
81 
82 };
83 
84 class StaticBody2D : public PhysicsBody2D {
85 
86  OBJ_TYPE(StaticBody2D,PhysicsBody2D);
87 
88  Vector2 constant_linear_velocity;
89  real_t constant_angular_velocity;
90 
91  real_t bounce;
92  real_t friction;
93 
94 
95 protected:
96 
97  static void _bind_methods();
98 
99 public:
100 
101  void set_friction(real_t p_friction);
102  real_t get_friction() const;
103 
104  void set_bounce(real_t p_bounce);
105  real_t get_bounce() const;
106 
107 
108  void set_constant_linear_velocity(const Vector2& p_vel);
109  void set_constant_angular_velocity(real_t p_vel);
110 
111  Vector2 get_constant_linear_velocity() const;
112  real_t get_constant_angular_velocity() const;
113 
114  StaticBody2D();
115  ~StaticBody2D();
116 
117 };
118 
119 class RigidBody2D : public PhysicsBody2D {
120 
121  OBJ_TYPE(RigidBody2D,PhysicsBody2D);
122 public:
123 
124  enum Mode {
125  MODE_RIGID,
126  MODE_STATIC,
127  MODE_CHARACTER,
128  MODE_KINEMATIC,
129  };
130 
131  enum CCDMode {
132  CCD_MODE_DISABLED,
133  CCD_MODE_CAST_RAY,
134  CCD_MODE_CAST_SHAPE,
135  };
136 
137 private:
138 
139  bool can_sleep;
140  Physics2DDirectBodyState *state;
141  Mode mode;
142 
143  real_t bounce;
144  real_t mass;
145  real_t friction;
146  real_t gravity_scale;
147  real_t linear_damp;
148  real_t angular_damp;
149 
150  Vector2 linear_velocity;
151  real_t angular_velocity;
152  bool sleeping;
153 
154 
155  int max_contacts_reported;
156 
157  bool custom_integrator;
158 
159  CCDMode ccd_mode;
160 
161 
162  struct ShapePair {
163 
164  int body_shape;
165  int local_shape;
166  bool tagged;
167  bool operator<(const ShapePair& p_sp) const {
168  if (body_shape==p_sp.body_shape)
169  return local_shape < p_sp.local_shape;
170  else
171  return body_shape < p_sp.body_shape;
172  }
173 
174  ShapePair() {}
175  ShapePair(int p_bs, int p_ls) { body_shape=p_bs; local_shape=p_ls; }
176  };
177  struct RigidBody2D_RemoveAction {
178 
179 
180  ObjectID body_id;
181  ShapePair pair;
182 
183  };
184  struct BodyState {
185 
186  //int rc;
187  bool in_scene;
188  VSet<ShapePair> shapes;
189  };
190 
191  struct ContactMonitor {
192 
193  bool locked;
194  Map<ObjectID,BodyState> body_map;
195 
196  };
197 
198 
199  ContactMonitor *contact_monitor;
200  void _body_enter_tree(ObjectID p_id);
201  void _body_exit_tree(ObjectID p_id);
202 
203 
204  void _body_inout(int p_status, ObjectID p_instance, int p_body_shape,int p_local_shape);
205  void _direct_state_changed(Object *p_state);
206 
207  bool _test_motion(const Vector2& p_motion,float p_margin=0.08,const Ref<Physics2DTestMotionResult>& p_result=Ref<Physics2DTestMotionResult>());
208 
209 protected:
210 
211  static void _bind_methods();
212 public:
213 
214  void set_mode(Mode p_mode);
215  Mode get_mode() const;
216 
217  void set_mass(real_t p_mass);
218  real_t get_mass() const;
219 
220  void set_weight(real_t p_weight);
221  real_t get_weight() const;
222 
223  void set_friction(real_t p_friction);
224  real_t get_friction() const;
225 
226  void set_bounce(real_t p_bounce);
227  real_t get_bounce() const;
228 
229  void set_gravity_scale(real_t p_gravity_scale);
230  real_t get_gravity_scale() const;
231 
232  void set_linear_damp(real_t p_linear_damp);
233  real_t get_linear_damp() const;
234 
235  void set_angular_damp(real_t p_angular_damp);
236  real_t get_angular_damp() const;
237 
238  void set_linear_velocity(const Vector2& p_velocity);
239  Vector2 get_linear_velocity() const;
240 
241  void set_axis_velocity(const Vector2& p_axis);
242 
243  void set_angular_velocity(real_t p_velocity);
244  real_t get_angular_velocity() const;
245 
246  void set_use_custom_integrator(bool p_enable);
247  bool is_using_custom_integrator();
248 
249  void set_sleeping(bool p_sleeping);
250  bool is_sleeping() const;
251 
252  void set_can_sleep(bool p_active);
253  bool is_able_to_sleep() const;
254 
255  void set_contact_monitor(bool p_enabled);
256  bool is_contact_monitor_enabled() const;
257 
258  void set_max_contacts_reported(int p_amount);
259  int get_max_contacts_reported() const;
260 
261  void set_continuous_collision_detection_mode(CCDMode p_mode);
262  CCDMode get_continuous_collision_detection_mode() const;
263 
264  void apply_impulse(const Vector2& p_pos, const Vector2& p_impulse);
265 
266  void set_applied_force(const Vector2& p_force);
267  Vector2 get_applied_force() const;
268 
269 
270 
271  Array get_colliding_bodies() const; //function for script
272 
273  RigidBody2D();
274  ~RigidBody2D();
275 
276 };
277 
278 VARIANT_ENUM_CAST(RigidBody2D::Mode);
279 VARIANT_ENUM_CAST(RigidBody2D::CCDMode);
280 
281 
282 
284 
285  OBJ_TYPE(KinematicBody2D,PhysicsBody2D);
286 
287  float margin;
288  bool colliding;
289  Vector2 collision;
290  Vector2 normal;
291  Vector2 collider_vel;
292  ObjectID collider;
293  int collider_shape;
294  Variant collider_metadata;
295  Vector2 travel;
296 
297  Variant _get_collider() const;
298 
299  _FORCE_INLINE_ bool _ignores_mode(Physics2DServer::BodyMode) const;
300 protected:
301 
302  static void _bind_methods();
303 public:
304 
305  Vector2 move(const Vector2& p_motion);
306  Vector2 move_to(const Vector2& p_position);
307 
308  bool test_move(const Vector2& p_motion);
309  bool is_colliding() const;
310 
311  Vector2 get_travel() const;
312  void revert_motion();
313 
314  Vector2 get_collision_pos() const;
315  Vector2 get_collision_normal() const;
316  Vector2 get_collider_velocity() const;
317  ObjectID get_collider() const;
318  int get_collider_shape() const;
319  Variant get_collider_metadata() const;
320 
321  void set_collision_margin(float p_margin);
322  float get_collision_margin() const;
323 
324  KinematicBody2D();
325  ~KinematicBody2D();
326 
327 };
328 
329 
330 #endif // PHYSICS_BODY_2D_H
Definition: physics_body_2d.h:119
Definition: tween_interpolaters.cpp:313
Definition: array.h:38
Definition: reference.h:78
Definition: variant.h:74
Definition: node.h:42
Definition: physics_body_2d.h:37
Definition: physics_body_2d.h:283
Definition: math_2d.h:65
Definition: physics_body_2d.h:84
Definition: object.h:317
Definition: collision_object_2d.h:35