| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- /*
- 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 "EventStream.h"
- #include "PhysicsTypes.h"
- #include "PxActor.h"
- #include "PxRigidActor.h"
- #include "PxController.h"
- #include "PxSimulationEventCallback.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;
- 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)
- {
- // printf("CONTACT\n");
- // Do not report contact if either actor0 or actor1 has been deleted
- if (pair_header.flags & PxContactPairHeaderFlag::eDELETED_ACTOR_0 || pair_header.flags & PxContactPairHeaderFlag::eDELETED_ACTOR_1) return;
- // printf("ACTOR0 = %.4X\n", pair_header.actors[0]->userData);
- // printf("ACTOR1 = %.4X\n", pair_header.actors[1]->userData);
- PxVec3 where;
- // printf("Num pairs = %d\n", num_pairs);
- for (PxU32 pp = 0; pp < num_pairs; pp++)
- {
- PxContactPairPoint points[8];
- const PxU32 num_points = pairs[pp].extractContacts(points, 8);
- // printf("Num points = %d\n", num_points);
- for (PxU32 i = 0; i < num_points; i++)
- {
- where = points[i].position;
- // printf("where = %.2f %.2f %.2f\n", where.x, where.y, where.z);
- }
- }
- physics_world::CollisionEvent ev;
- ev.actors[0] = (Actor*) pair_header.actors[0]->userData;
- ev.actors[1] = (Actor*) pair_header.actors[1]->userData;
- ev.where = Vector3(where.x, where.y, where.z);
- event_stream::write(m_events, physics_world::EventType::COLLISION, ev);
- }
- //-----------------------------------------------------------------------------
- void onTrigger(PxTriggerPair* pairs, PxU32 count)
- {
- // printf("TRIGGER\n");
- // printf("Num pairs = %d\n", count);
- for (PxU32 pp = 0; pp < count; pp++)
- {
- const PxTriggerPair& pair = pairs[pp];
- // Do not report event if either trigger ot other shape has been deleted
- if (pair.flags & PxTriggerPairFlag::eDELETED_SHAPE_TRIGGER || pair.flags & PxTriggerPairFlag::eDELETED_SHAPE_OTHER) continue;
- // TODO
- physics_world::TriggerEvent ev;
- event_stream::write(m_events, physics_world::EventType::TRIGGER, ev);
- }
- }
- //-----------------------------------------------------------------------------
- void onWake(PxActor** /*actors*/, PxU32 /*count*/)
- {
- // printf("WAKE\n");
- }
- //-----------------------------------------------------------------------------
- void onSleep(PxActor** /*actors*/, PxU32 /*count*/)
- {
- // printf("SLEEP\n");
- }
- private:
- EventStream& m_events;
- };
- //-----------------------------------------------------------------------------
- class PhysicsControllerCallback : public PxUserControllerHitReport
- {
- //-----------------------------------------------------------------------------
- void onShapeHit(const PxControllerShapeHit& hit)
- {
- // printf("SHAPE HIT\n");
- }
- //-----------------------------------------------------------------------------
- void onControllerHit(const PxControllersHit& hit)
- {
- // printf("CONTROLLER HIT\n");
- }
- //-----------------------------------------------------------------------------
- void onObstacleHit(const PxControllerObstacleHit& hit)
- {
- // printf("OBSTACLE HIT\n");
- }
- };
- } // namespace crown
|