瀏覽代碼

Add Trigger

Daniele Bartolini 12 年之前
父節點
當前提交
8355302954
共有 5 個文件被更改,包括 183 次插入0 次删除
  1. 1 0
      engine/physics/PhysicsTypes.h
  2. 27 0
      engine/physics/PhysicsWorld.cpp
  3. 10 0
      engine/physics/PhysicsWorld.h
  4. 82 0
      engine/physics/Trigger.cpp
  5. 63 0
      engine/physics/Trigger.h

+ 1 - 0
engine/physics/PhysicsTypes.h

@@ -31,6 +31,7 @@ namespace crown
 
 
 typedef Id ActorId;
 typedef Id ActorId;
 typedef Id ControllerId;
 typedef Id ControllerId;
+typedef Id TriggerId;
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 struct ActorType
 struct ActorType

+ 27 - 0
engine/physics/PhysicsWorld.cpp

@@ -32,6 +32,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Quaternion.h"
 #include "Quaternion.h"
 #include "SceneGraph.h"
 #include "SceneGraph.h"
 #include "Controller.h"
 #include "Controller.h"
+#include "Trigger.h"
 
 
 #include "PxPhysicsAPI.h"
 #include "PxPhysicsAPI.h"
 
 
@@ -59,6 +60,7 @@ PhysicsWorld::PhysicsWorld()
 	: m_scene(NULL)
 	: m_scene(NULL)
 	, m_actors_pool(default_allocator(), MAX_ACTORS, sizeof(Actor), CE_ALIGNOF(Actor))
 	, m_actors_pool(default_allocator(), MAX_ACTORS, sizeof(Actor), CE_ALIGNOF(Actor))
 	, m_controllers_pool(default_allocator(), MAX_CONTROLLERS, sizeof(Controller), CE_ALIGNOF(Controller))
 	, m_controllers_pool(default_allocator(), MAX_CONTROLLERS, sizeof(Controller), CE_ALIGNOF(Controller))
+	, m_triggers_pool(default_allocator(), MAX_TRIGGERS, sizeof(Trigger), CE_ALIGNOF(Trigger))
 {
 {
 	// Create scene
 	// Create scene
 	PxSceneDesc scene_desc(device()->physx()->getTolerancesScale());
 	PxSceneDesc scene_desc(device()->physx()->getTolerancesScale());
@@ -87,6 +89,9 @@ PhysicsWorld::PhysicsWorld()
 	PxRigidStatic* plane = device()->physx()->createRigidStatic(pose);
 	PxRigidStatic* plane = device()->physx()->createRigidStatic(pose);
 	PxShape* shape = plane->createShape(PxPlaneGeometry(), *mat);
 	PxShape* shape = plane->createShape(PxPlaneGeometry(), *mat);
 	m_scene->addActor(*plane);
 	m_scene->addActor(*plane);
+
+	// FIXME FIXME FIXME
+	create_trigger(Vector3(.5, .5, .5), Vector3(5.0, -3.0, 3), Quaternion::IDENTITY);
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
@@ -129,6 +134,22 @@ void PhysicsWorld::destroy_controller(ControllerId id)
 	m_controllers.destroy(id);
 	m_controllers.destroy(id);
 }
 }
 
 
+//-----------------------------------------------------------------------------
+TriggerId PhysicsWorld::create_trigger(const Vector3& half_extents, const Vector3& pos, const Quaternion& rot)
+{
+	Trigger* trigger = CE_NEW(m_triggers_pool, Trigger)(m_scene, half_extents, pos, rot);
+	return m_triggers.create(trigger);
+}
+
+//-----------------------------------------------------------------------------
+void PhysicsWorld::destroy_trigger(TriggerId id)
+{
+	CE_ASSERT(m_triggers.has(id), "Trigger does not exist");
+
+	CE_DELETE(m_triggers_pool, m_triggers.lookup(id));
+	m_triggers.destroy(id);
+}
+
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 Actor* PhysicsWorld::lookup_actor(ActorId id)
 Actor* PhysicsWorld::lookup_actor(ActorId id)
 {
 {
@@ -143,6 +164,12 @@ Controller* PhysicsWorld::lookup_controller(ControllerId id)
 	return m_controllers.lookup(id);
 	return m_controllers.lookup(id);
 }
 }
 
 
+//-----------------------------------------------------------------------------
+Trigger* PhysicsWorld::lookup_trigger(TriggerId id)
+{
+	CE_ASSERT(m_triggers.has(id), "Trigger does not exist");
+	return m_triggers.lookup(id);
+}
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 Vector3 PhysicsWorld::gravity() const
 Vector3 PhysicsWorld::gravity() const
 {
 {

+ 10 - 0
engine/physics/PhysicsWorld.h

@@ -40,6 +40,7 @@ using physx::PxDefaultCpuDispatcher;
 
 
 #define MAX_ACTORS 1024
 #define MAX_ACTORS 1024
 #define MAX_CONTROLLERS 1024
 #define MAX_CONTROLLERS 1024
+#define MAX_TRIGGERS 1024
 
 
 namespace crown
 namespace crown
 {
 {
@@ -48,6 +49,8 @@ struct PhysicsResource;
 struct Controller;
 struct Controller;
 struct Vector3;
 struct Vector3;
 struct Actor;
 struct Actor;
+struct Trigger;
+struct Quaternion;
 class SceneGraph;
 class SceneGraph;
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
@@ -64,8 +67,12 @@ public:
 	ControllerId				create_controller(const PhysicsResource* pr, SceneGraph& sg, int32_t node);
 	ControllerId				create_controller(const PhysicsResource* pr, SceneGraph& sg, int32_t node);
 	void						destroy_controller(ControllerId id);
 	void						destroy_controller(ControllerId id);
 
 
+	TriggerId					create_trigger(const Vector3& half_extents, const Vector3& pos, const Quaternion& rot);
+	void						destroy_trigger(TriggerId id);
+
 	Actor*						lookup_actor(ActorId id);
 	Actor*						lookup_actor(ActorId id);
 	Controller*					lookup_controller(ControllerId id);
 	Controller*					lookup_controller(ControllerId id);
+	Trigger*					lookup_trigger(TriggerId id);
 
 
 	Vector3						gravity() const;
 	Vector3						gravity() const;
 	void						set_gravity(const Vector3& g);
 	void						set_gravity(const Vector3& g);
@@ -83,6 +90,9 @@ public:
 
 
 	PoolAllocator				m_controllers_pool;
 	PoolAllocator				m_controllers_pool;
 	IdArray<MAX_CONTROLLERS, Controller*> m_controllers;
 	IdArray<MAX_CONTROLLERS, Controller*> m_controllers;
+
+	PoolAllocator				m_triggers_pool;
+	IdArray<MAX_TRIGGERS, Trigger*> m_triggers;
 };
 };
 
 
 } // namespace crown
 } // namespace crown

+ 82 - 0
engine/physics/Trigger.cpp

@@ -0,0 +1,82 @@
+/*
+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.
+*/
+
+#include "Device.h"
+#include "Matrix4x4.h"
+#include "Physics.h"
+#include "PxPhysicsAPI.h"
+#include "Quaternion.h"
+#include "Trigger.h"
+#include "Vector3.h"
+
+using physx::PxRigidDynamicFlag;
+using physx::PxMat44;
+using physx::PxTransform;
+using physx::PxActorFlag;
+using physx::PxVec3;
+using physx::PxReal;
+using physx::PxRigidBody;
+using physx::PxRigidDynamic;
+using physx::PxPlaneGeometry;
+using physx::PxSphereGeometry;
+using physx::PxBoxGeometry;
+using physx::PxRigidBodyExt;
+using physx::PxShape;
+using physx::PxShapeFlag;
+
+namespace crown
+{
+	
+//-----------------------------------------------------------------------------
+Trigger::Trigger(PxScene* scene, const Vector3& half_extents, const Vector3& pos, const Quaternion& rot)
+	: m_scene(scene)
+{
+	Matrix4x4 m(rot, pos);
+	m.transpose();
+	PxMat44 pose((PxReal*)(m.to_float_ptr()));
+
+	m_actor = device()->physx()->createRigidStatic(PxTransform(pose));
+	m_actor->userData = NULL;
+
+	m_mat = device()->physx()->createMaterial(0.5f, 0.5f, 0.5f);
+	PxShape* shape = m_actor->createShape(PxBoxGeometry(half_extents.x, half_extents.y, half_extents.z), *m_mat);
+	shape->setFlag(PxShapeFlag::eSIMULATION_SHAPE, false);
+	shape->setFlag(PxShapeFlag::eTRIGGER_SHAPE, true);
+
+	m_scene->addActor(*m_actor);
+}
+
+//-----------------------------------------------------------------------------
+Trigger::~Trigger()
+{
+	if (m_actor)
+	{
+		m_scene->removeActor(*m_actor);
+		m_actor->release();
+	}
+}
+
+} // namespace crown

+ 63 - 0
engine/physics/Trigger.h

@@ -0,0 +1,63 @@
+/*
+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 "Types.h"
+#include "PhysicsTypes.h"
+#include "Vector3.h"
+#include "Matrix4x4.h"
+#include "Quaternion.h"
+
+#include "PxPhysics.h"
+#include "PxScene.h"
+#include "PxRigidActor.h"
+
+using physx::PxRigidActor;
+using physx::PxMaterial;
+using physx::PxScene;
+
+namespace crown
+{
+
+struct Quaternion;
+struct Matrix4x4;
+struct Unit;
+class SceneGraph;
+
+struct Trigger
+{
+						Trigger(PxScene* scene, const Vector3& half_extents, const Vector3& pos, const Quaternion& rot);
+						~Trigger();
+
+public:
+
+	PxScene*			m_scene;
+	PxRigidActor* 		m_actor;
+	PxMaterial* 		m_mat;
+};
+
+} // namespace crown