Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Collider.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 
5 using SiliconStudio.Core.Collections;
6 using SiliconStudio.Core.Mathematics;
7 
8 namespace SiliconStudio.Paradox.Physics
9 {
10  public class Collider : IDisposable
11  {
12  /// <summary>
13  /// Initializes a new instance of the <see cref="Collider"/> class.
14  /// </summary>
15  /// <param name="collider">The collider.</param>
16  public Collider(ColliderShape collider)
17  {
18  ColliderShape = collider;
19  }
20 
21  /// <summary>
22  /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
23  /// </summary>
24  public virtual void Dispose()
25  {
26  if (InternalCollider == null) return;
27 
28  InternalCollider.Dispose();
29  InternalCollider = null;
30  }
31 
32  internal PhysicsEngine Engine;
33 
34  bool enabled = true;
35  /// <summary>
36  /// Gets or sets a value indicating whether this <see cref="Collider"/> is enabled.
37  /// </summary>
38  /// <value>
39  /// <c>true</c> if enabled; otherwise, <c>false</c>.
40  /// </value>
41  public bool Enabled
42  {
43  get
44  {
45  return enabled;
46  }
47  set
48  {
49  enabled = value;
50 
51  if (value)
52  {
53  InternalCollider.ActivationState = canSleep ? BulletSharp.ActivationState.ActiveTag : BulletSharp.ActivationState.DisableDeactivation;
54  }
55  else
56  {
57  InternalCollider.ActivationState = BulletSharp.ActivationState.DisableSimulation;
58  }
59  }
60  }
61 
62  bool canSleep = true; //default true
63  /// <summary>
64  /// Gets or sets a value indicating whether this instance can sleep.
65  /// </summary>
66  /// <value>
67  /// <c>true</c> if this instance can sleep; otherwise, <c>false</c>.
68  /// </value>
69  public bool CanSleep
70  {
71  get
72  {
73  return canSleep;
74  }
75  set
76  {
77  canSleep = value;
78 
79  if (enabled)
80  {
81  InternalCollider.ActivationState = value ? BulletSharp.ActivationState.ActiveTag : BulletSharp.ActivationState.DisableDeactivation;
82  }
83  }
84  }
85 
86  /// <summary>
87  /// Gets a value indicating whether this instance is active (awake).
88  /// </summary>
89  /// <value>
90  /// <c>true</c> if this instance is active; otherwise, <c>false</c>.
91  /// </value>
92  public bool IsActive
93  {
94  get
95  {
96  return InternalCollider.IsActive;
97  }
98  }
99 
100  /// <summary>
101  /// Attempts to awake the collider.
102  /// </summary>
103  /// <param name="forceActivation">if set to <c>true</c> [force activation].</param>
104  public void Activate(bool forceActivation = false)
105  {
106  InternalCollider.Activate(forceActivation);
107  }
108 
109  /// <summary>
110  /// Gets or sets the restitution.
111  /// </summary>
112  /// <value>
113  /// The restitution.
114  /// </value>
115  public float Restitution
116  {
117  get
118  {
119  return InternalCollider.Restitution;
120  }
121  set
122  {
123  InternalCollider.Restitution = value;
124  }
125  }
126 
127  /// <summary>
128  /// Gets or sets the friction.
129  /// </summary>
130  /// <value>
131  /// The friction.
132  /// </value>
133  public float Friction
134  {
135  get
136  {
137  return InternalCollider.Friction;
138  }
139  set
140  {
141  InternalCollider.Friction = value;
142  }
143  }
144 
145  /// <summary>
146  /// Gets or sets the rolling friction.
147  /// </summary>
148  /// <value>
149  /// The rolling friction.
150  /// </value>
151  public float RollingFriction
152  {
153  get
154  {
155  return InternalCollider.RollingFriction;
156  }
157  set
158  {
159  InternalCollider.RollingFriction = value;
160  }
161  }
162 
163  /// <summary>
164  /// Gets or sets the CCD motion threshold.
165  /// </summary>
166  /// <value>
167  /// The CCD motion threshold.
168  /// </value>
169  public float CcdMotionThreshold
170  {
171  get
172  {
173  return InternalCollider.CcdMotionThreshold;
174  }
175  set
176  {
177  InternalCollider.CcdMotionThreshold = value;
178  }
179  }
180 
181  /// <summary>
182  /// Gets or sets the CCD swept sphere radius.
183  /// </summary>
184  /// <value>
185  /// The CCD swept sphere radius.
186  /// </value>
187  public float CcdSweptSphereRadius
188  {
189  get
190  {
191  return InternalCollider.CcdSweptSphereRadius;
192  }
193  set
194  {
195  InternalCollider.CcdSweptSphereRadius = value;
196  }
197  }
198 
199  /// <summary>
200  /// Gets or sets a value indicating whether this instance is a trigger.
201  /// </summary>
202  /// <value>
203  /// <c>true</c> if this instance is trigger; otherwise, <c>false</c>.
204  /// </value>
205  public bool IsTrigger
206  {
207  get
208  {
209  return (InternalCollider.CollisionFlags & BulletSharp.CollisionFlags.NoContactResponse) != 0;
210  }
211  set
212  {
213  if (value) InternalCollider.CollisionFlags |= BulletSharp.CollisionFlags.NoContactResponse;
214  else if (InternalCollider.CollisionFlags.HasFlag(BulletSharp.CollisionFlags.NoContactResponse)) InternalCollider.CollisionFlags ^= BulletSharp.CollisionFlags.NoContactResponse;
215  }
216  }
217 
218  internal BulletSharp.CollisionObject InternalCollider;
219 
220  /// <summary>
221  /// Gets the physics world transform.
222  /// </summary>
223  /// <value>
224  /// The physics world transform.
225  /// </value>
226  public Matrix PhysicsWorldTransform
227  {
228  get
229  {
230  return InternalCollider.WorldTransform;
231  }
232  set
233  {
234  InternalCollider.WorldTransform = value;
235  }
236  }
237 
238  /// <summary>
239  /// Gets the collider shape.
240  /// </summary>
241  /// <value>
242  /// The collider shape.
243  /// </value>
244  public ColliderShape ColliderShape { get; internal set; }
245 
246  /// <summary>
247  /// Gets or sets a value indicating whether the Contacts list needs to be always valid.
248  /// This is used to improve performance in the case the list is not needed.
249  /// </summary>
250  /// <value>
251  /// <c>true</c> if [contacts always valid]; otherwise, <c>false</c>.
252  /// </value>
253  public bool ContactsAlwaysValid { get; set; }
254 
255  int eventUsers; //this helps optimize performance
256  internal bool NeedsCollisionCheck
257  {
258  get
259  {
260  return ContactsAlwaysValid || eventUsers > 0;
261  }
262  }
263 
264  readonly object eventsLock = new Object();
265 
266  event EventHandler<CollisionArgs> PrivateOnFirstContactBegin;
267  /// <summary>
268  /// Occurs when the first contant with a collider begins.
269  /// </summary>
270  public event EventHandler<CollisionArgs> OnFirstContactBegin
271  {
272  add
273  {
274  lock (eventsLock)
275  {
276  eventUsers++;
277  PrivateOnFirstContactBegin += value;
278  }
279  }
280  remove
281  {
282  lock (eventsLock)
283  {
284  eventUsers--;
285  PrivateOnFirstContactBegin -= value;
286  }
287  }
288  }
289 
290  internal void PropagateOnFirstContactBegin(CollisionArgs args)
291  {
292  var e = PrivateOnFirstContactBegin;
293  if (e == null) return;
294  e(this, args);
295  }
296 
297  event EventHandler<CollisionArgs> PrivateOnContactBegin;
298  /// <summary>
299  /// Occurs when a contact begins (there could be multiple contacts and contact points).
300  /// </summary>
301  public event EventHandler<CollisionArgs> OnContactBegin
302  {
303  add
304  {
305  lock (eventsLock)
306  {
307  eventUsers++;
308  PrivateOnContactBegin += value;
309  }
310  }
311  remove
312  {
313  lock (eventsLock)
314  {
315  eventUsers--;
316  PrivateOnContactBegin -= value;
317  }
318  }
319  }
320 
321  internal void PropagateOnContactBegin(CollisionArgs args)
322  {
323  var e = PrivateOnContactBegin;
324  if (e == null) return;
325  e(this, args);
326  }
327 
328  event EventHandler<CollisionArgs> PrivateOnContactChange;
329  /// <summary>
330  /// Occurs when a contact changed.
331  /// </summary>
332  public event EventHandler<CollisionArgs> OnContactChange
333  {
334  add
335  {
336  lock (eventsLock)
337  {
338  eventUsers++;
339  PrivateOnContactChange += value;
340  }
341  }
342  remove
343  {
344  lock (eventsLock)
345  {
346  eventUsers--;
347  PrivateOnContactChange -= value;
348  }
349  }
350  }
351 
352  internal void PropagateOnContactChange(CollisionArgs args)
353  {
354  var e = PrivateOnContactChange;
355  if (e == null) return;
356  e(this, args);
357  }
358 
359  event EventHandler<CollisionArgs> PrivateOnLastContactEnd;
360  /// <summary>
361  /// Occurs when the last contact with a collider happened.
362  /// </summary>
363  public event EventHandler<CollisionArgs> OnLastContactEnd
364  {
365  add
366  {
367  lock (eventsLock)
368  {
369  eventUsers++;
370  PrivateOnLastContactEnd += value;
371  }
372  }
373  remove
374  {
375  lock (eventsLock)
376  {
377  eventUsers--;
378  PrivateOnLastContactEnd -= value;
379  }
380  }
381  }
382 
383  internal void PropagateOnLastContactEnd(CollisionArgs args)
384  {
385  var e = PrivateOnLastContactEnd;
386  if (e == null) return;
387  e(this, args);
388  }
389 
390  event EventHandler<CollisionArgs> PrivateOnContactEnd;
391  /// <summary>
392  /// Occurs when a contact ended.
393  /// </summary>
394  public event EventHandler<CollisionArgs> OnContactEnd
395  {
396  add
397  {
398  lock (eventsLock)
399  {
400  eventUsers++;
401  PrivateOnContactEnd += value;
402  }
403  }
404  remove
405  {
406  lock (eventsLock)
407  {
408  eventUsers--;
409  PrivateOnContactEnd -= value;
410  }
411  }
412  }
413 
414  internal void PropagateOnContactEnd(CollisionArgs args)
415  {
416  var e = PrivateOnContactEnd;
417  if (e == null) return;
418  e(this, args);
419  }
420 
421  readonly FastList<Contact> contacts = new FastList<Contact>();
422  /// <summary>
423  /// Gets the contacts.
424  /// </summary>
425  /// <value>
426  /// The contacts.
427  /// </value>
428  public FastList<Contact> Contacts
429  {
430  get
431  {
432  return contacts;
433  }
434  }
435 
436  /// <summary>
437  /// Gets or sets the tag.
438  /// </summary>
439  /// <value>
440  /// The tag.
441  /// </value>
442  public string Tag { get; set; }
443 
444  /// <summary>
445  /// Gets or sets the entity object.
446  /// Should always cast this as an Entity
447  /// </summary>
448  /// <value>
449  /// The entity object.
450  /// </value>
451  public object EntityObject { get; set; }
452  }
453 }
Collider(ColliderShape collider)
Initializes a new instance of the Collider class.
Definition: Collider.cs:16
void Activate(bool forceActivation=false)
Attempts to awake the collider.
Definition: Collider.cs:104
virtual void Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resourc...
Definition: Collider.cs:24
using SiliconStudio.Paradox. Physics
Represents a 4x4 mathematical matrix.
Definition: Matrix.cs:47