From b3091982106112cc98c4fd81efbab2e29ed37197 Mon Sep 17 00:00:00 2001 From: enn0x Date: Tue, 27 Nov 2012 23:32:59 +0000 Subject: [PATCH] PhysX: adding controller report callbacks. --- panda/src/physx/Sources.pp | 6 ++ panda/src/physx/config_physx.cxx | 5 + panda/src/physx/p3physx_composite.cxx | 2 + panda/src/physx/physxControllerReport.I | 22 +++++ panda/src/physx/physxControllerReport.cxx | 39 ++++++-- panda/src/physx/physxControllerReport.h | 8 ++ panda/src/physx/physxControllerShapeHit.I | 101 ++++++++++++++++++++ panda/src/physx/physxControllerShapeHit.cxx | 19 ++++ panda/src/physx/physxControllerShapeHit.h | 71 ++++++++++++++ panda/src/physx/physxControllersHit.I | 53 ++++++++++ panda/src/physx/physxControllersHit.cxx | 19 ++++ panda/src/physx/physxControllersHit.h | 65 +++++++++++++ panda/src/physx/physxScene.I | 22 +++++ panda/src/physx/physxScene.h | 4 + 14 files changed, 426 insertions(+), 10 deletions(-) create mode 100644 panda/src/physx/physxControllerShapeHit.I create mode 100644 panda/src/physx/physxControllerShapeHit.cxx create mode 100644 panda/src/physx/physxControllerShapeHit.h create mode 100644 panda/src/physx/physxControllersHit.I create mode 100644 panda/src/physx/physxControllersHit.cxx create mode 100644 panda/src/physx/physxControllersHit.h diff --git a/panda/src/physx/Sources.pp b/panda/src/physx/Sources.pp index 404abc3ae1..5fef3e6b4f 100644 --- a/panda/src/physx/Sources.pp +++ b/panda/src/physx/Sources.pp @@ -44,6 +44,8 @@ physxController.I physxController.h \ physxControllerDesc.I physxControllerDesc.h \ physxControllerReport.I physxControllerReport.h \ + physxControllerShapeHit.I physxControllerShapeHit.h \ + physxControllersHit.I physxControllersHit.h \ physxConvexForceFieldShape.I physxConvexForceFieldShape.h \ physxConvexForceFieldShapeDesc.I physxConvexForceFieldShapeDesc.h \ physxConvexMesh.I physxConvexMesh.h \ @@ -173,6 +175,8 @@ physxController.cxx \ physxControllerDesc.cxx \ physxControllerReport.cxx \ + physxControllerShapeHit.cxx \ + physxControllersHit.cxx \ physxConvexForceFieldShape.cxx \ physxConvexForceFieldShapeDesc.cxx \ physxConvexMesh.cxx \ @@ -302,6 +306,8 @@ physxController.I physxController.h \ physxControllerDesc.I physxControllerDesc.h \ physxControllerReport.I physxControllerReport.h \ + physxControllerShapeHit.I physxControllerShapeHit.h \ + physxControllersHit.I physxControllersHit.h \ physxConvexForceFieldShape.I physxConvexForceFieldShape.h \ physxConvexForceFieldShapeDesc.I physxConvexForceFieldShapeDesc.h \ physxConvexMesh.I physxConvexMesh.h \ diff --git a/panda/src/physx/config_physx.cxx b/panda/src/physx/config_physx.cxx index 8aca136b2e..e97d8360c2 100644 --- a/panda/src/physx/config_physx.cxx +++ b/panda/src/physx/config_physx.cxx @@ -29,6 +29,9 @@ #include "physxContactPair.h" #include "physxContactPoint.h" #include "physxController.h" +#include "physxControllerReport.h" +#include "physxControllerShapeHit.h" +#include "physxControllersHit.h" #include "physxConvexMesh.h" #include "physxConvexForceFieldShape.h" #include "physxConvexShape.h" @@ -131,6 +134,8 @@ init_libphysx() { PhysxContactPair::init_type(); PhysxContactPoint::init_type(); PhysxController::init_type(); + PhysxControllerShapeHit::init_type(); + PhysxControllersHit::init_type(); PhysxConvexMesh::init_type(); PhysxConvexForceFieldShape::init_type(); PhysxConvexShape::init_type(); diff --git a/panda/src/physx/p3physx_composite.cxx b/panda/src/physx/p3physx_composite.cxx index 124bf74500..fed31a06d4 100644 --- a/panda/src/physx/p3physx_composite.cxx +++ b/panda/src/physx/p3physx_composite.cxx @@ -30,6 +30,8 @@ #include "physxController.cxx" #include "physxControllerDesc.cxx" #include "physxControllerReport.cxx" +#include "physxControllerShapeHit.cxx" +#include "physxControllersHit.cxx" #include "physxConstraintDominance.cxx" #include "physxConvexForceFieldShape.cxx" #include "physxConvexForceFieldShapeDesc.cxx" diff --git a/panda/src/physx/physxControllerReport.I b/panda/src/physx/physxControllerReport.I index 6267154074..fe8ef8d147 100644 --- a/panda/src/physx/physxControllerReport.I +++ b/panda/src/physx/physxControllerReport.I @@ -34,3 +34,25 @@ INLINE PhysxControllerReport:: } +//////////////////////////////////////////////////////////////////// +// Function: PhysxControllerReport::set_shape_callback +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE void PhysxControllerReport:: +set_shape_hit_callback(PT(CallbackObject) cbobj) { + + _shape_hit_cbobj = cbobj; +} + +//////////////////////////////////////////////////////////////////// +// Function: PhysxControllerReport::set_shape_callback +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE void PhysxControllerReport:: +set_controller_hit_callback(PT(CallbackObject) cbobj) { + + _controller_hit_cbobj = cbobj; +} + diff --git a/panda/src/physx/physxControllerReport.cxx b/panda/src/physx/physxControllerReport.cxx index 6f4cb7b337..8ce216e8dc 100644 --- a/panda/src/physx/physxControllerReport.cxx +++ b/panda/src/physx/physxControllerReport.cxx @@ -25,6 +25,9 @@ void PhysxControllerReport:: enable() { _enabled = true; + + _shape_hit_cbobj = NULL; + _controller_hit_cbobj = NULL; } //////////////////////////////////////////////////////////////////// @@ -63,13 +66,21 @@ onShapeHit( const NxControllerShapeHit& hit ) { _pcollector.start(); - if (1 && hit.shape) { - NxActor& actor = hit.shape->getActor(); - if (actor.isDynamic() && !actor.readBodyFlag(NX_BF_KINEMATIC)) { - if (hit.dir.z == 0.0f) { - NxF32 controllerMass = hit.controller->getActor()->getMass(); - NxF32 coeff = actor.getMass() * hit.length * controllerMass; - actor.addForceAtLocalPos(hit.dir*coeff, NxVec3(0,0,0), NX_IMPULSE); + if (_shape_hit_cbobj) { + // Callback + PhysxControllerShapeHit cbdata(hit); + _shape_hit_cbobj->do_callback(&cbdata); + } + else { + // Default implementation + if (1 && hit.shape) { + NxActor& actor = hit.shape->getActor(); + if (actor.isDynamic() && !actor.readBodyFlag(NX_BF_KINEMATIC)) { + if (hit.dir.z == 0.0f) { + NxF32 controllerMass = hit.controller->getActor()->getMass(); + NxF32 coeff = actor.getMass() * hit.length * controllerMass; + actor.addForceAtLocalPos(hit.dir*coeff, NxVec3(0,0,0), NX_IMPULSE); + } } } } @@ -93,9 +104,17 @@ onControllerHit(const NxControllersHit& hit) { _pcollector.start(); - if (1 && hit.other) { - // For now other controllers are unpushable. --TODO-- - //return NX_ACTION_PUSH; is not implemented! + if (_controller_hit_cbobj) { + // Callback + PhysxControllersHit cbdata(hit); + _controller_hit_cbobj->do_callback(&cbdata); + } + else { + // Default implementation + if (1 && hit.other) { + // For now other controllers are unpushable. --TODO-- + //return NX_ACTION_PUSH; is not implemented! + } } _pcollector.stop(); diff --git a/panda/src/physx/physxControllerReport.h b/panda/src/physx/physxControllerReport.h index 5f7e017989..7e49a73522 100644 --- a/panda/src/physx/physxControllerReport.h +++ b/panda/src/physx/physxControllerReport.h @@ -16,6 +16,7 @@ #define PHYSXCONTROLLERREPORT_H #include "pandabase.h" +#include "callbackObject.h" #include "pStatCollector.h" #include "physx_includes.h" @@ -35,11 +36,18 @@ public: void disable(); bool is_enabled() const; + INLINE void set_shape_hit_callback(PT(CallbackObject) cbobj); + INLINE void set_controller_hit_callback(PT(CallbackObject) cbobj); + virtual NxControllerAction onShapeHit(const NxControllerShapeHit& hit); virtual NxControllerAction onControllerHit(const NxControllersHit& hit); private: bool _enabled; + + PT(CallbackObject) _shape_hit_cbobj; + PT(CallbackObject) _controller_hit_cbobj; + static PStatCollector _pcollector; }; diff --git a/panda/src/physx/physxControllerShapeHit.I b/panda/src/physx/physxControllerShapeHit.I new file mode 100644 index 0000000000..d3455126a4 --- /dev/null +++ b/panda/src/physx/physxControllerShapeHit.I @@ -0,0 +1,101 @@ +// Filename: physxControllerShapeHit.I +// Created by: enn0x (28Nov12) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////// +// Function: PhysxControllerShapeHit::Constructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE PhysxControllerShapeHit:: +PhysxControllerShapeHit(const NxControllerShapeHit &hit) : + _hit(hit) { + +} + +//////////////////////////////////////////////////////////////////// +// Function: PhysxControllerShapeHit::get_controller +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE PhysxController *PhysxControllerShapeHit:: +get_controller() const { + + if (_hit.controller) { + return (PhysxController *)(_hit.controller->getUserData()); + } + else { + return NULL; + } +} + +//////////////////////////////////////////////////////////////////// +// Function: PhysxControllerShapeHit::get_shape +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE PhysxShape *PhysxControllerShapeHit:: +get_shape() const { + + if (_hit.shape) { + return (PhysxShape *)(_hit.shape->userData); + } + else { + return NULL; + } +} + +//////////////////////////////////////////////////////////////////// +// Function: PhysxControllerShapeHit::get_world_pos +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE LPoint3 PhysxControllerShapeHit:: +get_world_pos() const { + + return PhysxManager::nxExtVec3_to_point3(_hit.worldPos); +} + +//////////////////////////////////////////////////////////////////// +// Function: PhysxControllerShapeHit::get_world_normal +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE LVector3 PhysxControllerShapeHit:: +get_world_normal() const { + + return PhysxManager::nxVec3_to_vec3(_hit.worldNormal); +} + +//////////////////////////////////////////////////////////////////// +// Function: PhysxControllerShapeHit::get_dir +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE LVector3 PhysxControllerShapeHit:: +get_dir() const { + + return PhysxManager::nxVec3_to_vec3(_hit.dir); +} + +//////////////////////////////////////////////////////////////////// +// Function: PhysxControllerShapeHit::get_length +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE PN_stdfloat PhysxControllerShapeHit:: +get_length() const { + + return (PN_stdfloat)_hit.length; +} + diff --git a/panda/src/physx/physxControllerShapeHit.cxx b/panda/src/physx/physxControllerShapeHit.cxx new file mode 100644 index 0000000000..b2e7dc3511 --- /dev/null +++ b/panda/src/physx/physxControllerShapeHit.cxx @@ -0,0 +1,19 @@ +// Filename: physxControllerShapeHit.cxx +// Created by: enn0x (28Nov12) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "physxControllerShapeHit.h" + +TypeHandle PhysxControllerShapeHit::_type_handle; + + diff --git a/panda/src/physx/physxControllerShapeHit.h b/panda/src/physx/physxControllerShapeHit.h new file mode 100644 index 0000000000..d92c2b1d1a --- /dev/null +++ b/panda/src/physx/physxControllerShapeHit.h @@ -0,0 +1,71 @@ +// Filename: physxControllerShapeHit.h +// Created by: enn0x (28Nov12) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef PHYSXCONTROLLERSHAPEHIT +#define PHYSXCONTROLLERSHAPEHIT + +#include "pandabase.h" +#include "callbackData.h" +#include "callbackObject.h" + +#include "physx_includes.h" +#include "physxManager.h" + +class PhysxController; +class PhysxShape; + +//////////////////////////////////////////////////////////////////// +// Class : PhysxControllerShapeHit +// Description : +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDAPHYSX PhysxControllerShapeHit : public CallbackData { + +PUBLISHED: + INLINE PhysxControllerShapeHit(const NxControllerShapeHit &hit); + + INLINE PhysxController *get_controller() const; + INLINE PhysxShape *get_shape() const; + INLINE LPoint3 get_world_pos() const; + INLINE LVector3 get_world_normal() const; + INLINE LVector3 get_dir() const; + INLINE PN_stdfloat get_length() const; + +private: + const NxControllerShapeHit &_hit; + +//////////////////////////////////////////////////////////////////// +public: + static TypeHandle get_class_type() { + return _type_handle; + } + static void init_type() { + CallbackData::init_type(); + register_type(_type_handle, "PhysxControllerShapeHit", + CallbackData::get_class_type()); + } + virtual TypeHandle get_type() const { + return get_class_type(); + } + virtual TypeHandle force_init_type() { + init_type(); + return get_class_type(); + } + +private: + static TypeHandle _type_handle; +}; + +#include "physxControllerShapeHit.I" + +#endif // PHYSXCONTROLLERSHAPEHIT diff --git a/panda/src/physx/physxControllersHit.I b/panda/src/physx/physxControllersHit.I new file mode 100644 index 0000000000..124a23664a --- /dev/null +++ b/panda/src/physx/physxControllersHit.I @@ -0,0 +1,53 @@ +// Filename: physxControllersHit.I +// Created by: enn0x (28Nov12) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////// +// Function: PhysxControllersHit::Constructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE PhysxControllersHit:: +PhysxControllersHit(const NxControllersHit &hit) : + _hit(hit) { + +} + +//////////////////////////////////////////////////////////////////// +// Function: PhysxControllersHit::get_controller +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE PhysxController *PhysxControllersHit:: +get_controller() const { + + NxController *controllerPtr = _hit.controller; + PhysxController *controller = controllerPtr ? (PhysxController *)controllerPtr : NULL; + + return controller; +} + +//////////////////////////////////////////////////////////////////// +// Function: PhysxControllersHit::get_other +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE PhysxController *PhysxControllersHit:: +get_other() const { + + NxController *otherPtr = _hit.other; + PhysxController *other = otherPtr ? (PhysxController *)otherPtr : NULL; + + return other; +} + diff --git a/panda/src/physx/physxControllersHit.cxx b/panda/src/physx/physxControllersHit.cxx new file mode 100644 index 0000000000..123ecd2d89 --- /dev/null +++ b/panda/src/physx/physxControllersHit.cxx @@ -0,0 +1,19 @@ +// Filename: physxControllersHit.cxx +// Created by: enn0x (28Nov12) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "physxControllersHit.h" + +TypeHandle PhysxControllersHit::_type_handle; + + diff --git a/panda/src/physx/physxControllersHit.h b/panda/src/physx/physxControllersHit.h new file mode 100644 index 0000000000..def69fc96d --- /dev/null +++ b/panda/src/physx/physxControllersHit.h @@ -0,0 +1,65 @@ +// Filename: physxControllersHit.h +// Created by: enn0x (28Nov12) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef PHYSXCONTROLLERSHIT +#define PHYSXCONTROLLERSHIT + +#include "pandabase.h" +#include "callbackData.h" +#include "callbackObject.h" + +#include "physx_includes.h" + +class PhysxController; + +//////////////////////////////////////////////////////////////////// +// Class : PhysxControllersHit +// Description : +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDAPHYSX PhysxControllersHit : public CallbackData { + +PUBLISHED: + INLINE PhysxControllersHit(const NxControllersHit &hit); + + INLINE PhysxController *get_controller() const; + INLINE PhysxController *get_other() const; + +private: + const NxControllersHit &_hit; + +//////////////////////////////////////////////////////////////////// +public: + static TypeHandle get_class_type() { + return _type_handle; + } + static void init_type() { + CallbackData::init_type(); + register_type(_type_handle, "PhysxControllersHit", + CallbackData::get_class_type()); + } + virtual TypeHandle get_type() const { + return get_class_type(); + } + virtual TypeHandle force_init_type() { + init_type(); + return get_class_type(); + } + +private: + static TypeHandle _type_handle; +}; + +#include "physxControllersHit.I" + +#endif // PHYSXCONTROLLERSHIT diff --git a/panda/src/physx/physxScene.I b/panda/src/physx/physxScene.I index 6ae4bcf86b..5f5510630e 100644 --- a/panda/src/physx/physxScene.I +++ b/panda/src/physx/physxScene.I @@ -69,3 +69,25 @@ ls(ostream &out, int indent_level) const { _softbodies.ls(out, indent_level); } +//////////////////////////////////////////////////////////////////// +// Function: PhysxScene::set_controller_shape_hit_callback +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE void PhysxScene:: +set_controller_shape_hit_callback(PT(CallbackObject) cbobj) { + + _controller_report.set_shape_hit_callback(cbobj); +} + +//////////////////////////////////////////////////////////////////// +// Function: PhysxScene::set_controller_controller_hit_callback +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE void PhysxScene:: +set_controller_controller_hit_callback(PT(CallbackObject) cbobj) { + + _controller_report.set_controller_hit_callback(cbobj); +} + diff --git a/panda/src/physx/physxScene.h b/panda/src/physx/physxScene.h index 35c992124b..0d48ea2797 100644 --- a/panda/src/physx/physxScene.h +++ b/panda/src/physx/physxScene.h @@ -17,6 +17,7 @@ #include "pandabase.h" #include "luse.h" +#include "callbackObject.h" #include "physxObject.h" #include "physxObjectCollection.h" @@ -89,6 +90,9 @@ PUBLISHED: void enable_controller_reporting(bool enabled); bool is_controller_reporting_enabled() const; + INLINE void set_controller_shape_hit_callback(PT(CallbackObject) cbobj); + INLINE void set_controller_controller_hit_callback(PT(CallbackObject) cbobj); + void set_gravity(const LVector3f &gravity); LVector3f get_gravity() const;