| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- /*
- Copyright (c) 2013 Daniele Bartolini, Michele Rossi
- Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
- Permission is hereby granted, free of charge, to any person
- obtaining a copy of this software and associated documentation
- files (the "Software"), to deal in the Software without
- restriction, including without limitation the rights to use,
- copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the
- Software is furnished to do so, subject to the following
- conditions:
- The above copyright notice and this permission notice shall be
- included in all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
- HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- OTHER DEALINGS IN THE SOFTWARE.
- */
- #pragma once
- #include "event_stream.h"
- #include "physics_types.h"
- #include "PxActor.h"
- #include "PxRigidActor.h"
- #include "PxController.h"
- #include "PxSimulationEventCallback.h"
- #include "PxQueryReport.h"
- #include "PxQueryFiltering.h"
- using physx::PxActor;
- using physx::PxConstraintInfo;
- using physx::PxContactPair;
- using physx::PxContactPairHeader;
- using physx::PxContactPairHeader;
- using physx::PxContactPairHeaderFlag;
- using physx::PxContactPairPoint;
- using physx::PxControllerObstacleHit;
- using physx::PxControllerShapeHit;
- using physx::PxControllersHit;
- using physx::PxSimulationEventCallback;
- using physx::PxTriggerPair;
- using physx::PxTriggerPairFlag;
- using physx::PxU32;
- using physx::PxUserControllerHitReport;
- using physx::PxVec3;
- using physx::PxRaycastCallback;
- using physx::PxAgain;
- using physx::PxRaycastHit;
- using physx::PxPairFlag;
- using physx::PxContactPairFlag;
- namespace crown
- {
- class PhysicsWorld;
- class PhysicsSimulationCallback : public PxSimulationEventCallback
- {
- public:
- PhysicsSimulationCallback(EventStream& stream)
- : m_events(stream)
- {
- }
- void onConstraintBreak(PxConstraintInfo* /*constraints*/, PxU32 /*count*/)
- {
- // printf("COSTRAINTBREAK\n");
- }
- void onContact(const PxContactPairHeader& pair_header, const PxContactPair* pairs, PxU32 num_pairs)
- {
- // Do not report contact if either actor0 or actor1 or both have been deleted
- if (pair_header.flags & PxContactPairHeaderFlag::eDELETED_ACTOR_0 ||
- pair_header.flags & PxContactPairHeaderFlag::eDELETED_ACTOR_1) return;
- for (PxU32 pp = 0; pp < num_pairs; pp++)
- {
- const PxContactPair& cp = pairs[pp];
- // We are only interested in touch found or lost
- if ((cp.events & PxPairFlag::eNOTIFY_TOUCH_FOUND) == (PxPairFlag::Enum)0 &&
- (cp.events & PxPairFlag::eNOTIFY_TOUCH_LOST) == (PxPairFlag::Enum)0) continue;
- // Skip if either shape0 or shape1 or both have been deleted
- if (cp.flags & PxContactPairFlag::eDELETED_SHAPE_0 ||
- cp.flags & PxContactPairFlag::eDELETED_SHAPE_1) continue;
- PxContactPairPoint points[8];
- const PxU32 num_points = cp.extractContacts(points, 8);
- PxVec3 where(0, 0, 0);
- PxVec3 normal(0, 0, 0);
- for (PxU32 i = 0; i < num_points; i++)
- {
- where = points[i].position;
- normal = points[i].normal;
- }
- post_collision_event((Actor*) pair_header.actors[0]->userData,
- (Actor*) pair_header.actors[1]->userData,
- Vector3(where.x, where.y, where.z),
- Vector3(normal.x, normal.y, normal.z),
- (cp.events & PxPairFlag::eNOTIFY_TOUCH_FOUND) ?
- physics_world::CollisionEvent::BEGIN_TOUCH :
- physics_world::CollisionEvent::END_TOUCH);
- }
- }
- void onTrigger(PxTriggerPair* pairs, PxU32 count)
- {
- for (PxU32 pp = 0; pp < count; pp++)
- {
- const PxTriggerPair& tp = pairs[pp];
- // Do not report event if either trigger ot other shape or both have been deleted
- if (tp.flags & PxTriggerPairFlag::eDELETED_SHAPE_TRIGGER ||
- tp.flags & PxTriggerPairFlag::eDELETED_SHAPE_OTHER) continue;
- post_trigger_event((Actor*)tp.triggerActor->userData,
- (Actor*)tp.otherActor->userData,
- (tp.status & PxPairFlag::eNOTIFY_TOUCH_FOUND ?
- physics_world::TriggerEvent::BEGIN_TOUCH : physics_world::TriggerEvent::END_TOUCH));
- }
- }
- void onWake(PxActor** /*actors*/, PxU32 /*count*/)
- {
- }
- void onSleep(PxActor** /*actors*/, PxU32 /*count*/)
- {
- }
- private:
- void post_collision_event(Actor* actor0, Actor* actor1, const Vector3& where, const Vector3& normal, physics_world::CollisionEvent::Type type)
- {
- physics_world::CollisionEvent ev;
- ev.type = type;
- ev.actors[0] = actor0;
- ev.actors[1] = actor1;
- ev.where = where;
- ev.normal = normal;
- event_stream::write(m_events, physics_world::EventType::COLLISION, ev);
- }
- void post_trigger_event(Actor* trigger, Actor* other, physics_world::TriggerEvent::Type type)
- {
- physics_world::TriggerEvent ev;
- ev.type = type;
- ev.trigger = trigger;
- ev.other = other;
- event_stream::write(m_events, physics_world::EventType::TRIGGER, ev);
- }
- private:
- EventStream& m_events;
- };
- class PhysicsControllerCallback : public PxUserControllerHitReport
- {
- void onShapeHit(const PxControllerShapeHit& /*hit*/)
- {
- }
- void onControllerHit(const PxControllersHit& /*hit*/)
- {
- }
- void onObstacleHit(const PxControllerObstacleHit& /*hit*/)
- {
- }
- };
- } // namespace crown
|