Panda3D
physxControllerReport.cxx
1 // Filename: physxControllerReport.cxx
2 // Created by: enn0x (24Sep09)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "physxControllerReport.h"
16 
17 PStatCollector PhysxControllerReport::_pcollector("App:PhysX:Controller Reporting");
18 
19 ////////////////////////////////////////////////////////////////////
20 // Function: PhysxControllerReport::enable
21 // Access: Public
22 // Description:
23 ////////////////////////////////////////////////////////////////////
24 void PhysxControllerReport::
25 enable() {
26 
27  _enabled = true;
28 
29  _shape_hit_cbobj = NULL;
30  _controller_hit_cbobj = NULL;
31 }
32 
33 ////////////////////////////////////////////////////////////////////
34 // Function: PhysxControllerReport::disable
35 // Access: Public
36 // Description:
37 ////////////////////////////////////////////////////////////////////
38 void PhysxControllerReport::
39 disable() {
40 
41  _enabled = false;
42 }
43 
44 ////////////////////////////////////////////////////////////////////
45 // Function: PhysxControllerReport::is_enabled
46 // Access: Public
47 // Description:
48 ////////////////////////////////////////////////////////////////////
49 bool PhysxControllerReport::
50 is_enabled() const {
51 
52  return _enabled;
53 }
54 
55 ////////////////////////////////////////////////////////////////////
56 // Function: PhysxControllerReport::onShapeHit
57 // Access: Public
58 // Description:
59 ////////////////////////////////////////////////////////////////////
60 NxControllerAction PhysxControllerReport::
61 onShapeHit( const NxControllerShapeHit& hit ) {
62 
63  if (!_enabled) {
64  return NX_ACTION_NONE;
65  }
66 
67  _pcollector.start();
68 
69  if (_shape_hit_cbobj) {
70  // Callback
71  PhysxControllerShapeHit cbdata(hit);
72  _shape_hit_cbobj->do_callback(&cbdata);
73  }
74  else {
75  // Default implementation
76  if (1 && hit.shape) {
77  NxActor& actor = hit.shape->getActor();
78  if (actor.isDynamic() && !actor.readBodyFlag(NX_BF_KINEMATIC)) {
79  if (hit.dir.z == 0.0f) {
80  NxF32 controllerMass = hit.controller->getActor()->getMass();
81  NxF32 coeff = actor.getMass() * hit.length * controllerMass;
82  actor.addForceAtLocalPos(hit.dir*coeff, NxVec3(0,0,0), NX_IMPULSE);
83  }
84  }
85  }
86  }
87 
88  _pcollector.stop();
89 
90  return NX_ACTION_NONE;
91 }
92 
93 ////////////////////////////////////////////////////////////////////
94 // Function: PhysxControllerReport::onControllerHit
95 // Access: Public
96 // Description:
97 ////////////////////////////////////////////////////////////////////
98 NxControllerAction PhysxControllerReport::
99 onControllerHit(const NxControllersHit& hit) {
100 
101  if (!_enabled) {
102  return NX_ACTION_NONE;
103  }
104 
105  _pcollector.start();
106 
107  if (_controller_hit_cbobj) {
108  // Callback
109  PhysxControllersHit cbdata(hit);
110  _controller_hit_cbobj->do_callback(&cbdata);
111  }
112  else {
113  // Default implementation
114  if (1 && hit.other) {
115  // For now other controllers are unpushable. --TODO--
116  //return NX_ACTION_PUSH; is not implemented!
117  }
118  }
119 
120  _pcollector.stop();
121 
122  return NX_ACTION_NONE;
123 }
124 
A lightweight class that represents a single element that may be timed and/or counted via stats...